{"id":1011268,"date":"2024-12-27T11:28:31","date_gmt":"2024-12-27T03:28:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011268.html"},"modified":"2024-12-27T11:28:33","modified_gmt":"2024-12-27T03:28:33","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e6%89%a7%e8%a1%8c%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011268.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73af\u6267\u884c\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085424\/f83f0d36-9e39-4b09-a16f-e899906c882a.webp\" alt=\"python\u5982\u4f55\u5faa\u73af\u6267\u884c\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u5faa\u73af\u6267\u884c\u4ee3\u7801\u3002while\u5faa\u73af\u662f\u57fa\u4e8e\u6761\u4ef6\u7684\u5faa\u73af\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6301\u7eed\u8fd0\u884c\u76f4\u5230\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u7684\u60c5\u51b5\uff1bfor\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff1b\u9012\u5f52\u51fd\u6570\u5219\u662f\u901a\u8fc7\u51fd\u6570\u81ea\u8eab\u8c03\u7528\u6765\u5b9e\u73b0\u5faa\u73af\u3002while\u5faa\u73af\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u800c\u7075\u6d3b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>For\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5faa\u73af\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u904d\u5386\u5df2\u77e5\u957f\u5ea6\u7684\u5e8f\u5217\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u6216\u8303\u56f4\u5bf9\u8c61\u3002\u5b83\u901a\u8fc7\u9010\u4e2a\u8bbf\u95ee\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>For\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>for variable in sequence:<\/code>\uff0c\u5176\u4e2d<code>variable<\/code>\u662f\u5faa\u73af\u53d8\u91cf\uff0c<code>sequence<\/code>\u662f\u8981\u904d\u5386\u7684\u5e8f\u5217\u3002\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u5bf9\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u4e00\u6b21\u3002<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>fruits<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0cfor\u5faa\u73af\u9010\u4e2a\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528range()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>range()<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u662ffor\u5faa\u73af\u4e2d\u5e38\u7528\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ece\u8d77\u59cb\u503c\u5230\u7ed3\u675f\u503c\uff08\u4e0d\u5305\u62ec\u7ed3\u675f\u503c\uff09\u7684\u6570\u5b57\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u4f1a\u6253\u5370\u51fa0\u52304\u7684\u6570\u5b57\uff0c\u56e0\u4e3a<code>range(5)<\/code>\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u52304\u7684\u5e8f\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5d4c\u5957FOR\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>For\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u5373\u5728\u4e00\u4e2afor\u5faa\u73af\u5185\u518d\u4f7f\u7528\u53e6\u4e00\u4e2afor\u5faa\u73af\u3002\u8fd9\u5728\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42for\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5185\u5c42for\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001WHILE\u5faa\u73af<\/p>\n<\/p>\n<p><p>While\u5faa\u73af\u7528\u4e8e\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u5b83\u7279\u522b\u9002\u5408\u4e8e\u9700\u8981\u6839\u636e\u52a8\u6001\u6761\u4ef6\u51b3\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>While\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>while condition:<\/code>\uff0c\u5176\u4e2d<code>condition<\/code>\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7ed3\u679c\u4e3a\u771f\u65f6\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code>\u4ece0\u5f00\u59cb\uff0c\u6bcf\u6b21\u5faa\u73af\u589e\u52a01\uff0c\u76f4\u5230<code>count<\/code>\u7b49\u4e8e5\u65f6\u505c\u6b62\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e0\u9650\u5faa\u73af\u4e0e\u9000\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6761\u4ef6\u59cb\u7ec8\u4e3a\u771f\uff0cwhile\u5faa\u73af\u4f1a\u6210\u4e3a\u65e0\u9650\u5faa\u73af\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u4e2d\u65ad\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;Enter &#39;exit&#39; to quit: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5faa\u73af\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165&#39;exit&#39;\u65f6\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528continue\u8df3\u8fc7\u5f53\u524d\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u90e8\u5206\uff0c\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    count += 1<\/p>\n<p>    if count == 3:<\/p>\n<p>        continue<\/p>\n<p>    print(count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>count<\/code>\u7b49\u4e8e3\u65f6\uff0c<code>continue<\/code>\u8bed\u53e5\u4f1a\u8df3\u8fc7\u6253\u5370\u6b65\u9aa4\uff0c\u76f4\u63a5\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u9012\u5f52\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u6280\u672f\uff0c\u9002\u5408\u7528\u4e8e\u89e3\u51b3\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u4e3a\u7c7b\u4f3c\u5b50\u95ee\u9898\u7684\u60c5\u51b5\u3002\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u6709\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u9012\u5f52\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u7684\u57fa\u672c\u7ed3\u6784\u662f\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u8c03\u7528\u81ea\u8eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>print(factorial(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>factorial<\/code>\u51fd\u6570\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\u3002\u7ec8\u6b62\u6761\u4ef6\u662f<code>n == 1<\/code>\uff0c\u5426\u5219\u51fd\u6570\u4f1a\u8c03\u7528\u81ea\u8eab\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9012\u5f52\u4e0e\u8fed\u4ee3\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u7b80\u6d01\uff0c\u4f46\u53ef\u80fd\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002\u8fed\u4ee3\u5219\u901a\u5e38\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u5360\u7528\u989d\u5916\u7684\u51fd\u6570\u8c03\u7528\u6808\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def iterative_factorial(n):<\/p>\n<p>    result = 1<\/p>\n<p>    for i in range(1, n + 1):<\/p>\n<p>        result *= i<\/p>\n<p>    return result<\/p>\n<p>print(iterative_factorial(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u5b9e\u73b0\u4e0e\u9012\u5f52\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c3e\u9012\u5f52\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u4e9b\u7f16\u7a0b\u8bed\u8a00\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u8fd9\u79cd\u6280\u672f\u5141\u8bb8\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f18\u5316\u9012\u5f52\u8c03\u7528\uff0c\u4ee5\u907f\u514d\u6808\u6ea2\u51fa\u3002Python\u76ee\u524d\u4e0d\u652f\u6301\u8fd9\u79cd\u4f18\u5316\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u9012\u5f52\u7b97\u6cd5\u65f6\u9700\u8981\u5c0f\u5fc3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_recursive_factorial(n, accumulator=1):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return accumulator<\/p>\n<p>    else:<\/p>\n<p>        return tail_recursive_factorial(n - 1, n * accumulator)<\/p>\n<p>print(tail_recursive_factorial(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5c3e\u9012\u5f52\u4f8b\u5b50\u4e2d\uff0c<code>accumulator<\/code>\u53c2\u6570\u7528\u4e8e\u7d2f\u79ef\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u4f7f\u7528\u989d\u5916\u7684\u6808\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5faa\u73af\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5faa\u73af\u7c7b\u578b\u65f6\uff0c\u5e94\u6839\u636e\u95ee\u9898\u7684\u5177\u4f53\u9700\u6c42\u6765\u51b3\u5b9a\u3002for\u5faa\u73af\u9002\u7528\u4e8e\u5df2\u77e5\u957f\u5ea6\u7684\u5e8f\u5217\uff0c\u800cwhile\u5faa\u73af\u5219\u9002\u7528\u4e8e\u57fa\u4e8e\u6761\u4ef6\u7684\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u65e0\u9650\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528while\u5faa\u73af\u65f6\uff0c\u786e\u4fdd\u6761\u4ef6\u80fd\u591f\u5728\u67d0\u4e2a\u65f6\u523b\u53d8\u4e3a\u5047\uff0c\u4ee5\u907f\u514d\u610f\u5916\u7684\u65e0\u9650\u5faa\u73af\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728\u5faa\u73af\u5185\u8c03\u6574\u6761\u4ef6\u53d8\u91cf\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528break\u548ccontinue<\/strong><\/p>\n<\/p>\n<p><p><code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\u53ef\u4ee5\u4f7f\u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u514d\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u901a\u5e38\uff0c\u53ea\u5728\u9700\u8981\u8df3\u51fa\u5faa\u73af\u6216\u8df3\u8fc7\u67d0\u6b21\u8fed\u4ee3\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9012\u5f52\u51fd\u6570\u7684\u7ec8\u6b62\u6761\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u6709\u660e\u786e\u7684\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u9012\u5f52\u548c\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u5faa\u73af\u6027\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u5408\uff0c\u4f18\u5316\u5faa\u73af\u6027\u80fd\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5c3d\u91cf\u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u8ba1\u7b97\uff0c\u6216\u8005\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u751f\u6210\u5668\u6765\u60f0\u6027\u8ba1\u7b97\u5e8f\u5217\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5faa\u73af\u7684\u5b9e\u9645\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u5982\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u7279\u5b9a\u64cd\u4f5c\uff0c\u6216\u8005\u4ece\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_numbers = [x  2 for x in numbers]<\/p>\n<p>print(squared_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7528\u6237\u8f93\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u5e38\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u591a\u6b21\u8be2\u95ee\u7528\u6237\u76f4\u5230\u83b7\u5f97\u6709\u6548\u8f93\u5165\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    age = input(&quot;Please enter your age: &quot;)<\/p>\n<p>    if age.isdigit():<\/p>\n<p>        print(&quot;Thank you!&quot;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Invalid input. Please enter a number.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cwhile\u5faa\u73af\u4f1a\u6301\u7eed\u8fd0\u884c\u76f4\u5230\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b97\u6cd5\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u7b97\u6cd5\u4f9d\u8d56\u4e8e\u5faa\u73af\u6765\u91cd\u590d\u6267\u884c\u67d0\u4e9b\u6b65\u9aa4\uff0c\u5982\u6392\u5e8f\u7b97\u6cd5\u3001\u641c\u7d22\u7b97\u6cd5\u6216\u56fe\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bubble_sort(arr):<\/p>\n<p>    n = len(arr)<\/p>\n<p>    for i in range(n):<\/p>\n<p>        for j in range(0, n-i-1):<\/p>\n<p>            if arr[j] &gt; arr[j+1]:<\/p>\n<p>                arr[j], arr[j+1] = arr[j+1], arr[j]<\/p>\n<p>    return arr<\/p>\n<p>print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53cc\u5c42for\u5faa\u73af\u5b9e\u73b0\u4e86\u5192\u6ce1\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001Python\u4e2d\u7684\u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>break\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7ec8\u6b62\u5f53\u524d\u5faa\u73af\u3002\u5b83\u901a\u5e38\u4e0e\u6761\u4ef6\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for num in range(10):<\/p>\n<p>    if num == 5:<\/p>\n<p>        break<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>num<\/code>\u7b49\u4e8e5\u65f6\uff0c<code>break<\/code>\u8bed\u53e5\u4f1a\u7ec8\u6b62for\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>continue\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u90e8\u5206\uff0c\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u8df3\u8fc7\u4e0d\u5fc5\u8981\u7684\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for num in range(10):<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>continue<\/code>\u8bed\u53e5\u8df3\u8fc7\u4e86\u6240\u6709\u5076\u6570\uff0c\u53ea\u6253\u5370\u51fa\u5947\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>pass\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p><code>pass<\/code>\u8bed\u53e5\u662f\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u7528\u4e8e\u5360\u4f4d\u7b26\u3002\u5728\u9700\u8981\u4e00\u4e2a\u8bed\u53e5\u4f46\u4e0d\u60f3\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for num in range(10):<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        pass<\/p>\n<p>    else:<\/p>\n<p>        print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>pass<\/code>\u8bed\u53e5\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5faa\u73af\u7684\u9ad8\u7ea7\u6280\u5de7<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u7528\u4e8e\u66ff\u4ee3\u7b80\u5355\u7684for\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_numbers = [x  2 for x in numbers]<\/p>\n<p>print(squared_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u5b57\u5e73\u65b9\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5217\u8868\u3002\u751f\u6210\u5668\u5bf9\u8c61\u60f0\u6027\u8ba1\u7b97\u5143\u7d20\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = (x  2 for x in range(1000000))<\/p>\n<p>print(next(numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u7528\u4e8e\u8ba1\u7b97\u6570\u5b57\u5e73\u65b9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u679a\u4e3e\uff08enumerate\uff09<\/strong><\/p>\n<\/p>\n<p><p><code>enumerate()<\/code>\u51fd\u6570\u7528\u4e8e\u5728for\u5faa\u73af\u4e2d\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u5143\u7d20\u548c\u7d22\u5f15\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for index, fruit in enumerate(fruits):<\/p>\n<p>    print(index, fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5faa\u73af\u7684\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9057\u6f0f\u7ec8\u6b62\u6761\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728while\u5faa\u73af\u4e2d\uff0c\u9057\u6f0f\u7ec8\u6b62\u6761\u4ef6\u4f1a\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\u3002\u5e94\u786e\u4fdd\u5728\u5faa\u73af\u4f53\u5185\u6709\u903b\u8f91\u53ef\u4ee5\u4f7f\u6761\u4ef6\u53d8\u4e3a\u5047\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code>\u5728\u5faa\u73af\u4f53\u5185\u589e\u52a0\uff0c\u786e\u4fdd\u6700\u7ec8\u6761\u4ef6\u4e3a\u5047\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5fd8\u8bb0\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728while\u5faa\u73af\u4e2d\uff0c\u5fd8\u8bb0\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\u4f1a\u5bfc\u81f4\u5faa\u73af\u6c38\u8fdc\u65e0\u6cd5\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code>\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u66f4\u65b0\uff0c\u786e\u4fdd\u6700\u7ec8\u6761\u4ef6\u4e3a\u5047\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u7684\u5faa\u73af\u6761\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u9519\u8bef\u7684\u5faa\u73af\u6761\u4ef6\u4f1a\u5bfc\u81f4\u5faa\u73af\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002\u5728while\u5faa\u73af\u4e2d\uff0c\u5e94\u786e\u4fdd\u6761\u4ef6\u80fd\u6b63\u786e\u53cd\u6620\u5faa\u73af\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6<code>count &lt; 5<\/code>\u4fdd\u8bc1\u4e86\u5faa\u73af\u6b63\u786e\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u590d\u6267\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u5982<code>for<\/code>\u548c<code>while<\/code>\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u590d\u6267\u884c\u3002\u4f8b\u5982\uff0c<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5217\u8868\u6216\u8303\u56f4\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u6839\u636e\u6761\u4ef6\u6267\u884c\u4ee3\u7801\u5757\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u7075\u6d3b\u63a7\u5236\u6267\u884c\u6b21\u6570\u548c\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u4ee3\u7801\u6267\u884c\u7684\u6b21\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>range()<\/code>\u51fd\u6570\u7ed3\u5408<code>for<\/code>\u5faa\u73af\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>for i in range(5):<\/code>\u5c06\u4f1a\u6267\u884c\u5faa\u73af\u4f535\u6b21\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u9700\u8981\u91cd\u590d\u6267\u884c\u7279\u5b9a\u6b21\u6570\u7684\u573a\u666f\uff0c\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u6279\u91cf\u5904\u7406\u6216\u91cd\u590d\u8ba1\u7b97\u7684\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True:<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u8fd9\u4e2a\u7ed3\u6784\u4f1a\u6301\u7eed\u6267\u884c\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u88ab\u5916\u90e8\u6761\u4ef6\u6253\u65ad\u6216\u624b\u52a8\u7ec8\u6b62\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u65f6\u4e00\u5b9a\u8981\u786e\u4fdd\u6709\u5408\u7406\u7684\u9000\u51fa\u673a\u5236\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u5361\u6b7b\u6216\u5360\u7528\u8fc7\u591a\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u5faa\u73af\u6267\u884c\u4ee3\u7801\u3002while\u5faa\u73af\u662f\u57fa\u4e8e\u6761\u4ef6\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1011277,"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\/1011268"}],"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=1011268"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011268\/revisions"}],"predecessor-version":[{"id":1011281,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011268\/revisions\/1011281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011277"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}