{"id":1099940,"date":"2025-01-08T15:35:31","date_gmt":"2025-01-08T07:35:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1099940.html"},"modified":"2025-01-08T15:35:33","modified_gmt":"2025-01-08T07:35:33","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%80%e4%b8%aapython%e7%a8%8b%e5%ba%8f%e5%be%aa%e7%8e%af-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1099940.html","title":{"rendered":"\u5982\u4f55\u8ba9\u4e00\u4e2apython\u7a0b\u5e8f\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063419\/0176e17e-064a-43ea-9a47-88f4a74301e4.webp\" alt=\"\u5982\u4f55\u8ba9\u4e00\u4e2apython\u7a0b\u5e8f\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/strong>\u662f\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\u7684\u51e0\u79cd\u4e3b\u8981\u65b9\u6cd5\u3002<strong>\u4f7f\u7528<code>while<\/code>\u5faa\u73af<\/strong>\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u5e03\u5c14\u6761\u4ef6\uff0c\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\u5c31\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\uff0c\u4e0d\u6ee1\u8db3\u65f6\u5219\u9000\u51fa\u5faa\u73af\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5f88\u591a\u6709\u7528\u7684\u7a0b\u5e8f\u529f\u80fd\uff0c\u4f8b\u5982\u53cd\u590d\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u3001\u4e0d\u65ad\u68c0\u67e5\u67d0\u4e2a\u6761\u4ef6\u6216\u72b6\u6001\u7b49\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u5982\u4f55\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af<\/strong><\/h2>\n<p><p>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u5faa\u73af\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5faa\u73af\u5141\u8bb8\u6211\u4eec\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5b9e\u73b0Python\u7a0b\u5e8f\u5faa\u73af\u7684\u65b9\u6cd5\uff0c\u5305\u62ec<code>while<\/code>\u5faa\u73af\u3001<code>for<\/code>\u5faa\u73af\u4ee5\u53ca\u9012\u5f52\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>while<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><h3>1. \u4ec0\u4e48\u662f<code>while<\/code>\u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u662f\u4e00\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u5b83\u4f1a\u53cd\u590d\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6307\u5b9a\u7684\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u4ee3\u7801\u5757<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7b80\u5355\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>while<\/code>\u5faa\u73af\u793a\u4f8b\uff0c\u5b83\u4f1a\u6253\u5370\u6570\u5b570\u52304\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 5:<\/p>\n<p>    print(i)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>i<\/code>\u5c0f\u4e8e5\u65f6\uff0c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\uff0c<code>i<\/code>\u6bcf\u6b21\u589e\u52a01\u3002<\/p>\n<\/p>\n<p><h3>3. \u65e0\u9650\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4f8b\u5982\u670d\u52a1\u5668\u4e00\u76f4\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6ce8\u610f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u67d0\u79cd\u65b9\u5f0f\u6765\u6253\u7834\u8fd9\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u6c38\u8fdc\u8fd0\u884c\u4e0b\u53bb\u3002<\/p>\n<\/p>\n<p><h3>4. \u7ed3\u5408<code>break<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u7ec8\u6b62<code>while<\/code>\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while True:<\/p>\n<p>    if i &gt;= 5:<\/p>\n<p>        break<\/p>\n<p>    print(i)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>i<\/code>\u5927\u4e8e\u6216\u7b49\u4e8e5\u65f6\uff0c<code>break<\/code>\u8bed\u53e5\u5c06\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><h3>1. \u4ec0\u4e48\u662f<code>for<\/code>\u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>for<\/code>\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\uff09\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for element in iterable:<\/p>\n<p>    # \u4ee3\u7801\u5757<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7b80\u5355\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>for<\/code>\u5faa\u73af\u793a\u4f8b\uff0c\u5b83\u4f1a\u6253\u5370\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [0, 1, 2, 3, 4]<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528<code>range<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<code>range<\/code>\u51fd\u6570\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff1a<\/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><code>range(5)<\/code>\u751f\u6210\u4e00\u4e2a\u4ece0\u52304\u7684\u6574\u6570\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>4. \u5d4c\u5957<code>for<\/code>\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5d4c\u5957<code>for<\/code>\u5faa\u73af\u6765\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u4e8c\u7ef4\u5217\u8868\uff1a<\/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 item in row:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. \u4ec0\u4e48\u662f\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u5b83\u4f1a\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u5206\u6cbb\u95ee\u9898\u6216\u6811\u5f62\u7ed3\u6784\u7684\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h3>2. \u7b80\u5355\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u51fd\u6570\u793a\u4f8b\uff0c\u5b83\u8ba1\u7b97\u7ed9\u5b9a\u6574\u6570\u7684\u9636\u4e58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/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\u8c03\u7528\u81ea\u8eab\u6765\u8ba1\u7b97\u9636\u4e58\u3002<\/p>\n<\/p>\n<p><h3>3. \u63a7\u5236\u9012\u5f52\u6df1\u5ea6<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u9632\u6b62\u65e0\u9650\u9012\u5f52\u3002\u901a\u5e38\u5728\u51fd\u6570\u5185\u90e8\u901a\u8fc7\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u6765\u63a7\u5236\u9012\u5f52\u6df1\u5ea6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5<\/h2>\n<\/p>\n<p><h3>1. \u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u7ed3\u5408\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\u4e0d\u65ad\u8bf7\u6c42\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u6709\u6548\u7684\u6570\u636e\u4e3a\u6b62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>    if user_input.isdigit() and int(user_input) &gt; 0:<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u65e0\u6548\u8f93\u5165\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5faa\u73af\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\u5904\u7406\u5f02\u5e38\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6280\u5de7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p>        break<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u65e0\u6548\u8f93\u5165\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\uff0c\u786e\u4fdd\u5728\u7528\u6237\u8f93\u5165\u65e0\u6548\u6570\u636e\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f18\u5316\u5faa\u73af\u6027\u80fd<\/h2>\n<\/p>\n<p><h3>1. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u5728\u5faa\u73af\u4e2d\u6267\u884c\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u8bbf\u95ee\u5217\u8868\u7684\u957f\u5ea6\u53ef\u4ee5\u6539\u4e3a\u5728\u5faa\u73af\u5916\u90e8\u8ba1\u7b97\u4e00\u6b21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>length = len(my_list)<\/p>\n<p>for i in range(length):<\/p>\n<p>    print(my_list[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        yield i<\/p>\n<p>for value in my_generator():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u751f\u6210\u6570\u636e\uff0c\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u65f6\u4f1a\u4ece\u4e0a\u6b21\u4e2d\u65ad\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>3. \u5e76\u884c\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e76\u884c\u5904\u7406\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def task(n):<\/p>\n<p>    return n * n<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(task, range(10)))<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5e76\u884c\u6267\u884c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u603b\u8fd0\u884c\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\uff0c\u5305\u62ec\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3001<code>for<\/code>\u5faa\u73af\u548c\u9012\u5f52\u51fd\u6570\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u5982\u4f55\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u548c\u5f02\u5e38\u5904\u7406\u6765\u63a7\u5236\u5faa\u73af\uff0c\u4ee5\u53ca\u4f18\u5316\u5faa\u73af\u6027\u80fd\u7684\u65b9\u6cd5\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u9ad8\u6548\u3001\u5065\u58ee\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True:<\/code>\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u4e0d\u65ad\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\uff0c\u76f4\u5230\u7a0b\u5e8f\u88ab\u5916\u90e8\u5e72\u9884\uff08\u5982\u7528\u6237\u6309\u4e0bCtrl+C\uff09\u6216\u901a\u8fc7\u7279\u5b9a\u6761\u4ef6\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u9000\u51fa\u5faa\u73af\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    print(&quot;\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4e00\u76f4\u8fd0\u884c&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u8f7b\u677e\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4f7f\u7528<code>for<\/code>\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5217\u8868\u540d\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4]\nfor item in my_list:\n    print(item)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4f9d\u6b21\u6253\u5370\u51fa\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u6b21\u6570\uff1f<\/strong><br \/>\u8981\u63a7\u5236\u5faa\u73af\u6267\u884c\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u7ed3\u5408<code>range()<\/code>\u51fd\u6570\u3002<code>range()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7ec8\u6b62\u503c\u548c\u6b65\u957f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u4ece0\u52304\u7684\u6570\u5b57\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    print(i)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6267\u884c5\u6b21\u5faa\u73af\uff0c\u4f9d\u6b21\u8f93\u51fa0\u52304\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528while\u5faa\u73af\u3001\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u662f\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\u7684\u51e0\u79cd\u4e3b\u8981\u65b9\u6cd5\u3002\u4f7f\u7528while\u5faa\u73af [&hellip;]","protected":false},"author":3,"featured_media":1099949,"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\/1099940"}],"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=1099940"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099940\/revisions"}],"predecessor-version":[{"id":1099951,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099940\/revisions\/1099951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1099949"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1099940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1099940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1099940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}