{"id":1141519,"date":"2025-01-08T22:31:53","date_gmt":"2025-01-08T14:31:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141519.html"},"modified":"2025-01-08T22:31:56","modified_gmt":"2025-01-08T14:31:56","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%b1%821%e4%b8%80%e7%9b%b4%e5%88%b0n%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141519.html","title":{"rendered":"\u5982\u4f55\u7528python\u6c421\u4e00\u76f4\u5230n\u7684\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104226\/c882b194-2da3-4aa6-8d3a-1eeacfff5378.webp\" alt=\"\u5982\u4f55\u7528python\u6c421\u4e00\u76f4\u5230n\u7684\u548c\" \/><\/p>\n<p><p> <strong>\u7528Python\u6c421\u5230n\u7684\u548c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u548c\u516c\u5f0f\u7b49\u65b9\u6cd5\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u76f8\u5173\u6982\u5ff5\u89e3\u91ca\uff0c\u4ee5\u5e2e\u52a9\u60a8\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u6c421\u5230n\u7684\u548c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u662f\u6c421\u5230n\u7684\u548c\u7684\u6700\u76f4\u89c2\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u4ece1\u5230n\u7684\u6bcf\u4e2a\u6570\u5b57\uff0c\u5e76\u5c06\u5b83\u4eec\u7d2f\u52a0\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u7ec8\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001for\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5e38\u89c1\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_using_for(n):<\/p>\n<p>    total = 0<\/p>\n<p>    for i in range(1, n + 1):<\/p>\n<p>        total += i<\/p>\n<p>    return total<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 10<\/p>\n<p>print(f&quot;1\u5230{n}\u7684\u548c\u662f: {sum_using_for(n)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf<code>total<\/code>\u4e3a0\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u4ece1\u904d\u5386\u5230n\uff0c\u6bcf\u6b21\u5faa\u73af\u5c06\u5f53\u524d\u7684\u6570\u5b57<code>i<\/code>\u52a0\u5230<code>total<\/code>\u4e2d\uff0c\u6700\u540e\u8fd4\u56de<code>total<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001while\u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u6765\u6c421\u5230n\u7684\u548c\uff0c\u903b\u8f91\u4e0a\u4e0e<code>for<\/code>\u5faa\u73af\u76f8\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_using_while(n):<\/p>\n<p>    total = 0<\/p>\n<p>    i = 1<\/p>\n<p>    while i &lt;= n:<\/p>\n<p>        total += i<\/p>\n<p>        i += 1<\/p>\n<p>    return total<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 10<\/p>\n<p>print(f&quot;1\u5230{n}\u7684\u548c\u662f: {sum_using_while(n)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u540c\u6837\u521d\u59cb\u5316<code>total<\/code>\u4e3a0\uff0c\u5e76\u4e14\u5b9a\u4e49\u4e00\u4e2a\u8ba1\u6570\u5668<code>i<\/code>\u4ece1\u5f00\u59cb\uff0c<code>while<\/code>\u5faa\u73af\u5728<code>i<\/code>\u5c0f\u4e8e\u7b49\u4e8e<code>n<\/code>\u65f6\u7ee7\u7eed\uff0c\u6bcf\u6b21\u5faa\u73af\u5c06<code>i<\/code>\u52a0\u5230<code>total<\/code>\uff0c\u5e76\u5c06<code>i<\/code>\u52a01\uff0c\u6700\u7ec8\u8fd4\u56de<code>total<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u6c421\u5230n\u7684\u548c<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u4e00\u4e9b\u91cd\u590d\u6027\u95ee\u9898\u3002\u5728\u6c421\u5230n\u7684\u548c\u65f6\uff0c\u9012\u5f52\u4e5f\u53ef\u4ee5\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_using_recursion(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n + sum_using_recursion(n - 1)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 10<\/p>\n<p>print(f&quot;1\u5230{n}\u7684\u548c\u662f: {sum_using_recursion(n)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u9012\u5f52\u51fd\u6570<code>sum_using_recursion<\/code>\u9996\u5148\u68c0\u67e5\u662f\u5426<code>n<\/code>\u7b49\u4e8e1\uff0c\u5982\u679c\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de<code>n<\/code>\u52a0\u4e0a<code>sum_using_recursion(n - 1)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u5bf9\u4e8e\u8f83\u5927\u7684<code>n<\/code>\uff0c\u9012\u5f52\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u6c421\u5230n\u7684\u548c<\/h3>\n<\/p>\n<p><p>\u6700\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3002\u6839\u636e\u7b49\u5dee\u6570\u5217\u7684\u6c42\u548c\u516c\u5f0f\uff0c1\u5230n\u7684\u548c\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ S = \\frac{n(n + 1)}{2} ]<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_using_formula(n):<\/p>\n<p>    return n * (n + 1) \/\/ 2<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 10<\/p>\n<p>print(f&quot;1\u5230{n}\u7684\u548c\u662f: {sum_using_formula(n)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u516c\u5f0f\u8ba1\u7b971\u5230n\u7684\u548c\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u8fd0\u884c\u6548\u7387\u9ad8\u3002<strong>\u8fd9\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8f83\u5927\u6570\u503c\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6c421\u5230n\u7684\u548c<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u6765\u6c421\u5230n\u7684\u548c\uff0c\u6bd4\u5982<code>sum<\/code>\u51fd\u6570\u7ed3\u5408<code>range<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_using_builtin(n):<\/p>\n<p>    return sum(range(1, n + 1))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 10<\/p>\n<p>print(f&quot;1\u5230{n}\u7684\u548c\u662f: {sum_using_builtin(n)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>range<\/code>\u51fd\u6570\u751f\u6210\u4ece1\u5230n\u7684\u5e8f\u5217\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u6c42\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u5229\u7528\u4e86Python\u5185\u7f6e\u51fd\u6570\u7684\u9ad8\u6548\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u57fa\u51c6\u6d4b\u8bd5\u6765\u6bd4\u8f83\u5b83\u4eec\u7684\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u65b9\u6cd5\u7684\u7b80\u5355\u6027\u80fd\u6d4b\u8bd5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def performance_test():<\/p>\n<p>    n = 1000000<\/p>\n<p>    start = time.time()<\/p>\n<p>    sum_using_for(n)<\/p>\n<p>    print(f&quot;for\u5faa\u73af\u65f6\u95f4: {time.time() - start:.6f}\u79d2&quot;)<\/p>\n<p>    start = time.time()<\/p>\n<p>    sum_using_while(n)<\/p>\n<p>    print(f&quot;while\u5faa\u73af\u65f6\u95f4: {time.time() - start:.6f}\u79d2&quot;)<\/p>\n<p>    start = time.time()<\/p>\n<p>    sum_using_recursion(n)<\/p>\n<p>    print(f&quot;\u9012\u5f52\u65f6\u95f4: {time.time() - start:.6f}\u79d2&quot;)<\/p>\n<p>    start = time.time()<\/p>\n<p>    sum_using_formula(n)<\/p>\n<p>    print(f&quot;\u516c\u5f0f\u65f6\u95f4: {time.time() - start:.6f}\u79d2&quot;)<\/p>\n<p>    start = time.time()<\/p>\n<p>    sum_using_builtin(n)<\/p>\n<p>    print(f&quot;\u5185\u7f6e\u51fd\u6570\u65f6\u95f4: {time.time() - start:.6f}\u79d2&quot;)<\/p>\n<p>performance_test()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5927\u6570\u503c\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<strong>\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u548c\u5185\u7f6e\u51fd\u6570\u662f\u6700\u5feb\u7684\u65b9\u6cd5<\/strong>\uff0c\u800c\u9012\u5f52\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5927\u6570\u503c\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u56db\u79cd\u7528Python\u6c421\u5230n\u7684\u548c\u7684\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u3001\u6570\u5b66\u516c\u5f0f\u548c\u5185\u7f6e\u51fd\u6570<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u548c\u5185\u7f6e\u51fd\u6570\u662f\u6700\u6709\u6548\u7684\u9009\u62e9\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u9700\u6c42\u4e0b\uff0c\u5176\u4ed6\u65b9\u6cd5\u4e5f\u6709\u5176\u4ef7\u503c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b971\u5230n\u7684\u548c\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u4ece1\u5230n\u7684\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>sum<\/code>\u51fd\u6570\u548c<code>range<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">n = 10  # \u8fd9\u91cc\u53ef\u4ee5\u66ff\u6362\u6210\u4efb\u610f\u6b63\u6574\u6570\ntotal_sum = sum(range(1, n + 1))\nprint(total_sum)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa1\u523010\u7684\u603b\u548c\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539n\u7684\u503c\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b971\u5230n\u7684\u548c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sum<\/code>\u548c<code>range<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u6765\u8ba1\u7b971\u5230n\u7684\u548c\u3002\u516c\u5f0f\u4e3a\uff1a<br \/>[ S = \\frac{n(n + 1)}{2} ]<br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">n = 10  # \u4fee\u6539\u4e3a\u60a8\u60f3\u8ba1\u7b97\u7684\u6570\u5b57\ntotal_sum = n * (n + 1) \/\/ 2\nprint(total_sum)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u8ba1\u7b97\u8f83\u5927\u6570\u5b57\u65f6\u4f1a\u66f4\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u679cn\u662f\u8d1f\u6570\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u8ba1\u7b971\u5230n\u7684\u548c\u65f6\uff0c\u5982\u679cn\u662f\u8d1f\u6570\uff0c\u901a\u5e38\u4f1a\u8fd4\u56de0\uff0c\u56e0\u4e3a\u5728\u6570\u5b66\u4e0a\uff0c\u4ece1\u5230\u8d1f\u6570\u7684\u8303\u56f4\u662f\u7a7a\u7684\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u4ee5\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff1a  <\/p>\n<pre><code class=\"language-python\">n = -5  # \u8d1f\u6570\u793a\u4f8b\nif n &lt; 1:\n    total_sum = 0\nelse:\n    total_sum = sum(range(1, n + 1))\nprint(total_sum)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728n\u4e3a\u8d1f\u6570\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u62a5\u9519\uff0c\u5e76\u4e14\u8fd4\u56de\u5408\u7406\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u6c421\u5230n\u7684\u548c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u548c\u516c\u5f0f\u7b49\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b [&hellip;]","protected":false},"author":3,"featured_media":1141529,"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\/1141519"}],"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=1141519"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141519\/revisions"}],"predecessor-version":[{"id":1141531,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141519\/revisions\/1141531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141529"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}