{"id":305,"date":"2025-01-11T20:16:20","date_gmt":"2025-01-11T12:16:20","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=305"},"modified":"2025-01-11T20:16:20","modified_gmt":"2025-01-11T12:16:20","slug":"python-%e4%b8%ad%ef%bc%8c-for-%e5%be%aa%e7%8e%af%e5%92%8c-while-%e5%be%aa%e7%8e%af%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=305","title":{"rendered":"Python \u4e2d\uff0c for \u5faa\u73af\u548c while \u5faa\u73af\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u00a0for\u00a0\u5faa\u73af\u548c\u00a0while\u00a0\u5faa\u73af\u6709\u4ee5\u4e0b\u533a\u522b\uff1a<br> <br>\u4e00\u3001\u8bed\u6cd5\u548c\u57fa\u672c\u7528\u6cd5<br> <br>1.\u00a0\u00a0for\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002<br>&#8211; \u8bed\u6cd5\uff1a\u00a0for \u53d8\u91cf in \u53ef\u8fed\u4ee3\u5bf9\u8c61:&#8230;\u00a0\u3002<br>&#8211; \u4f8b\u5982\uff1a<br>for item in [1, 2, 3, 4]:<br>    print(item)<br>\u00a0<br>&#8211; \u8f93\u51fa\uff1a1\u30012\u30013\u30014\u3002<br>2.\u00a0\u00a0while\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u53ea\u8981\u7ed9\u5b9a\u7684\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u4e00\u76f4\u6267\u884c\u5faa\u73af\u4f53\u3002<br>&#8211; \u8bed\u6cd5\uff1a\u00a0while \u6761\u4ef6:&#8230;\u00a0\u3002<br>&#8211; \u4f8b\u5982\uff1a<br>count = 0<br>while count &lt; 5:<br>    print(count)<br>    count += 1<br>\u00a0<br>&#8211; \u8f93\u51fa\uff1a0\u30011\u30012\u30013\u30014\u3002<br> <br>\u4e8c\u3001\u5faa\u73af\u63a7\u5236\u65b9\u5f0f<br> <br>1.\u00a0\u00a0for\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u901a\u5e38\u4e0d\u9700\u8981\u624b\u52a8\u63a7\u5236\u5faa\u73af\u7684\u6b21\u6570\uff0c\u5b83\u4f1a\u81ea\u52a8\u904d\u5386\u5b8c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u540e\u7ed3\u675f\u3002<br>&#8211; \u53ef\u4ee5\u4f7f\u7528\u00a0break\u00a0\u8bed\u53e5\u63d0\u524d\u9000\u51fa\u5faa\u73af\uff0c\u4f7f\u7528\u00a0continue\u00a0\u8bed\u53e5\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<br>2.\u00a0\u00a0while\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u9700\u8981\u624b\u52a8\u63a7\u5236\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u51b3\u5b9a\u4f55\u65f6\u7ed3\u675f\u5faa\u73af\u3002<br>&#8211; \u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u00a0break\u00a0\u548c\u00a0continue\u00a0\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<br> <br>\u4e09\u3001\u9002\u7528\u573a\u666f<br> <br>1.\u00a0\u00a0for\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u5f53\u4f60\u660e\u786e\u77e5\u9053\u8981\u904d\u5386\u7684\u5bf9\u8c61\u7684\u957f\u5ea6\u6216\u8303\u56f4\u65f6\uff0c\u4f7f\u7528\u00a0for\u00a0\u5faa\u73af\u66f4\u52a0\u65b9\u4fbf\u3002<br>&#8211; \u4f8b\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u5df2\u77e5\u957f\u5ea6\u7684\u5217\u8868\u6216\u5bf9\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6570\u5b57\u8fdb\u884c\u64cd\u4f5c\u3002<br>2.\u00a0\u00a0while\u00a0\u5faa\u73af\uff1a<br> <br>&#8211; \u5f53\u4f60\u4e0d\u786e\u5b9a\u5faa\u73af\u7684\u6b21\u6570\uff0c\u800c\u662f\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u5faa\u73af\u65f6\uff0c\u9002\u5408\u4f7f\u7528\u00a0while\u00a0\u5faa\u73af\u3002<br>&#8211; \u4f8b\u5982\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u503c\uff0c\u6216\u8005\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u4e4b\u524d\u4e0d\u65ad\u91cd\u590d\u67d0\u4e2a\u64cd\u4f5c\u3002<br> <br>\u603b\u7684\u6765\u8bf4\uff0c\u00a0for\u00a0\u5faa\u73af\u548c\u00a0while\u00a0\u5faa\u73af\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u6709\u5404\u81ea\u7684\u4f18\u52bf\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u00a0for\u00a0\u5faa\u73af\u548c\u00a0while\u00a0\u5faa\u73af\u6709\u4ee5\u4e0b\u533a\u522b\uff1a \u4e00\u3001\u8bed\u6cd5\u548c\u57fa\u672c\u7528\u6cd5 1.\u00a0\u00a0for\u00a0\u5faa\u73af\uff1a &#8211; \u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002&#8211; \u8bed\u6cd5\uff1a\u00a0for \u53d8\u91cf in \u53ef\u8fed\u4ee3\u5bf9\u8c61:&#8230;\u00a0\u3002&#8211; \u4f8b\u5982\uff1afor item in [1, 2, 3, 4]: print(item)\u00a0&#8211; \u8f93\u51fa\uff1a1\u30012\u30013\u30014\u30022.\u00a0\u00a0while\u00a0\u5faa\u73af\uff1a &#8211; \u53ea\u8981\u7ed9\u5b9a\u7684\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u4e00\u76f4\u6267\u884c\u5faa\u73af\u4f53\u3002&#8211; \u8bed\u6cd5\uff1a\u00a0while \u6761\u4ef6:&#8230;\u00a0\u3002&#8211; \u4f8b\u5982\uff1acount = 0while count &lt; 5:&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=305"}],"version-history":[{"count":0,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}