{"id":994917,"date":"2024-12-27T08:59:37","date_gmt":"2024-12-27T00:59:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994917.html"},"modified":"2024-12-27T08:59:40","modified_gmt":"2024-12-27T00:59:40","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994917.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071712\/077f2538-f7b7-471b-a563-054dda23a25c.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5faa\u73af\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5faa\u73af\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a<strong>for\u5faa\u73af<\/strong>\u548c<strong>while\u5faa\u73af<\/strong>\u3002<strong>for\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20<\/strong>\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff0c\u800c<strong>while\u5faa\u73af\u7528\u4e8e\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u5faa\u73af\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FOR\u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>For\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u96c6\u5408\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u7684\u573a\u666f<\/strong>\u3002Python\u7684for\u5faa\u73af\u7ed3\u6784\u975e\u5e38\u7b80\u6d01\uff0c\u901a\u5e38\u4e0e<code>range()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">for element in iterable:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>element<\/code>\uff1a\u6bcf\u6b21\u5faa\u73af\u4e2d\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u53d6\u51fa\u7684\u5143\u7d20\u3002<\/li>\n<li><code>iterable<\/code>\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u7b49\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528range()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>range()<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\uff0c\u5e38\u7528\u4e8e\u63a7\u5236\u5faa\u73af\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(start, stop, step):<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>start<\/code>\uff1a\u5e8f\u5217\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002<\/li>\n<li><code>stop<\/code>\uff1a\u5e8f\u5217\u7684\u7ec8\u6b62\u503c\uff08\u4e0d\u5305\u542b\uff09\u3002<\/li>\n<li><code>step<\/code>\uff1a\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a1\u3002<\/li>\n<\/ul>\n<p><h4>3. \u904d\u5386\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>for char in &quot;Python&quot;:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u904d\u5386\u53ef\u4ee5\u901a\u8fc7keys()\u3001values()\u6216items()\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;course&#39;: &#39;Computer Science&#39;}<\/p>\n<p>for key, value in student.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001WHILE\u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>While\u5faa\u73af\u7528\u4e8e\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u5224\u65ad\u6765\u63a7\u5236\u5faa\u73af\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>condition<\/code>\uff1a\u5faa\u73af\u7ee7\u7eed\u6267\u884c\u7684\u6761\u4ef6\uff0c\u8fd4\u56deTrue\u65f6\u5faa\u73af\u7ee7\u7eed\uff0cFalse\u65f6\u505c\u6b62\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f8b\u5b50\uff1a\u6c421\u5230100\u7684\u548c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">i = 1<\/p>\n<p>sum = 0<\/p>\n<p>while i &lt;= 100:<\/p>\n<p>    sum += i<\/p>\n<p>    i += 1<\/p>\n<p>print(sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528break\u548ccontinue<\/h4>\n<\/p>\n<ul>\n<li><code>break<\/code>\uff1a\u7528\u4e8e\u63d0\u524d\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li><code>continue<\/code>\uff1a\u7528\u4e8e\u8df3\u8fc7\u672c\u6b21\u5faa\u73af\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528break<\/p>\n<p>for i in range(10):<\/p>\n<p>    if i == 5:<\/p>\n<p>        break<\/p>\n<p>    print(i)<\/p>\n<h2><strong>\u4f7f\u7528continue<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5faa\u73af\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u5d4c\u5957\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u5373\u5728\u4e00\u4e2a\u5faa\u73af\u4f53\u5185\u518d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e58\u6cd5\u8868<\/p>\n<p>for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{i} * {j} = {i * j}&quot;, end=&quot;\\t&quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5faa\u73af\u7684\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5faa\u73af\u65f6\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\uff0c\u51cf\u5c11\u5faa\u73af\u4f53\u5185\u4e0d\u5fc5\u8981\u7684\u5de5\u4f5c\u91cf\u3002\u4f8b\u5982\uff0c\u5c06\u4e0d\u53d8\u7684\u8ba1\u7b97\u79fb\u51fa\u5faa\u73af\u4f53\u5916\uff0c\u6216\u5229\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5faa\u73af\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u6b7b\u5faa\u73af<\/strong>\uff1a\u5728\u4f7f\u7528while\u5faa\u73af\u65f6\uff0c\u9700\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u4f1a\u5728\u67d0\u4e2a\u65f6\u523b\u53d8\u4e3aFalse\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u3002<\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528break\u548ccontinue<\/strong>\uff1a\u867d\u7136break\u548ccontinue\u53ef\u4ee5\u6539\u53d8\u5faa\u73af\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u514d\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9Python\u4e2d\u5faa\u73af\u7684\u5b9a\u4e49\u4e0e\u4f7f\u7528\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u7f16\u5199\u9ad8\u6548\u7684\u5faa\u73af\u8bed\u53e5\u3002\u5faa\u73af\u662f\u7f16\u7a0b\u4e2d\u6700\u57fa\u672c\u548c\u91cd\u8981\u7684\u7ed3\u6784\u4e4b\u4e00\uff0c\u719f\u7ec3\u638c\u63e1\u5b83\u4eec\u5c06\u6781\u5927\u5730\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u80fd\u529b\u548c\u4ee3\u7801\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u7c7b\u578b\u7684\u5faa\u73af\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>Python\u4e3b\u8981\u6709\u4e24\u79cd\u5faa\u73af\u7ed3\u6784\uff1a<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\uff09\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u5728\u7ed9\u5b9a\u6761\u4ef6\u4e3a\u771f\u7684\u60c5\u51b5\u4e0b\u6301\u7eed\u6267\u884c\u3002\u901a\u8fc7\u8fd9\u4e24\u79cd\u5faa\u73af\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u63a7\u5236\u6d41\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7684\u5faa\u73af\u4e2d\u4f7f\u7528break\u548ccontinue\u8bed\u53e5\uff1f<\/strong><br \/><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u8bed\u53e5\u5219\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u5f00\u59cb\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a<code>for<\/code>\u5faa\u73af\u4e2d\uff0c\u5982\u679c\u9047\u5230\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u9000\u51fa\u5faa\u73af\uff0c\u800c\u4f7f\u7528<code>continue<\/code>\u5219\u53ef\u4ee5\u8df3\u8fc7\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\uff0c\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684\u5faa\u73af\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u5904\u7406\u5217\u8868\u6216\u5b57\u5178\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u76f4\u63a5\u904d\u5386\u5217\u8868\u6216\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002\u5728\u5904\u7406\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fed\u4ee3\u5176\u5143\u7d20\uff1b\u5728\u5904\u7406\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u8fed\u4ee3\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5bf9\u96c6\u5408\u6570\u636e\u7684\u64cd\u4f5c\u53d8\u5f97\u7b80\u6d01\u660e\u4e86\uff0c\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u4e0e\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5faa\u73af\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1afor\u5faa\u73af\u548cwhile\u5faa\u73af\u3002for\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":994931,"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\/994917"}],"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=994917"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994917\/revisions"}],"predecessor-version":[{"id":994937,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994917\/revisions\/994937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994931"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}