{"id":1121041,"date":"2025-01-08T19:04:50","date_gmt":"2025-01-08T11:04:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121041.html"},"modified":"2025-01-08T19:04:53","modified_gmt":"2025-01-08T11:04:53","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%87%ba1-30%e5%81%b6%e6%95%b0%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121041.html","title":{"rendered":"python\u5982\u4f55\u6253\u51fa1-30\u5076\u6570\u7684\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083439\/f41077a3-1966-498f-b186-15b9f1db1641.webp\" alt=\"python\u5982\u4f55\u6253\u51fa1-30\u5076\u6570\u7684\u548c\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u3002<\/strong> \u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u548cif\u8bed\u53e5\u6765\u904d\u53861\u523030\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u4e3a\u5076\u6570\u3002\u7136\u540e\uff0c\u5c06\u8fd9\u4e9b\u5076\u6570\u76f8\u52a0\u5f97\u5230\u6700\u7ec8\u7684\u548c\u3002<strong>\u4f8b\u5982\uff0c\u4f7f\u7528for\u5faa\u73af\u548cif\u8bed\u53e5\u6765\u904d\u53861\u523030\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u4e3a\u5076\u6570<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u548cif\u8bed\u53e5\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u5e76\u4f7f\u7528if\u8bed\u53e5\u6765\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u3002\u5982\u679c\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u603b\u548c\u4e2d\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_sum = 0<\/p>\n<p>for num in range(1, 31):<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        total_sum += num<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u904d\u5386\u4e861\u523030\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u4f7f\u7528if\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\uff08\u5373\u662f\u5426\u80fd\u88ab2\u6574\u9664\uff09\u3002\u5982\u679c\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230total_sum\u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u6253\u5370\u51fa\u6240\u6709\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u548csum\u51fd\u6570\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528for\u5faa\u73af\u548cif\u8bed\u53e5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u751f\u6210\u5f0f\u548csum\u51fd\u6570\u6765\u7b80\u5316\u4ee3\u7801\u3002\u5217\u8868\u751f\u6210\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u6765\u751f\u62101\u523030\u4e4b\u95f4\u7684\u6240\u6709\u5076\u6570\uff0c\u5e76\u4f7f\u7528sum\u51fd\u6570\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_sum = sum([num for num in range(1, 31) if num % 2 == 0])<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u5217\u8868\u3002\u7136\u540e\uff0csum\u51fd\u6570\u8ba1\u7b97\u4e86\u8fd9\u4e2a\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528while\u5faa\u73af\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528for\u5faa\u73af\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002while\u5faa\u73af\u662f\u4e00\u79cd\u5faa\u73af\u7ed3\u6784\uff0c\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u904d\u53861\u523030\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u4e3a\u5076\u6570\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_sum = 0<\/p>\n<p>num = 1<\/p>\n<p>while num &lt;= 30:<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        total_sum += num<\/p>\n<p>    num += 1<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u904d\u5386\u4e861\u523030\u4e4b\u95f4\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u4f7f\u7528if\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u3002\u5982\u679c\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230total_sum\u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u6253\u5370\u51fa\u6240\u6709\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u5c06\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u548c\u7684\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u65b9\u4fbf\u5730\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_even_sum(start, end):<\/p>\n<p>    total_sum = 0<\/p>\n<p>    for num in range(start, end + 1):<\/p>\n<p>        if num % 2 == 0:<\/p>\n<p>            total_sum += num<\/p>\n<p>    return total_sum<\/p>\n<p>total_sum = calculate_even_sum(1, 30)<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3acalculate_even_sum\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1astart\u548cend\uff0c\u8868\u793a\u8ba1\u7b97\u8303\u56f4\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u6570\u5b57\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528for\u5faa\u73af\u548cif\u8bed\u53e5\u904d\u5386\u8fd9\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u8ba1\u7b97\u5076\u6570\u7684\u548c\u3002\u6700\u540e\uff0c\u8fd4\u56de\u603b\u548c\u3002\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c\u5373\u53ef\u5f97\u52301\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u9012\u5f52\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u3002\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u95ee\u9898\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_even_sum_recursive(start, end):<\/p>\n<p>    if start &gt; end:<\/p>\n<p>        return 0<\/p>\n<p>    if start % 2 == 0:<\/p>\n<p>        return start + calculate_even_sum_recursive(start + 1, end)<\/p>\n<p>    else:<\/p>\n<p>        return calculate_even_sum_recursive(start + 1, end)<\/p>\n<p>total_sum = calculate_even_sum_recursive(1, 30)<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3acalculate_even_sum_recursive\u7684\u9012\u5f52\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1astart\u548cend\uff0c\u8868\u793a\u8ba1\u7b97\u8303\u56f4\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u6570\u5b57\u3002\u51fd\u6570\u5185\u90e8\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u6765\u904d\u5386\u8fd9\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u8ba1\u7b97\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>Python\u7684NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u7b80\u5316\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u548c\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.arange(1, 31)<\/p>\n<p>even_sum = np.sum(arr[arr % 2 == 0])<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, even_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b1\u523030\u4e4b\u95f4\u6240\u6709\u6570\u5b57\u7684\u6570\u7ec4\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u7b5b\u9009\u51fa\u5076\u6570\uff0c\u5e76\u4f7f\u7528np.sum\u51fd\u6570\u8ba1\u7b97\u8fd9\u4e9b\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b80\u5316\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u548c\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>series = pd.Series(range(1, 31))<\/p>\n<p>even_sum = series[series % 2 == 0].sum()<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, even_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b1\u523030\u4e4b\u95f4\u6240\u6709\u6570\u5b57\u7684Series\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u7b5b\u9009\u51fa\u5076\u6570\uff0c\u5e76\u4f7f\u7528sum\u65b9\u6cd5\u8ba1\u7b97\u8fd9\u4e9b\u5076\u6570\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7c7b\u4f3c\u4e8e\u5217\u8868\u751f\u6210\u5f0f\u7684\u7b80\u6d01\u8bed\u6cd5\uff0c\u4f46\u4e0d\u4f1a\u7acb\u5373\u521b\u5efa\u6574\u4e2a\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u7b80\u5316\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u548c\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_sum = sum(num for num in range(1, 31) if num % 2 == 0)<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, total_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u751f\u6210\u5668\u5bf9\u8c61\u3002\u7136\u540e\uff0csum\u51fd\u6570\u8ba1\u7b97\u4e86\u8fd9\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u4e2d\u6240\u6709\u6570\u5b57\u7684\u548c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528reduce\u51fd\u6570\u8ba1\u7b97\u5076\u6570\u548c<\/h3>\n<\/p>\n<p><p>Python\u7684functools\u6a21\u5757\u63d0\u4f9b\u4e86reduce\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528reduce\u51fd\u6570\u6765\u7b80\u5316\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u548c\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>even_sum = reduce(lambda acc, num: acc + num if num % 2 == 0 else acc, range(1, 31), 0)<\/p>\n<p>print(&quot;1\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\u662f\uff1a&quot;, even_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528reduce\u51fd\u6570\u5bf91\u523030\u4e4b\u95f4\u6240\u6709\u6570\u5b57\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u5982\u679c\u5f53\u524d\u6570\u5b57\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u7d2f\u79ef\u548c\u4e2d\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6027\u80fd\u4f18\u5316\u548c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5bf9\u4e8e\u5c0f\u8303\u56f4\u7684\u6570\u5b57\u8ba1\u7b97\uff0c\u6240\u6709\u65b9\u6cd5\u7684\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u8ba1\u7b97\uff0c\u4f7f\u7528NumPy\u6216Pandas\u5e93\u53ef\u80fd\u4f1a\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528timeit\u6a21\u5757\u5bf9\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u8fdb\u884c\u6bd4\u8f83\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6027\u80fd\u6bd4\u8f83\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>For loop and if statement<\/strong><\/h2>\n<p>time1 = timeit.timeit(&#39;&#39;&#39;<\/p>\n<p>total_sum = 0<\/p>\n<p>for num in range(1, 31):<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        total_sum += num<\/p>\n<p>&#39;&#39;&#39;, number=10000)<\/p>\n<h2><strong>List comprehension and sum function<\/strong><\/h2>\n<p>time2 = timeit.timeit(&#39;sum([num for num in range(1, 31) if num % 2 == 0])&#39;, number=10000)<\/p>\n<h2><strong>While loop<\/strong><\/h2>\n<p>time3 = timeit.timeit(&#39;&#39;&#39;<\/p>\n<p>total_sum = 0<\/p>\n<p>num = 1<\/p>\n<p>while num &lt;= 30:<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        total_sum += num<\/p>\n<p>    num += 1<\/p>\n<p>&#39;&#39;&#39;, number=10000)<\/p>\n<h2><strong>NumPy library<\/strong><\/h2>\n<p>time4 = timeit.timeit(&#39;&#39;&#39;<\/p>\n<p>import numpy as np<\/p>\n<p>arr = np.arange(1, 31)<\/p>\n<p>np.sum(arr[arr % 2 == 0])<\/p>\n<p>&#39;&#39;&#39;, number=10000)<\/p>\n<h2><strong>Pandas library<\/strong><\/h2>\n<p>time5 = timeit.timeit(&#39;&#39;&#39;<\/p>\n<p>import pandas as pd<\/p>\n<p>series = pd.Series(range(1, 31))<\/p>\n<p>series[series % 2 == 0].sum()<\/p>\n<p>&#39;&#39;&#39;, number=10000)<\/p>\n<p>print(f&quot;For loop and if statement: {time1} seconds&quot;)<\/p>\n<p>print(f&quot;List comprehension and sum function: {time2} seconds&quot;)<\/p>\n<p>print(f&quot;While loop: {time3} seconds&quot;)<\/p>\n<p>print(f&quot;NumPy library: {time4} seconds&quot;)<\/p>\n<p>print(f&quot;Pandas library: {time5} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528timeit\u6a21\u5757\u5bf9\u4e0d\u540c\u65b9\u6cd5\u8fdb\u884c\u4e86\u6027\u80fd\u6d4b\u8bd5\uff0c\u5e76\u6253\u5370\u4e86\u6bcf\u79cd\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\uff0c\u5e76\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5305\u62ecfor\u5faa\u73af\u548cif\u8bed\u53e5\u3001\u5217\u8868\u751f\u6210\u5f0f\u548csum\u51fd\u6570\u3001while\u5faa\u73af\u3001\u51fd\u6570\u5c01\u88c5\u3001\u9012\u5f52\u3001NumPy\u5e93\u3001Pandas\u5e93\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548creduce\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b971\u523030\u7684\u5076\u6570\u548c\uff1f<\/strong><br \/>\u8981\u8ba1\u7b971\u523030\u7684\u5076\u6570\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8303\u56f4\u4ece1\u523030\u7684\u6570\u5b57\u5217\u8868\uff0c\u7b5b\u9009\u51fa\u5076\u6570\u5e76\u8fdb\u884c\u6c42\u548c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">even_sum = sum(num for num in range(1, 31) if num % 2 == 0)\nprint(even_sum)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u4e86\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u8ba1\u7b97\u51fa\u6240\u6709\u5076\u6570\u7684\u548c\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\uff1f<\/strong><br \/>\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u76f8\u5bf9\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26\u3002\u4efb\u4f55\u4e00\u4e2a\u6570\u5b57\uff0c\u5982\u679c\u75282\u53d6\u6a21\u7684\u7ed3\u679c\u4e3a0\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6570\u5b57\u5c31\u662f\u5076\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">number = 4\nif number % 2 == 0:\n    print(f&quot;{number} \u662f\u5076\u6570&quot;)\nelse:\n    print(f&quot;{number} \u4e0d\u662f\u5076\u6570&quot;)\n<\/code><\/pre>\n<p><strong>\u80fd\u5426\u4f7f\u7528\u51fd\u6570\u6765\u8ba1\u7b971\u523030\u7684\u5076\u6570\u548c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u91cd\u7528\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">def sum_of_evens(start, end):\n    return sum(num for num in range(start, end + 1) if num % 2 == 0)\n\nresult = sum_of_evens(1, 30)\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u901a\u8fc7\u8c03\u7528<code>sum_of_evens<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7075\u6d3b\u8ba1\u7b97\u4efb\u610f\u8303\u56f4\u5185\u7684\u5076\u6570\u548c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u8ba1\u7b971\u523030\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u3002 \u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1121046,"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\/1121041"}],"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=1121041"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121041\/revisions"}],"predecessor-version":[{"id":1121050,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121041\/revisions\/1121050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121046"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}