{"id":1093333,"date":"2025-01-08T14:30:03","date_gmt":"2025-01-08T06:30:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1093333.html"},"modified":"2025-01-08T14:30:05","modified_gmt":"2025-01-08T06:30:05","slug":"python%e4%b8%ad%e7%9a%84for%e5%be%aa%e7%8e%af-%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%a2%9e%e9%87%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093333.html","title":{"rendered":"python\u4e2d\u7684for\u5faa\u73af \u5982\u4f55\u8bbe\u7f6e\u589e\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205954\/577fad8e-c59a-4e95-a0ab-35f49f724e6d.webp\" alt=\"python\u4e2d\u7684for\u5faa\u73af \u5982\u4f55\u8bbe\u7f6e\u589e\u91cf\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684for\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528range()\u51fd\u6570\u6765\u8bbe\u7f6e\u589e\u91cf<\/strong>\u3002<strong>\u901a\u8fc7\u4f20\u9012\u4e09\u4e2a\u53c2\u6570\u7ed9range()\u51fd\u6570\uff1a\u8d77\u59cb\u503c\u3001\u7ec8\u6b62\u503c\u548c\u6b65\u957f\uff0c\u53ef\u4ee5\u63a7\u5236\u5faa\u73af\u7684\u589e\u91cf<\/strong>\u3002\u4f8b\u5982\uff0crange(0, 10, 2)\u4f1a\u751f\u62100\u30012\u30014\u30016\u30018\uff0c\u6b65\u957f\u4e3a2\u3002<strong>\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\uff0c\u6216\u8005\u4f7f\u7528range()\u51fd\u6570\u751f\u6210\u7684\u6570\u5b57\u5e8f\u5217\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0crange()\u51fd\u6570\u751f\u6210\u7684\u6570\u5b57\u5e8f\u5217\u7684\u6b65\u957f\u4e3a1\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u7b2c\u4e09\u4e2a\u53c2\u6570\u6765\u6539\u53d8\u6b65\u957f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u8bbe\u7f6e\u589e\u91cf\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u4f7f\u7528\u8fd9\u4e00\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528range()\u51fd\u6570\u8bbe\u7f6e\u589e\u91cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0crange()\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u6700\u591a\u4e09\u4e2a\u53c2\u6570\uff1astart\uff08\u8d77\u59cb\u503c\uff09\u3001stop\uff08\u7ec8\u6b62\u503c\uff0c\u4e0d\u5305\u62ec\u5728\u5185\uff09\u548cstep\uff08\u6b65\u957f\uff09\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cstep\u53c2\u6570\u7684\u503c\u4e3a1\uff0c\u4f46\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528range()\u51fd\u6570\u8bbe\u7f6e\u589e\u91cf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(0, 10, 2):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(0, 10, 2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u52309\uff08\u4e0d\u5305\u62ec10\uff09\u7684\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a0\u30012\u30014\u30016\u30018\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u8d1f\u6b65\u957f<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6b65\u957f\u6765\u751f\u6210\u9012\u51cf\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10, 0, -2):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(10, 0, -2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece10\u52301\uff08\u4e0d\u5305\u62ec0\uff09\u7684\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a-2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a10\u30018\u30016\u30014\u30012\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u5217\u8868\u548c\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5217\u8868\u65f6\u4f7f\u7528\u589e\u91cf<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5728\u904d\u5386\u5217\u8868\u65f6\u4f7f\u7528\u7279\u5b9a\u7684\u589e\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>for i in range(0, len(my_list), 2):<\/p>\n<p>    print(my_list[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(0, len(my_list), 2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u5230\u5217\u8868\u957f\u5ea6\u51cf1\u7684\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a1\u30013\u30015\u30017\u30019\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u8bbe\u7f6e\u589e\u91cf\u4e5f\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff08\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u4f60\u60f3\u6bcf\u9694\u4e00\u884c\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>]<\/p>\n<p>for i in range(0, len(matrix), 2):<\/p>\n<p>    print(matrix[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(0, len(matrix), 2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u5230\u77e9\u9635\u884c\u6570\u51cf1\u7684\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[1, 2, 3]<\/p>\n<p>[7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u5b57\u7b26\u4e32\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528\u589e\u91cf<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u4e5f\u662f\u4e00\u79cd\u5e8f\u5217\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u548crange()\u51fd\u6570\u6765\u904d\u5386\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6bcf\u9694\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;abcdefghij&quot;<\/p>\n<p>for i in range(0, len(my_string), 2):<\/p>\n<p>    print(my_string[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(0, len(my_string), 2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u5230\u5b57\u7b26\u4e32\u957f\u5ea6\u51cf1\u7684\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1aa\u3001c\u3001e\u3001g\u3001i\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u53cd\u5411\u904d\u5386\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6b65\u957f\u6765\u53cd\u5411\u904d\u5386\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;abcdefghij&quot;<\/p>\n<p>for i in range(len(my_string)-1, -1, -2):<\/p>\n<p>    print(my_string[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(len(my_string)-1, -1, -2)\u751f\u6210\u4e86\u4e00\u4e2a\u4ece\u5b57\u7b26\u4e32\u957f\u5ea6\u51cf1\u52300\u7684\u9012\u51cf\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a-2\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1aj\u3001h\u3001f\u3001d\u3001b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u5176\u4ed6\u573a\u666f\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u81ea\u5b9a\u4e49\u6570\u5217<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528range()\u51fd\u6570\u751f\u6210\u5404\u79cd\u81ea\u5b9a\u4e49\u6570\u5217\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a\u4ece10\u5230100\uff0c\u6b65\u957f\u4e3a10\u7684\u6570\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10, 101, 10):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a10\u300120\u300130\u300140\u300150\u300160\u300170\u300180\u300190\u3001100\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6b65\u957f\u7684\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6b65\u957f\u7684\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u4ece1\u523020\uff0c\u6bcf\u6b21\u589e\u52a0\u4e00\u4e2a\u968f\u673a\u7684\u6b65\u957f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>i = 1<\/p>\n<p>while i &lt;= 20:<\/p>\n<p>    print(i)<\/p>\n<p>    i += random.randint(1, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0ci\u7684\u503c\u90fd\u589e\u52a0\u4e00\u4e2a\u968f\u673a\u7684\u6b65\u957f\uff081\u52303\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528range()\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3afor\u5faa\u73af\u8bbe\u7f6e\u589e\u91cf\u3002\u8fd9\u4e00\u6280\u672f\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u5305\u62ec\u904d\u5386\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u591a\u7ef4\u6570\u7ec4\u7b49\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u589e\u91cf\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684for\u5faa\u73af\u589e\u91cf\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684for\u5faa\u73af\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u589e\u91cf\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6efor\u5faa\u73af\u7684\u589e\u91cf\u503c\u3002<code>range(start, stop, step)<\/code>\u7684\u8bed\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7ed3\u675f\u503c\u548c\u589e\u91cf\u3002\u4f8b\u5982\uff0c<code>for i in range(0, 10, 2)<\/code>\u5c06\u4ea7\u751f0\u30012\u30014\u30016\u30018\u7684\u5e8f\u5217\uff0c\u5176\u4e2d\u589e\u91cf\u4e3a2\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<p><strong>\u5728for\u5faa\u73af\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u975e\u6574\u6570\u589e\u91cf\uff1f<\/strong><br \/>Python\u7684for\u5faa\u73af\u901a\u5e38\u4f7f\u7528\u6574\u6570\u589e\u91cf\uff0c\u4f46\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u6d6e\u70b9\u6570\u589e\u91cf\u3002\u4f7f\u7528<code>numpy.arange(start, stop, step)<\/code>\u53ef\u4ee5\u751f\u6210\u6d6e\u70b9\u6570\u5e8f\u5217\uff0c<code>for i in np.arange(0.0, 1.0, 0.1)<\/code>\u5c06\u8f93\u51fa0.0\u52300.9\u4e4b\u95f4\u7684\u6570\uff0c\u589e\u91cf\u4e3a0.1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728for\u5faa\u73af\u4e2d\u5b9e\u73b0\u5012\u5e8f\u904d\u5386\uff1f<\/strong><br \/>\u8981\u5728for\u5faa\u73af\u4e2d\u5b9e\u73b0\u5012\u5e8f\u904d\u5386\uff0c\u53ef\u4ee5\u5c06<code>range()<\/code>\u51fd\u6570\u7684\u8d77\u59cb\u503c\u548c\u7ed3\u675f\u503c\u53cd\u8f6c\uff0c\u5e76\u8bbe\u7f6e\u8d1f\u589e\u91cf\u3002\u4f8b\u5982\uff0c<code>for i in range(10, 0, -1)<\/code>\u5c06\u8f93\u51fa10\u52301\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u589e\u91cf\u4e3a-1\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u53cd\u5411\u5904\u7406\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684for\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528range()\u51fd\u6570\u6765\u8bbe\u7f6e\u589e\u91cf\u3002\u901a\u8fc7\u4f20\u9012\u4e09\u4e2a\u53c2\u6570\u7ed9range()\u51fd\u6570\uff1a\u8d77\u59cb [&hellip;]","protected":false},"author":3,"featured_media":1093338,"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\/1093333"}],"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=1093333"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093333\/revisions"}],"predecessor-version":[{"id":1093340,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093333\/revisions\/1093340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}