{"id":1098409,"date":"2025-01-08T15:20:56","date_gmt":"2025-01-08T07:20:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098409.html"},"modified":"2025-01-08T15:20:58","modified_gmt":"2025-01-08T07:20:58","slug":"python%e8%af%ad%e5%8f%a5%e4%b8%adfor%e5%be%aa%e7%8e%af%e5%a6%82%e4%bd%95%e5%86%99-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098409.html","title":{"rendered":"Python\u8bed\u53e5\u4e2dfor\u5faa\u73af\u5982\u4f55\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212730\/bc49bf4c-4890-4055-94df-a123948fd863.webp\" alt=\"Python\u8bed\u53e5\u4e2dfor\u5faa\u73af\u5982\u4f55\u5199\" \/><\/p>\n<p><p> <strong>Python\u8bed\u53e5\u4e2dfor\u5faa\u73af\u7684\u5199\u6cd5\u5305\u62ec\uff1a\u57fa\u7840\u8bed\u6cd5\u3001\u904d\u5386\u5217\u8868\u3001\u904d\u5386\u5143\u7ec4\u3001\u904d\u5386\u5b57\u5178\u3001\u904d\u5386\u5b57\u7b26\u4e32\u3001\u4f7f\u7528range\u51fd\u6570\u3001\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u904d\u5386\u5217\u8868\u548c\u4f7f\u7528range\u51fd\u6570\u3002\u904d\u5386\u5217\u8868\u662f\u6307\u901a\u8fc7for\u5faa\u73af\u9010\u4e2a\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u4f7f\u7528range\u51fd\u6570\u5219\u662f\u901a\u8fc7\u6307\u5b9a\u8303\u56f4\u6765\u751f\u6210\u4e00\u7cfb\u5217\u6570\u503c\uff0c\u5e76\u9010\u4e2a\u8bbf\u95ee\u8fd9\u4e9b\u6570\u503c\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0cfor\u5faa\u73af\u7684\u4f7f\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u57fa\u7840\u8bed\u6cd5<\/strong><\/p>\n<p>Python\u4e2d\u7684for\u5faa\u73af\u4e3b\u8981\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\uff0c\u5176\u57fa\u7840\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for variable in sequence:<\/p>\n<p>    # \u6267\u884c\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u8bed\u6cd5\u4e2d\uff0c<code>variable<\/code>\u662f\u5faa\u73af\u53d8\u91cf\uff0c<code>sequence<\/code>\u662f\u8981\u904d\u5386\u7684\u5e8f\u5217\u3002\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c<code>variable<\/code>\u4f1a\u88ab\u8d4b\u503c\u4e3a\u5e8f\u5217\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u6267\u884c\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u904d\u5386\u5217\u8868<\/strong><\/p>\n<p>\u5217\u8868\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728Python\u4e2d\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5373<code>apple<\/code>, <code>banana<\/code>, <code>cherry<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u5143\u7d20\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squares = []<\/p>\n<p>for number in numbers:<\/p>\n<p>    squares.append(number  2)<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u904d\u5386\u5143\u7ec4<\/strong><\/p>\n<p>\u5143\u7ec4\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u90fd\u662f\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3002\u904d\u5386\u5143\u7ec4\u7684\u65b9\u5f0f\u4e0e\u904d\u5386\u5217\u8868\u76f8\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coordinates = (1, 2, 3)<\/p>\n<p>for coordinate in coordinates:<\/p>\n<p>    print(coordinate)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5373<code>1<\/code>, <code>2<\/code>, <code>3<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u904d\u5386\u5b57\u5178<\/strong><\/p>\n<p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u904d\u5386\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u904d\u5386\u952e<\/strong><\/h2>\n<p>for key in person:<\/p>\n<p>    print(key, person[key])<\/p>\n<h2><strong>\u904d\u5386\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in person.items():<\/p>\n<p>    print(key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u4e5f\u662f\u4e00\u79cd\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u9010\u4e2a\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">message = &quot;Hello, World!&quot;<\/p>\n<p>for char in message:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u4f7f\u7528range\u51fd\u6570<\/strong><\/p>\n<p><code>range<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u6570\u503c\uff0c\u53ef\u4ee5\u4e0efor\u5faa\u73af\u914d\u5408\u4f7f\u7528\u3002\u5e38\u89c1\u7684\u7528\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4ece0\u52304\u7684\u6570\u503c<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<h2><strong>\u751f\u6210\u4ece1\u52305\u7684\u6570\u503c<\/strong><\/h2>\n<p>for i in range(1, 6):<\/p>\n<p>    print(i)<\/p>\n<h2><strong>\u751f\u6210\u4ece0\u52308\u7684\u5076\u6570<\/strong><\/h2>\n<p>for i in range(0, 10, 2):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>range<\/code>\u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u8d77\u59cb\u503c\u3001\u7ed3\u675f\u503c\u548c\u6b65\u957f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8d77\u59cb\u503c\u4e3a0\uff0c\u6b65\u957f\u4e3a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e03\u3001\u5d4c\u5957\u5faa\u73af<\/strong><\/p>\n<p>\u5d4c\u5957\u5faa\u73af\u662f\u6307\u5728\u4e00\u4e2a\u5faa\u73af\u4f53\u5185\u518d\u5d4c\u5957\u4e00\u4e2a\u6216\u591a\u4e2a\u5faa\u73af\uff0c\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5d4c\u5957\u5faa\u73af\u7684\u793a\u4f8b\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>]<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element, end=&#39; &#39;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u516b\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u5e38\u7528\u4e8e\u66ff\u4ee3\u4f20\u7edf\u7684for\u5faa\u73af\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squares = [number  2 for number in numbers]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6570\u5b57\u5e73\u65b9\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>Python\u4e2d\u7684for\u5faa\u73af\u662f\u975e\u5e38\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\uff0c\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5404\u79cd\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528for\u5faa\u73af\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684for\u5faa\u73af\u65b9\u5f0f\uff0c\u4f8b\u5982\u904d\u5386\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528range\u51fd\u6570\u751f\u6210\u6570\u503c\u5e8f\u5217\uff0c\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5904\u7406\u591a\u7ef4\u6570\u636e\uff0c\u6216\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u8f7b\u677e\u904d\u5386\u5217\u8868\u3002\u57fa\u672c\u8bed\u6cd5\u662f<code>for item in list:<\/code>\uff0c\u5176\u4e2d<code>item<\/code>\u4e3a\u5f53\u524d\u5faa\u73af\u7684\u5143\u7d20\uff0c<code>list<\/code>\u4e3a\u8981\u904d\u5386\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [1, 2, 3, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u904d\u5386\u5e76\u6253\u5370\u6bcf\u4e2a\u6570\u5b57\uff1a  <\/p>\n<pre><code class=\"language-python\">for number in numbers:\n    print(number)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa1\u52305\u7684\u6bcf\u4e2a\u6570\u5b57\u3002<\/p>\n<p><strong>for\u5faa\u73af\u4e2d\u5982\u4f55\u4f7f\u7528range()\u51fd\u6570\u751f\u6210\u5e8f\u5217\uff1f<\/strong><br \/>\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u751f\u6210\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u6307\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\u3002\u8bed\u6cd5\u4e3a<code>for i in range(start, stop):<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u4e3a\u8d77\u59cb\u6570\u5b57\uff0c<code>stop<\/code>\u4e3a\u7ed3\u675f\u6570\u5b57\uff08\u4e0d\u5305\u62ec\uff09\u3002\u4f8b\u5982\uff0c<code>for i in range(1, 6):<\/code>\u5c06\u751f\u62101\u52305\u7684\u5e8f\u5217\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(1, 6):\n    print(i)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa1\u52305\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5728for\u5faa\u73af\u4e2d\u5982\u4f55\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff1f<\/strong><br \/>\u5728for\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408if\u8bed\u53e5\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u904d\u5386\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u6253\u5370\u5217\u8868\u4e2d\u5927\u4e8e2\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u5728for\u5faa\u73af\u5185\u90e8\u6dfb\u52a0if\u6761\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]\nfor number in numbers:\n    if number &gt; 2:\n        print(number)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u53ea\u8f93\u51fa3\u30014\u548c5\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ee1\u8db3\u6761\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bed\u53e5\u4e2dfor\u5faa\u73af\u7684\u5199\u6cd5\u5305\u62ec\uff1a\u57fa\u7840\u8bed\u6cd5\u3001\u904d\u5386\u5217\u8868\u3001\u904d\u5386\u5143\u7ec4\u3001\u904d\u5386\u5b57\u5178\u3001\u904d\u5386\u5b57\u7b26\u4e32\u3001\u4f7f\u7528range\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1098413,"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\/1098409"}],"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=1098409"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098409\/revisions"}],"predecessor-version":[{"id":1098417,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098409\/revisions\/1098417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098413"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}