{"id":183896,"date":"2023-08-03T00:35:50","date_gmt":"2023-08-02T16:35:50","guid":{"rendered":"http:\/\/www.idc.net\/help\/183896\/"},"modified":"2023-08-03T00:35:50","modified_gmt":"2023-08-02T16:35:50","slug":"python-for%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/183896\/","title":{"rendered":"Python for\u5faa\u73af"},"content":{"rendered":"<p>\u5728 Python \u4e2d\uff0c<code>for<\/code>\u5173\u952e\u5b57\u63d0\u4f9b\u4e86\u66f4\u5168\u9762\u7684\u673a\u5236\u6765\u6784\u6210\u5faa\u73af\u3002 <code>for<\/code>\u5faa\u73af\u7528\u4e8e\u5e8f\u5217\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u8303\u56f4\u7b49\u3002<\/p>\n<p>\u5bf9\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u6210\u5458\u5143\u7d20\u6267\u884c<code>for<\/code>\u5faa\u73af\u7684\u4e3b\u4f53\u3002\u56e0\u6b64\uff0c\u5b83\u4e0d\u9700\u8981\u663e\u5f0f\u9a8c\u8bc1\u63a7\u5236\u5faa\u73af\u7684\u5e03\u5c14\u8868\u8fbe\u5f0f(\u5982 <code>while<\/code>\u5faa\u73af)\u3002<\/p>\n<p>Syntax:<\/p>\n<pre><code>for x in sequence:\n    statement1\n    statement2\n    ...\n    statementN\n<\/code><\/pre>\n<p>\u9996\u5148\uff0cfor \u8bed\u53e5\u4e2d\u7684\u53d8\u91cf<code>x<\/code>\u5f15\u7528\u5e8f\u5217\u4e2d 0 \u7d22\u5f15\u5904\u7684\u9879\u76ee\u3002 \u5c06\u6267\u884c<code>:<\/code>\u7b26\u53f7\u540e\u7f29\u8fdb\u91cf\u589e\u52a0\u7684\u8bed\u53e5\u5757\u3002\u4e00\u4e2a\u53d8\u91cf<code>x<\/code>\u73b0\u5728\u5f15\u7528\u4e0b\u4e00\u4e2a\u9879\u76ee\uff0c\u5e76\u91cd\u590d\u5faa\u73af\u7684\u4e3b\u4f53\uff0c\u76f4\u5230\u5e8f\u5217\u7ed3\u675f\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5e26\u6709\u5217\u8868\u5bf9\u8c61\u7684 <code>for<\/code>\u5faa\u73af\u3002<\/p>\n<p>Example: <\/p>\n<pre><code>nums = [10, 20, 30, 40, 50]\n\nfor i in nums:\n    print(i) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>10\n20\n30\n40\n50 <\/code><\/pre>\n<p>\u4e0b\u9762\u6f14\u793a\u4e86\u5e26\u6709\u5143\u7ec4\u5bf9\u8c61\u7684 <code>for<\/code>\u5faa\u73af\u3002<\/p>\n<p>Example: For Loop with Tuple <\/p>\n<pre><code>nums = (10, 20, 30, 40, 50)\nfor i in nums:\n    print(i) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>10\n20\n30\n40\n50 <\/code><\/pre>\n<p>\u4efb\u4f55 Python \u5e8f\u5217\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f7f\u7528 for \u8bed\u53e5\u8fdb\u884c\u8fed\u4ee3\u3002<\/p>\n<p>Example: For Loop with String <\/p>\n<pre><code>for char in 'Hello':\n    print (char) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>H\ne\nl\nl\no <\/code><\/pre>\n<p>\u4e0b\u9762\u7684<code>for<\/code>\u5faa\u73af\u4f7f\u7528\u9879()\u65b9\u6cd5\u904d\u5386\u5b57\u5178\u3002<\/p>\n<p>Example: For Loop with Dictionary <\/p>\n<pre><code>numNames = { 1:'One', 2: 'Two', 3: 'Three'}\n\nfor pair in numNames.items():\n    print(pair) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>(1, 'One')\n(2, 'Two')\n(3, 'Three')<\/code><\/pre>\n<p>\u952e\u503c paris \u53ef\u4ee5\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u89e3\u5305\u6210\u4e24\u4e2a\u53d8\u91cf\uff0c\u5206\u522b\u5f97\u5230\u952e\u503c\u3002<\/p>\n<p>Example: For Loop with Dictionary <\/p>\n<pre><code>numNames = { 1:'One', 2: 'Two', 3: 'Three'}\n\nfor k,v in numNames.items():\n    print(\"key = \", k , \", value =\", v) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>key = 1, value = One\nkey = 2, value = Two\nkey = 3, value = Three <\/code><\/pre>\n<h2>\u5bf9\u4e8e\u5e26\u8303\u56f4()\u51fd\u6570\u7684\u5faa\u73af<\/h2>\n<p><code>range<\/code>\u7c7b\u662f\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\u3002\u8303\u56f4()\u8fd4\u56de\u53ef\u4e0e<code>for<\/code>\u5faa\u73af\u4e00\u8d77\u4f7f\u7528\u7684<code>range<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>Example: <\/p>\n<pre><code>for i in range(5):\n    print(i) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>0\n1\n2\n3\n4 <\/code><\/pre>\n<h2>\u9000\u51fa <code>for<\/code>\u5faa\u73af<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u5173\u952e\u5b57\u505c\u6b62\u5e76\u9000\u51fa <code>for<\/code>\u5faa\u73af\u7684\u6267\u884c\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<p>Example: <\/p>\n<pre><code>for i in range(1, 5):\n    if i &gt; 2\n        break\n    print(i) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>1\n2 <\/code><\/pre>\n<h2>\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3<\/h2>\n<p>\u4f7f\u7528<code>continue<\/code>\u5173\u952e\u5b57\u8df3\u8fc7\u5f53\u524d\u6267\u884c\uff0c\u5e76\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u4f7f\u7528<code>continue<\/code>\u5173\u952e\u5b57\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<p>Example: <\/p>\n<pre><code>for i in range(1, 5):\n    if i &gt; 3\n        continue\n    print(i) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>1\n2\n3 <\/code><\/pre>\n<h2>\u5bf9\u4e8e\u5e26\u5176\u4ed6\u5757\u7684\u5faa\u73af<\/h2>\n<p><code>else<\/code>\u5757\u53ef\u4ee5\u8ddf\u968f<code>for<\/code>\u5faa\u73af\uff0c\u8be5\u5faa\u73af\u5c06\u5728<code>for<\/code>\u5faa\u73af\u7ed3\u675f\u65f6\u6267\u884c\u3002<\/p>\n<p>Example: <\/p>\n<pre><code>for i in range(2):\n    print(i)\nelse:\n     print('End of for loop') <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>0\n1\nEnd of for loop <\/code><\/pre>\n<h2>\u5faa\u73af\u5d4c\u5957<\/h2>\n<p>\u5982\u679c\u4e00\u4e2a\u5faa\u73af(<code>for<\/code>\u5faa\u73af\u6216 <code>while<\/code>\u5faa\u73af)\u5728\u5176\u4e3b\u4f53\u5757\u4e2d\u5305\u542b\u53e6\u4e00\u4e2a\u5faa\u73af\uff0c\u6211\u4eec\u8bf4\u8fd9\u4e24\u4e2a\u5faa\u73af\u662f\u5d4c\u5957\u7684\u3002\u5982\u679c\u5916\u5faa\u73af\u88ab\u8bbe\u8ba1\u4e3a\u6267\u884c m \u6b21\u8fed\u4ee3\uff0c\u800c\u5185\u5faa\u73af\u88ab\u8bbe\u8ba1\u4e3a\u6267\u884c n \u6b21\u91cd\u590d\uff0c\u90a3\u4e48\u5185\u5faa\u73af\u7684\u4e3b\u4f53\u5757\u5c06\u88ab\u6267\u884c m\u00d7n \u6b21\u3002<\/p>\n<p>Example: Nested for loop <\/p>\n<pre><code>for x in range(1,4):\n    for y in range(1,3):\n        print('x = ', x, ', y = ', y) <\/code><\/pre>\n<p>Output<\/p>\n<pre><code>x =  1, y =  1\nx =  1, y =  2\nx =  2, y =  1\nx =  2, y =  2\nx =  3, y =  1\nx =  3, y =  2 <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0cfor\u5173\u952e\u5b57\u63d0\u4f9b\u4e86\u66f4\u5168\u9762\u7684\u673a\u5236\u6765\u6784\u6210\u5faa\u73af\u3002 for\u5faa\u73af\u7528\u4e8e\u5e8f\u5217\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-183896","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/183896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=183896"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/183896\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=183896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=183896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=183896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}