{"id":176491,"date":"2024-05-08T19:11:52","date_gmt":"2024-05-08T11:11:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176491.html"},"modified":"2024-05-08T19:11:57","modified_gmt":"2024-05-08T11:11:57","slug":"%e6%80%8e%e4%b9%88%e7%94%a8python-%e7%9a%84range%e5%87%bd%e6%95%b0%e6%b1%82%e5%92%8c%e5%95%8a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176491.html","title":{"rendered":"\u600e\u4e48\u7528Python  \u7684range\u51fd\u6570\u6c42\u548c\u554a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060137\/1c498afa-8976-444a-bc72-9d484fe0d570.webp\" alt=\"\u600e\u4e48\u7528Python  \u7684range\u51fd\u6570\u6c42\u548c\u554a\" \/><\/p>\n<p><p>Python \u7684 <code>range<\/code> \u51fd\u6570\u5e38\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff0c\u800c\u8fd9\u4e2a\u5e8f\u5217\u53ef\u4ee5\u8f7b\u677e\u5730\u7528\u4e8e\u6c42\u548c\u3002<strong>\u4f7f\u7528\u5faa\u73af\u7ed3\u5408 <code>range<\/code> \u51fd\u6570\u662f\u5b8c\u6210\u6c42\u548c\u7684\u5e38\u89c1\u65b9\u5f0f<\/strong>\uff0c\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528 Python \u5185\u7f6e\u7684 <code>sum<\/code> \u51fd\u6570\u76f4\u63a5\u5bf9 <code>range<\/code> \u5bf9\u8c61\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5728\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528 <code>sum<\/code> \u51fd\u6570\u662f\u6700\u4e3a\u7b80\u4fbf\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\uff0c\u6211\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528 <code>range<\/code> \u548c <code>sum<\/code> \u51fd\u6570\u6c42\u548c\u7684\u5177\u4f53\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u7528\u5faa\u73af\u5b9e\u73b0\u76f8\u540c\u7684\u7ed3\u679c\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u5c06\u63a2\u8ba8\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u6bcf\u79cd\u65b9\u6cd5\u7684\u5e94\u7528\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>sum<\/code> \u548c <code>range<\/code> \u51fd\u6570\u6c42\u548c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u4e14\u6700\u76f4\u63a5\u7684\u4f7f\u7528 <code>range<\/code> \u6765\u6c42\u548c\u7684\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u5185\u7f6e\u51fd\u6570 <code>sum<\/code>\u3002<code>sum<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5305\u62ec\u901a\u8fc7 <code>range<\/code> \u751f\u6210\u7684\u5e8f\u5217\uff09\u5e76\u8fd4\u56de\u6240\u6709\u5143\u7d20\u7684\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 sum \u51fd\u6570\u548c range \u51fd\u6570\u6c42\u5e8f\u5217\u7684\u548c<\/p>\n<p>total = sum(range(1, 101))  # \u8ba1\u7b97 1 \u5230 100 \u7684\u6574\u6570\u548c<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa1\u5230100\uff08\u5305\u62ec1\u548c100\uff09\u6240\u6709\u6574\u6570\u7684\u548c\uff0c\u4e5f\u5c31\u662f5050\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u5faa\u73af\u7ed3\u5408 <code>range<\/code> \u6c42\u548c<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4f7f\u7528 <code>sum<\/code> \u51fd\u6570\u662f\u6700\u7b80\u5355\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u4f46\u4e86\u89e3\u5982\u4f55\u7528\u5faa\u73af\u7ed3\u5408 <code>range<\/code> \u51fd\u6570\u5b9e\u73b0\u6c42\u548c\u4e5f\u662f\u6709\u76ca\u7684\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5faa\u73af\u548c range \u51fd\u6570\u6c42\u5e8f\u5217\u7684\u548c<\/p>\n<p>total = 0<\/p>\n<p>for number in range(1, 101):  # \u904d\u5386 1 \u5230 100 \u7684\u6574\u6570<\/p>\n<p>    total += number  # \u5c06\u6bcf\u4e2a\u6570\u7d2f\u52a0\u5230 total \u53d8\u91cf\u4e0a<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4f8b\u4e2d\uff0c<code>for<\/code> \u5faa\u73af\u9010\u4e00\u904d\u5386 <code>range<\/code> \u5bf9\u8c61\u751f\u6210\u7684\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6570\u5b57\u7d2f\u52a0\u5230\u53d8\u91cf <code>total<\/code> \u4e0a\u3002\u5b8c\u6210\u904d\u5386\u540e\uff0c<code>total<\/code> \u5305\u542b\u4e86\u5e8f\u5217\u7684\u603b\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7406\u89e3 <code>range<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u4f7f\u7528 <code>range<\/code> \u51fd\u6570\u4e4b\u524d\uff0c\u7406\u89e3\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u91cd\u8981\u3002<strong><code>range<\/code> \u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff1a<code>start<\/code>\uff08\u8d77\u59cb\u503c\uff09\u3001<code>stop<\/code>\uff08\u7ed3\u675f\u503c\uff0c\u4f46\u4e0d\u5305\u62ec\u5728\u5185\uff09\u3001\u548c\u53ef\u9009\u7684 <code>step<\/code>\uff08\u6b65\u957f\uff09<\/strong>\u3002\u5f53\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u662f\u7ed3\u675f\u503c\uff0c\u6b64\u65f6\u8d77\u59cb\u503c\u4e3a0\uff0c\u6b65\u957f\u4e3a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># range \u51fd\u6570\u7684\u57fa\u672c\u4f7f\u7528<\/p>\n<p>r1 = range(10)  # \u4ece 0 \u5230 9<\/p>\n<p>r2 = range(1, 11)  # \u4ece 1 \u5230 10<\/p>\n<p>r3 = range(0, 100, 10)  # \u4ece 0 \u5230 90\uff0c\u6b65\u957f\u4e3a 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e86\u89e3 <code>range<\/code> \u7684\u8fd9\u4e9b\u7279\u6027\u540e\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5728\u5faa\u73af\u548c\u6c42\u548c\u64cd\u4f5c\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u5faa\u73af\u6c42\u548c<\/h3>\n<\/p>\n<p><p><strong>\u5f53\u5904\u7406\u5de8\u5927\u7684\u6570\u5b57\u5e8f\u5217\u65f6\uff0c\u4f18\u5316\u5faa\u73af\u6c42\u548c\u7684\u6027\u80fd\u662f\u5f88\u6709\u5fc5\u8981\u7684<\/strong>\u3002\u867d\u7136\u5728\u6c42\u548c\u64cd\u4f5c\u4e0a <code>sum<\/code> \u51fd\u6570\u5df2\u7ecf\u8db3\u591f\u9ad8\u6548\uff0c\u4f46\u5728\u5faa\u73af\u4e2d\uff0c\u6709\u65f6\u5019\u91c7\u7528\u66f4\u4f18\u96c5\u6216\u8005\u8bf4\u662f\u66f4\u201cPythonic\u201d\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u53d8\u5f97\u66f4\u7b80\u6d01\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff08List Comprehension\uff09\u7ed3\u5408 <code>sum<\/code> \u51fd\u6570\u8fdb\u884c\u6c42\u548c\u7684\u4f8b\u5b50\uff0c\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c sum \u51fd\u6570\u6c42\u548c<\/p>\n<p>total = sum([number for number in range(1, 101)])<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u5e94\u5f53\u6ce8\u610f\u7684\u662f\uff0c\u5217\u8868\u89e3\u6790\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b9e\u9645\u7684\u5217\u8868\uff0c\u5982\u679c\u6570\u5217\u975e\u5e38\u5927\uff0c\u5c06\u4f1a\u5360\u7528\u5927\u91cf\u7684\u5185\u5b58\u3002\u56e0\u6b64\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u5217\u89c4\u6a21\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u9636\u7528\u6cd5\uff1a\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6c42\u548c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u6c42\u548c\u65f6\u517c\u987e\u6027\u80fd\u548c\u5185\u5b58\u4f7f\u7528\uff0c<strong>\u63a8\u8350\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff08Generator Expression\uff09\u800c\u4e0d\u662f\u5217\u8868\u89e3\u6790<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c sum \u51fd\u6570\u6c42\u548c<\/p>\n<p>total = sum(number for number in range(1, 101))<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u6574\u4e2a\u5217\u8868\uff0c\u800c\u662f\u901a\u8fc7\u8fed\u4ee3\u4e00\u6b21\u8fd4\u56de\u4e00\u4e2a\u6570\uff0c\u56e0\u6b64\u66f4\u52a0\u5185\u5b58\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001<code>range<\/code> \u6c42\u548c\u7684\u6570\u5b66\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u7f16\u7a0b\u65b9\u6cd5\u5916\uff0c\u6570\u5b66\u4e0a\u53ef\u4ee5\u5229\u7528\u7b49\u5dee\u6570\u5217\u6c42\u548c\u516c\u5f0f\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\uff0c\u5bf9\u4e8e1\u5230n\u7684\u6574\u6570\u548c\uff0c\u516c\u5f0f\u4e3a <code>n*(n+1)\/2<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7b49\u5dee\u6570\u5217\u6c42\u548c\u516c\u5f0f<\/p>\n<p>n = 100<\/p>\n<p>total = n * (n + 1) \/\/ 2  # \u76f4\u63a5\u8ba1\u7b971\u5230100\u7684\u548c<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u65e0\u9700\u5faa\u73af\u6216\u751f\u6210\u4efb\u4f55\u4e2d\u95f4\u5e8f\u5217\uff0c\u76f4\u63a5\u8ba1\u7b97\u5f97\u51fa\u7ed3\u679c\u3002\u8fd9\u662f\u6700\u4e3a\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u4e0d\u8fc7\u5b83\u4ec5\u9002\u7528\u4e8e\u8fde\u7ed1\u65f6\u95f4\u4e0d acumulative \u8fd9\u79cd\u7279\u5b9a\u7684\u6570\u5217\u548c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528Python\u7684 <code>range<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u6c42\u548c\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u73af\u5883\u9009\u62e9\u6700\u5408\u9002\u7684\u6280\u672f\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7684range\u51fd\u6570\u6765\u5b9e\u73b0\u6c42\u548c\u64cd\u4f5c\uff1f<\/strong><\/p>\n<ol>\n<li><strong>\u5982\u4f55\u4f7f\u7528range\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6570\u503c\u8303\u56f4\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528range\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u8fde\u7eed\u6574\u6570\u5e8f\u5217\u3002range\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662frange(start, stop, step)\uff0c\u5176\u4e2dstart\u4e3a\u8d77\u59cb\u503c\uff08\u9ed8\u8ba4\u4e3a0\uff09\uff0cstop\u4e3a\u7ec8\u6b62\u503c\uff08\u5fc5\u9009\uff09\uff0cstep\u4e3a\u6b65\u957f\uff08\u9ed8\u8ba4\u4e3a1\uff09\u3002\u4f8b\u5982\uff0crange(1, 10, 2)\u5c06\u751f\u6210\u4e00\u4e2a\u4ece1\u52309\u7684\u5947\u6570\u5e8f\u5217\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u548crange\u51fd\u6570\u6765\u5b9e\u73b0\u6c42\u548c\u529f\u80fd\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u501f\u52a9\u4e8erange\u51fd\u6570\u548c\u5faa\u73af\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u4e00\u4e2a\u6570\u503c\u8303\u56f4\u5185\u7684\u6570\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4f7f\u7528for\u5faa\u73af\u548crange\u51fd\u6570\u6765\u5b9e\u73b0\u6c42\u548c\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">total = 0\nfor num in range(1, 11):\n    total += num\nprint(&quot;\u603b\u548c\u4e3a\uff1a&quot;, total)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8ba1\u7b97\u4ece1\u523010\u7684\u6574\u6570\u4e4b\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum\u548crange\u51fd\u6570\u6765\u5b9e\u73b0\u6c42\u548c\u529f\u80fd\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u9664\u4e86\u4f7f\u7528\u5faa\u73af\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570sum\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u5c06range\u51fd\u6570\u751f\u6210\u7684\u6570\u503c\u8303\u56f4\u4f5c\u4e3asum\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u6c42\u548c\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4f7f\u7528sum\u51fd\u6570\u548crange\u51fd\u6570\u6765\u8ba1\u7b97\u4ece1\u523010\u7684\u6574\u6570\u4e4b\u548c\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">total = sum(range(1, 11))\nprint(&quot;\u603b\u548c\u4e3a\uff1a&quot;, total)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u76f4\u63a5\u8ba1\u7b97\u51fa\u4ece1\u523010\u7684\u6574\u6570\u4e4b\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u7684 range \u51fd\u6570\u5e38\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff0c\u800c\u8fd9\u4e2a\u5e8f\u5217\u53ef\u4ee5\u8f7b\u677e\u5730\u7528\u4e8e\u6c42\u548c\u3002\u4f7f\u7528\u5faa\u73af\u7ed3\u5408 rang [&hellip;]","protected":false},"author":3,"featured_media":176500,"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\/176491"}],"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=176491"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176500"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}