{"id":1043156,"date":"2024-12-31T13:03:37","date_gmt":"2024-12-31T05:03:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043156.html"},"modified":"2024-12-31T13:03:40","modified_gmt":"2024-12-31T05:03:40","slug":"python%e5%a6%82%e4%bd%95%e6%b1%821%e5%88%b0100%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043156.html","title":{"rendered":"python\u5982\u4f55\u6c421\u5230100\u7684\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a0e43fd2-5555-46d0-979f-94c9645ddbdd.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6c421\u5230100\u7684\u548c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6c421\u5230100\u7684\u548c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570sum()\u7b49\u3002\u63a8\u8350\u4f7f\u7528for\u5faa\u73af\u548csum()\u5185\u7f6e\u51fd\u6570\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u66f4\u7b80\u6d01\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u662f\u6c421\u5230100\u7684\u548c\u7684\u5e38\u89c1\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u53ef\u4ee5\u904d\u5386\u4ece1\u5230100\u7684\u6bcf\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u7d2f\u52a0\u8d77\u6765\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = 0<\/p>\n<p>for i in range(1, 101):<\/p>\n<p>    total += i<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c<code>total<\/code>\u662f\u7d2f\u52a0\u5668\uff0c<code>range(1, 101)<\/code>\u751f\u6210\u4ece1\u5230100\u7684\u6570\u5b57\u5e8f\u5217\uff0cfor\u5faa\u73af\u904d\u5386\u8fd9\u4e9b\u6570\u5b57\u5e76\u5c06\u5b83\u4eec\u76f8\u52a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>while\u5faa\u73af\u662f\u53e6\u4e00\u79cd\u5b9e\u73b0\u65b9\u6cd5\u3002\u76f8\u6bd4for\u5faa\u73af\uff0cwhile\u5faa\u73af\u9700\u8981\u624b\u52a8\u63a7\u5236\u5faa\u73af\u53d8\u91cf\u548c\u7ec8\u6b62\u6761\u4ef6\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = 0<\/p>\n<p>i = 1<\/p>\n<p>while i &lt;= 100:<\/p>\n<p>    total += i<\/p>\n<p>    i += 1<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c<code>i<\/code>\u662f\u5faa\u73af\u53d8\u91cf\uff0c\u521d\u59cb\u503c\u4e3a1\uff0c\u5faa\u73af\u6761\u4ef6\u662f<code>i &lt;= 100<\/code>\uff0c\u6bcf\u6b21\u5faa\u73af\u540e<code>i<\/code>\u9012\u589e1\uff0c\u76f4\u5230<code>i<\/code>\u5927\u4e8e100\uff0c\u5faa\u73af\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570sum()\u53ef\u4ee5\u7528\u4e8e\u5bf9\u4efb\u610f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5143\u7d20\u6c42\u548c\uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = sum(range(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>range(1, 101)<\/code>\u751f\u6210\u4ece1\u5230100\u7684\u6570\u5b57\u5e8f\u5217\uff0c<code>sum()<\/code>\u51fd\u6570\u5bf9\u8fd9\u4e2a\u5e8f\u5217\u7684\u6240\u6709\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6570\u5b66\u516c\u5f0f<\/h3>\n<\/p>\n<p><p>\u6c421\u5230100\u7684\u548c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u516c\u5f0f\u76f4\u63a5\u8ba1\u7b97\u3002\u6839\u636e\u7b49\u5dee\u6570\u5217\u6c42\u548c\u516c\u5f0fS = n\/2 * (a + l)\uff0c\u5176\u4e2dn\u662f\u9879\u6570\uff0ca\u662f\u9996\u9879\uff0cl\u662f\u672b\u9879\u3002\u5bf9\u4e8e1\u5230100\u7684\u548c\uff0cn=100\uff0ca=1\uff0cl=100\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 100<\/p>\n<p>total = n * (n + 1) \/\/ 2<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u8fd9\u4e2a\u516c\u5f0f\u662f\u7b49\u5dee\u6570\u5217\u7684\u6c42\u548c\u516c\u5f0f\uff0c\u76f4\u63a5\u5c06\u9879\u6570\u548c\u9996\u672b\u9879\u4ee3\u5165\u516c\u5f0f\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u4e00\u79cd\u7f16\u7a0b\u65b9\u6cd5\u3002\u9012\u5f52\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6c421\u5230100\u7684\u548c\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_sum(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n + recursive_sum(n - 1)<\/p>\n<p>total = recursive_sum(100)<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u9012\u5f52\u51fd\u6570<code>recursive_sum<\/code>\u8c03\u7528\u81ea\u8eab\u8ba1\u7b97n\u7684\u548c\uff0c\u5f53n\u7b49\u4e8e1\u65f6\uff0c\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56den\u52a0\u4e0a<code>recursive_sum(n - 1)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u8bed\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\u751f\u62101\u5230100\u7684\u5217\u8868\uff0c\u5e76\u4f7f\u7528sum()\u51fd\u6570\u6c42\u548c\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = sum([i for i in range(1, 101)])<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5217\u8868\u89e3\u6790\u751f\u6210\u4ece1\u5230100\u7684\u5217\u8868\uff0c<code>sum()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u5b66\u51fd\u6570\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u9ad8\u6548\u5730\u6c421\u5230100\u7684\u548c\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>total = np.sum(np.arange(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>np.arange(1, 101)<\/code>\u751f\u6210\u4ece1\u5230100\u7684\u6570\u7ec4\uff0c<code>np.sum()<\/code>\u51fd\u6570\u5bf9\u6570\u7ec4\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h3>\n<\/p>\n<p><p>reduce\u51fd\u6570\u662ffunctools\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u5e8f\u5217\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>total = reduce(lambda x, y: x + y, range(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>reduce<\/code>\u51fd\u6570\u5bf9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\uff0c<code>lambda x, y: x + y<\/code>\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u8868\u793a\u4e24\u4e2a\u6570\u76f8\u52a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u89e3\u6790\uff0c\u4f46\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u60f0\u6027\u6c42\u503c\u7684\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6574\u4e2a\u5217\u8868\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = sum(i for i in range(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u751f\u6210\u4ece1\u5230100\u7684\u5e8f\u5217\uff0c<code>sum()<\/code>\u51fd\u6570\u5bf9\u5e8f\u5217\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map\u51fd\u6570\u7528\u4e8e\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = sum(map(int, range(1, 101)))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>map(int, range(1, 101))<\/code>\u5c06range\u751f\u6210\u7684\u5e8f\u5217\u8f6c\u6362\u4e3a\u6574\u6570\uff0c<code>sum()<\/code>\u51fd\u6570\u5bf9\u5e8f\u5217\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528reduce\u548coperator.add<\/h3>\n<\/p>\n<p><p>operator\u6a21\u5757\u63d0\u4f9b\u4e86\u5e38\u7528\u7684\u8fd0\u7b97\u7b26\u51fd\u6570\uff0c\u53ef\u4ee5\u4e0ereduce\u7ed3\u5408\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>import operator<\/p>\n<p>total = reduce(operator.add, range(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>operator.add<\/code>\u662f\u52a0\u6cd5\u8fd0\u7b97\u7b26\u51fd\u6570\uff0c<code>reduce<\/code>\u51fd\u6570\u5bf9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528itertools.accumulate<\/h3>\n<\/p>\n<p><p>itertools\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u64cd\u4f5c\u51fd\u6570\uff0caccumulate\u51fd\u6570\u7528\u4e8e\u5bf9\u5e8f\u5217\u8fdb\u884c\u7d2f\u79ef\u6c42\u548c\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>total = list(itertools.accumulate(range(1, 101)))[-1]<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a<code>itertools.accumulate<\/code>\u751f\u6210\u7d2f\u79ef\u548c\u7684\u8fed\u4ee3\u5668\uff0c<code>list()<\/code>\u51fd\u6570\u5c06\u8fed\u4ee3\u5668\u8f6c\u6362\u4e3a\u5217\u8868\uff0c<code>[-1]<\/code>\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5373\u7d2f\u79ef\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u65b9\u6cd5\u5728Python\u4e2d\u6c421\u5230100\u7684\u548c\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570sum()\u3001\u6570\u5b66\u516c\u5f0f\u3001\u9012\u5f52\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u3001reduce\u51fd\u6570\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001map\u51fd\u6570\u3001reduce\u548coperator.add\u3001itertools.accumulate\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b9e\u9645\u9700\u6c42\u548c\u4e2a\u4eba\u7f16\u7a0b\u4e60\u60ef\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u6700\u7ec8\u90fd\u80fd\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u53735050\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b971\u5230100\u7684\u548c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u7ed3\u5408<code>range()<\/code>\u51fd\u6570\u6765\u8f7b\u677e\u8ba1\u7b971\u5230100\u7684\u548c\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">total = sum(range(1, 101))\nprint(total)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa5050\uff0c\u8fd9\u662f1\u5230100\u7684\u603b\u548c\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u6c421\u5230100\u7684\u548c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sum()<\/code>\u548c<code>range()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u9010\u4e2a\u7d2f\u52a0\uff1a<\/p>\n<pre><code class=\"language-python\">total = 0\nfor i in range(1, 101):\n    total += i\nprint(total)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u5199\u6cd5\u4e0d\u540c\uff0c\u4f46\u7ed3\u679c\u4e5f\u662f5050\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u6709\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u8ba1\u7b971\u5230100\u7684\u548c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5229\u7528\u6570\u5b66\u516c\u5f0f\u6765\u8ba1\u7b971\u5230n\u7684\u548c\uff0c\u516c\u5f0f\u4e3a\uff1a<code>n(n + 1) \/ 2<\/code>\u3002\u5bf9\u4e8e1\u5230100\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\uff1a<\/p>\n<pre><code class=\"language-python\">n = 100\ntotal = n * (n + 1) \/\/ 2\nprint(total)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u63d0\u9ad8\u4e86\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u9a8c\u8bc1\u8ba1\u7b97\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u7ed3\u679c\uff0c\u6bd4\u5982\u8ba1\u7b97\u5c0f\u8303\u56f4\u5185\u7684\u548c\u5e76\u624b\u52a8\u68c0\u67e5\uff1a<\/p>\n<pre><code class=\"language-python\">assert sum(range(1, 11)) == 55  # \u9a8c\u8bc11\u523010\u7684\u548c\u662f\u5426\u4e3a55\nassert sum(range(1, 101)) == 5050  # \u9a8c\u8bc11\u5230100\u7684\u548c\u662f\u5426\u4e3a5050\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>assert<\/code>\u8bed\u53e5\u53ef\u4ee5\u5feb\u901f\u9a8c\u8bc1\u4ee3\u7801\u7684\u51c6\u786e\u6027\uff0c\u5982\u679c\u6761\u4ef6\u4e0d\u6ee1\u8db3\uff0c\u7a0b\u5e8f\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6c421\u5230100\u7684\u548c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570sum [&hellip;]","protected":false},"author":3,"featured_media":1043166,"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\/1043156"}],"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=1043156"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043156\/revisions"}],"predecessor-version":[{"id":1043172,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043156\/revisions\/1043172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043166"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}