{"id":1137609,"date":"2025-01-08T21:50:36","date_gmt":"2025-01-08T13:50:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137609.html"},"modified":"2025-01-08T21:50:38","modified_gmt":"2025-01-08T13:50:38","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8for%e4%b8%8eif%e4%b8%80%e8%b5%b7%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1137609.html","title":{"rendered":"python\u5982\u4f55\u7528for\u4e0eif\u4e00\u8d77\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101401\/b52eebb6-218e-4cfc-9c72-cc3468364248.webp\" alt=\"python\u5982\u4f55\u7528for\u4e0eif\u4e00\u8d77\u7528\" \/><\/p>\n<p><p> <strong>Python \u4e2d\u5982\u4f55\u7528 for \u548c if \u4e00\u8d77\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c<strong>for \u5faa\u73af<\/strong>\u548c<strong>if \u6761\u4ef6\u8bed\u53e5<\/strong>\u901a\u5e38\u7ed3\u5408\u4f7f\u7528\u4ee5\u4fbf\u904d\u5386\u6570\u636e\u7ed3\u6784\u5e76\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u3001\u5904\u7406\u6216\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002<strong>\u901a\u8fc7\u5c06 for \u548c if \u7ed3\u5408\u4f7f\u7528<\/strong>\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u903b\u8f91\u63a7\u5236\uff0c\u4f8b\u5982\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u5c55\u793a\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number % 2 == 0:<\/p>\n<p>        print(f&quot;{number} is even&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<strong>for \u5faa\u73af\u904d\u5386\u5217\u8868 numbers<\/strong>\uff0c<strong>if \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570<\/strong>\uff0c\u5e76\u4ec5\u6253\u5370\u5076\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728\u4e0d\u540c\u60c5\u5883\u4e0b\u5982\u4f55\u7ed3\u5408\u4f7f\u7528 for \u548c if\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n<p><h2>1\u3001\u904d\u5386\u5217\u8868\u5e76\u7b5b\u9009\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u904d\u5386\u5217\u8868\u5e76\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7b5b\u9009\u5143\u7d20\u662f for \u548c if \u7ec4\u5408\u4f7f\u7528\u7684\u4e00\u4e2a\u7ecf\u5178\u573a\u666f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u4f60\u5e0c\u671b\u4ec5\u6253\u5370\u957f\u5ea6\u5927\u4e8e 3 \u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&quot;apple&quot;, &quot;cat&quot;, &quot;dog&quot;, &quot;banana&quot;, &quot;kiwi&quot;]<\/p>\n<p>for word in words:<\/p>\n<p>    if len(word) &gt; 3:<\/p>\n<p>        print(word)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfor \u5faa\u73af\u904d\u5386\u5217\u8868 words\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\u5426\u5927\u4e8e 3\uff0c\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\uff0c\u5373\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u7ed3\u5408 for \u548c if\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_numbers = [number for number in numbers if number % 2 == 0]<\/p>\n<p>print(even_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5076\u6570\u7684\u65b0\u5217\u8868\u3002for \u8d1f\u8d23\u904d\u5386\u5217\u8868 numbers\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5d4c\u5957\u5faa\u73af\u4e0e\u6761\u4ef6<\/strong><\/h2>\n<p><h2>1\u3001\u904d\u5386\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u904d\u5386\u5d4c\u5957\u5217\u8868\uff0c\u5e76\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\uff0c\u4f60\u5e0c\u671b\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>for sublist in nested_list:<\/p>\n<p>    for item in sublist:<\/p>\n<p>        if item &gt; 5:<\/p>\n<p>            print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8 for \u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\uff0c\u5185\u90e8 for \u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5927\u4e8e 5\uff0c\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u5355\u4e2a if \u8bed\u53e5\u4e2d\u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5728\u904d\u5386\u5217\u8868\u65f6\u540c\u65f6\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number % 2 == 0 and number &gt; 5:<\/p>\n<p>        print(f&quot;{number} is even and greater than 5&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfor \u5faa\u73af\u904d\u5386\u5217\u8868 numbers\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u4e14\u5927\u4e8e 5\uff0c\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p><h2>1\u3001\u6570\u636e\u6e05\u6d17<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6e05\u6d17\u6570\u636e\u3002\u4f7f\u7528 for \u548c if \u53ef\u4ee5\u9ad8\u6548\u5730\u7b5b\u9009\u548c\u5904\u7406\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u5178\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b57\u5178\u4ee3\u8868\u4e00\u4e2a\u7528\u6237\u7684\u4fe1\u606f\uff0c\u4f60\u5e0c\u671b\u7b5b\u9009\u51fa\u5e74\u9f84\u5927\u4e8e 18 \u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">users = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 17},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 19}<\/p>\n<p>]<\/p>\n<p>adult_users = []<\/p>\n<p>for user in users:<\/p>\n<p>    if user[&quot;age&quot;] &gt; 18:<\/p>\n<p>        adult_users.append(user)<\/p>\n<p>print(adult_users)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfor \u5faa\u73af\u904d\u5386\u5217\u8868 users\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u7528\u6237\u7684\u5e74\u9f84\u662f\u5426\u5927\u4e8e 18\uff0c\u5e76\u5c06\u6ee1\u8db3\u6761\u4ef6\u7684\u7528\u6237\u6dfb\u52a0\u5230 adult_users \u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u6587\u4ef6\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0cfor \u548c if \u7684\u7ed3\u5408\u4f7f\u7528\u4e5f\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u904d\u5386\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u4ec5\u5904\u7406\u5305\u542b\u7279\u5b9a\u5173\u952e\u5b57\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keyword = &quot;error&quot;<\/p>\n<p>with open(&quot;logfile.txt&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        if keyword in line:<\/p>\n<p>            print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cwith \u8bed\u53e5\u6253\u5f00\u6587\u4ef6 logfile.txt\uff0cfor \u5faa\u73af\u904d\u5386\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\uff0cif \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u5173\u952e\u5b57 error\uff0c\u5e76\u4ec5\u6253\u5370\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n<p><h2>1\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 for \u548c if \u65f6\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u5faa\u73af\u4e2d\u591a\u6b21\u4f7f\u7528\u67d0\u4e2a\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u5916\u90e8\u8fdb\u884c\u8ba1\u7b97\u5e76\u5b58\u50a8\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>threshold = 5<\/p>\n<h2><strong>\u9884\u5148\u8ba1\u7b97\u6761\u4ef6<\/strong><\/h2>\n<p>is_even = [number % 2 == 0 for number in numbers]<\/p>\n<p>for i, number in enumerate(numbers):<\/p>\n<p>    if is_even[i] and number &gt; threshold:<\/p>\n<p>        print(f&quot;{number} is even and greater than {threshold}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cis_even \u5217\u8868\u5728\u5faa\u73af\u5916\u90e8\u9884\u5148\u8ba1\u7b97\u5e76\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u800c\u4e0d\u662f\u5217\u8868\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6548\u7387\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def even_numbers(numbers):<\/p>\n<p>    for number in numbers:<\/p>\n<p>        if number % 2 == 0:<\/p>\n<p>            yield number<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_gen = even_numbers(numbers)<\/p>\n<p>for even in even_gen:<\/p>\n<p>    print(even)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ceven_numbers \u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u4f7f\u7528 yield \u5173\u952e\u5b57\u5728\u9700\u8981\u65f6\u751f\u6210\u5076\u6570\u3002\u4f7f\u7528\u751f\u6210\u5668\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/strong><\/h2>\n<p><h2>1\u3001\u5904\u7406\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 for \u548c if \u65f6\uff0c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u662f\u786e\u4fdd\u4ee3\u7801\u5065\u58ee\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4f8b\u5982\uff0c\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6548\u6570\u636e\u6216\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [10, &quot;twenty&quot;, 30, None, 50]<\/p>\n<p>for item in data:<\/p>\n<p>    try:<\/p>\n<p>        if item &gt; 20:<\/p>\n<p>            print(f&quot;{item} is greater than 20&quot;)<\/p>\n<p>    except TypeError:<\/p>\n<p>        print(f&quot;Invalid data: {item}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ctry-except \u8bed\u53e5\u7528\u4e8e\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u7c7b\u578b\u9519\u8bef\uff0c\u5e76\u6253\u5370\u65e0\u6548\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u65ad\u8a00<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u65ad\u8a00\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6355\u6349\u9519\u8bef\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002\u65ad\u8a00\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u7279\u5b9a\u6761\u4ef6\uff0c\u5e76\u5728\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>for number in numbers:<\/p>\n<p>    assert isinstance(number, int), f&quot;Invalid data: {number}&quot;<\/p>\n<p>    if number % 2 == 0:<\/p>\n<p>        print(f&quot;{number} is even&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cassert \u8bed\u53e5\u7528\u4e8e\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u6574\u6570\uff0c\u5e76\u5728\u9047\u5230\u65e0\u6548\u6570\u636e\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u8fdb\u9636\u7528\u6cd5<\/strong><\/h2>\n<p><h2>1\u3001\u7ed3\u5408\u51fd\u6570\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5c06 for \u548c if \u7ed3\u5408\u4f7f\u7528\u5728\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u7b5b\u9009\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def filter_even(numbers):<\/p>\n<p>    return [number for number in numbers if number % 2 == 0]<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_numbers = filter_even(numbers)<\/p>\n<p>print(even_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfilter_even \u51fd\u6570\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b5b\u9009\u5076\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u7ed3\u5408\u7c7b\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u5c06 for \u548c if \u7ed3\u5408\u4f7f\u7528\u5728\u7c7b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u903b\u8f91\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class NumberProcessor:<\/p>\n<p>    def __init__(self, numbers):<\/p>\n<p>        self.numbers = numbers<\/p>\n<p>    def filter_even(self):<\/p>\n<p>        return [number for number in self.numbers if number % 2 == 0]<\/p>\n<p>    def filter_greater_than(self, threshold):<\/p>\n<p>        return [number for number in self.numbers if number &gt; threshold]<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>processor = NumberProcessor(numbers)<\/p>\n<p>print(processor.filter_even())<\/p>\n<p>print(processor.filter_greater_than(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cNumberProcessor \u7c7b\u5305\u542b\u4e24\u4e2a\u65b9\u6cd5\uff1afilter_even \u548c filter_greater_than\uff0c\u5206\u522b\u7528\u4e8e\u7b5b\u9009\u5076\u6570\u548c\u5927\u4e8e\u7279\u5b9a\u9608\u503c\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u7ed3\u8bba<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528 for \u5faa\u73af\u548c if \u6761\u4ef6\u8bed\u53e5\uff0cPython \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u904d\u5386\u548c\u5904\u7406\u6570\u636e\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5217\u8868\u7b5b\u9009\u3001\u590d\u6742\u7684\u5d4c\u5957\u5faa\u73af\uff0c\u8fd8\u662f\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e2d\u7684\u6570\u636e\u6e05\u6d17\u548c\u6587\u4ef6\u5904\u7406\uff0cfor \u548c if \u7684\u7ed3\u5408\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u6570\u636e\u5904\u7406\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6ce8\u610f\u6027\u80fd\u4f18\u5316\u548c\u9519\u8bef\u5904\u7406\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728 Python \u4e2d\u7ed3\u5408\u4f7f\u7528 for \u548c if \u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u8fdb\u4e00\u6b65\u7684\u8ba8\u8bba\uff0c\u6b22\u8fce\u968f\u65f6\u4ea4\u6d41\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528for\u5faa\u73af\u548cif\u6761\u4ef6\u8bed\u53e5\u4e00\u8d77\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed3\u5408for\u5faa\u73af\u548cif\u8bed\u53e5\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u8fc7\u6ee4\u548c\u5904\u7406\u3002\u4f60\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff0c\u5e76\u6839\u636e\u6761\u4ef6\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u6253\u5370\u51fa\u6240\u6709\u5076\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6]\nfor num in numbers:\n    if num % 2 == 0:\n        print(num)\n<\/code><\/pre>\n<p><strong>\u5728\u4f7f\u7528for\u548cif\u8bed\u53e5\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u7ed3\u5408for\u548cif\u8bed\u53e5\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3001\u7edf\u8ba1\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u4e2a\u6570\u3001\u5bf9\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u7b49\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e00\u4e2a\u5217\u8868\u4e2d\u5927\u4e8e10\u7684\u6570\u5b57\u6570\u91cf\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = [5, 10, 15, 20]\ncount = 0\nfor num in numbers:\n    if num &gt; 10:\n        count += 1\nprint(count)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u591a\u4e2aif\u6761\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u591a\u4e2aif\u6761\u4ef6\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u5b57\uff0c\u6253\u5370\u51fa\u65e2\u662f\u5076\u6570\u53c8\u5927\u4e8e10\u7684\u6570\u5b57\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = [5, 12, 8, 15, 22]\nfor num in numbers:\n    if num % 2 == 0 and num &gt; 10:\n        print(num)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4e2d\u5982\u4f55\u7528 for \u548c if \u4e00\u8d77\u4f7f\u7528 \u5728 Python \u4e2d\uff0cfor \u5faa\u73af\u548cif \u6761\u4ef6\u8bed\u53e5\u901a\u5e38\u7ed3\u5408 [&hellip;]","protected":false},"author":3,"featured_media":1137616,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137609"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1137609"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137609\/revisions"}],"predecessor-version":[{"id":1137618,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137609\/revisions\/1137618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1137616"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1137609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1137609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1137609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}