{"id":1065748,"date":"2024-12-31T16:20:13","date_gmt":"2024-12-31T08:20:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065748.html"},"modified":"2024-12-31T16:20:16","modified_gmt":"2024-12-31T08:20:16","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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065748.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-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/07753c42-a04b-414e-988d-480074cb7845.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8ba9\u4e00\u4e2apython\u7a0b\u5e8f\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\u6267\u884c<\/strong>\u3002\u5728\u8fd9\u5176\u4e2d\uff0c<strong>while\u5faa\u73af<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>for\u5faa\u73af\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u7684\u5faa\u73af\u65b9\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u904d\u5386\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for variable in iterable:<\/p>\n<p>    # \u6267\u884c\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.1\u3001\u904d\u5386\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>for num in numbers:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>numbers<\/code>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4f1a\u88ab\u4f9d\u6b21\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u4f7f\u7528range\u51fd\u6570<\/h4>\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<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5e8f\u5217\uff0c\u4ece0\u5f00\u59cb\uff0c\u52305\u7ed3\u675f\uff08\u4e0d\u5305\u62ec5\uff09\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af\u4f1a\u6253\u5370\u51fa0\u52304\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>while\u5faa\u73af\u662f\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u7684\u5faa\u73af\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.1\u3001\u7b80\u5355\u7684while\u5faa\u73af<\/h4>\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\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230<code>i<\/code>\u7684\u503c\u4e0d\u518d\u5c0f\u4e8e5\u3002\u6bcf\u6b21\u5faa\u73af\uff0c<code>i<\/code>\u7684\u503c\u90fd\u4f1a\u52a01\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u65e0\u9650\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    print(&quot;This is an infinite loop&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4f1a\u4e00\u76f4\u6253\u5370&quot;This is an infinite loop&quot;\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u67d0\u79cd\u65b9\u5f0f\u6765\u6253\u7834\u8fd9\u4e2a\u5faa\u73af\uff0c\u6bd4\u5982\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u81ea\u5df1\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u5faa\u73af\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u57fa\u672c\u9012\u5f52\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    if n &lt;= 0:<\/p>\n<p>        print(&quot;Blast off!&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(n)<\/p>\n<p>        countdown(n-1)<\/p>\n<p>countdown(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>countdown<\/code>\u51fd\u6570\u4f1a\u4ece5\u5f00\u59cb\u5012\u6570\uff0c\u76f4\u5230\u6253\u5370&quot;Blast off!&quot;\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u9012\u5f52\u8ba1\u7b97\u9636\u4e58<\/h4>\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>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u8ba1\u7b97\u9636\u4e58\u3002<code>factorial(5)<\/code>\u7684\u7ed3\u679c\u662f120\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u5faa\u73af\u6765\u5b8c\u6210\u590d\u6742\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u5d4c\u5957\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(3):<\/p>\n<p>    for j in range(3):<\/p>\n<p>        print(f&quot;i={i}, j={j}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u7684\u6bcf\u4e2a\u7ec4\u5408\u90fd\u4f1a\u88ab\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u5faa\u73af\u4e2d\u4f7f\u7528\u6761\u4ef6\u5224\u65ad<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6]<\/p>\n<p>for num in numbers:<\/p>\n<p>    if num % 2 == 0:<\/p>\n<p>        print(f&quot;{num} is even&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;{num} is odd&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u6253\u5370\u6bcf\u4e2a\u6570\u5b57\u662f\u5947\u6570\u8fd8\u662f\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u662fPython\u4e2d\u66f4\u4e3a\u9ad8\u7ea7\u7684\u5faa\u73af\u63a7\u5236\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u5b9e\u73b0\u590d\u6742\u7684\u8fed\u4ee3\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def simple_generator():<\/p>\n<p>    yield 1<\/p>\n<p>    yield 2<\/p>\n<p>    yield 3<\/p>\n<p>gen = simple_generator()<\/p>\n<p>for value in gen:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u51fd\u6570<code>simple_generator<\/code>\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u4e00\u6b21\u4ea7\u751f\u4e00\u4e2a\u503c\u3002\u5faa\u73af\u4f1a\u4f9d\u6b21\u6253\u53701\u30012\u548c3\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyIterator:<\/p>\n<p>    def __init__(self, max):<\/p>\n<p>        self.max = max<\/p>\n<p>        self.current = 0<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.current &lt; self.max:<\/p>\n<p>            self.current += 1<\/p>\n<p>            return self.current<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p>it = MyIterator(5)<\/p>\n<p>for value in it:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>MyIterator<\/code>\u7c7b\u5b9e\u73b0\u4e86<code>__iter__<\/code>\u548c<code>__next__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u5faa\u73af\u4f1a\u6253\u53701\u52305\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u51fd\u6570\u548c\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u548c\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5\u5faa\u73af\u903b\u8f91\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5c01\u88c5\u5faa\u73af\u903b\u8f91<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def repeat_message(message, times):<\/p>\n<p>    for _ in range(times):<\/p>\n<p>        print(message)<\/p>\n<p>repeat_message(&quot;Hello, world!&quot;, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>repeat_message<\/code>\u51fd\u6570\u5c01\u88c5\u4e86\u4e00\u4e2afor\u5faa\u73af\uff0c\u7528\u4e8e\u91cd\u590d\u6253\u5370\u6307\u5b9a\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u4f7f\u7528\u95ed\u5305<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def make_repeater(message):<\/p>\n<p>    def repeater(times):<\/p>\n<p>        for _ in range(times):<\/p>\n<p>            print(message)<\/p>\n<p>    return repeater<\/p>\n<p>repeater = make_repeater(&quot;Hello, world!&quot;)<\/p>\n<p>repeater(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>make_repeater<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305\uff0c\u5c01\u88c5\u4e86\u4e00\u4e2a\u5faa\u73af\u903b\u8f91\u3002\u8c03\u7528<code>repeater(3)<\/code>\u4f1a\u6253\u5370\u4e09\u6b21&quot;Hello, world!&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5e76\u53d1\u548c\u5e76\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u6267\u884c\u590d\u6742\u8ba1\u7b97\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5e76\u53d1\u6216\u5e76\u884c\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>7.1\u3001\u4f7f\u7528\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<p>thread = threading.Thread(target=print_numbers)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6765\u5e76\u53d1\u6267\u884c<code>print_numbers<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>7.2\u3001\u4f7f\u7528\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<p>process = multiprocessing.Process(target=print_numbers)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u8fdb\u7a0b\u6765\u5e76\u884c\u6267\u884c<code>print_numbers<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5e93\u548c\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u5e93\u548c\u6846\u67b6\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5faa\u73af\u63a7\u5236\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u7f16\u5199\u548c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>8.1\u3001\u4f7f\u7528itertools\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>for value in itertools.cycle([1, 2, 3]):<\/p>\n<p>    print(value)<\/p>\n<p>    if value == 3:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>itertools.cycle<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4f9d\u6b21\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>8.2\u3001\u4f7f\u7528asyncio\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>asyncio.run(print_numbers())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>asyncio<\/code>\u5e93\u6765\u5f02\u6b65\u6267\u884c<code>print_numbers<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5faa\u73af\u65f6\uff0c\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u627e\u5230\u548c\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>9.1\u3001\u4f7f\u7528try-except\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    for i in range(5):<\/p>\n<p>        if i == 3:<\/p>\n<p>            raise ValueError(&quot;An error occurred&quot;)<\/p>\n<p>        print(i)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>9.2\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982pdb\uff09\u6765\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u503c\u548c\u7a0b\u5e8f\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb; pdb.set_trace()<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>pdb.set_trace()<\/code>\u4f1a\u542f\u52a8\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u5668\uff0c\u5141\u8bb8\u60a8\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6700\u4f73\u5b9e\u8df5\u548c\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5faa\u73af\u65f6\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u548c\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>10.1\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = []<\/p>\n<p>for i in range(1000):<\/p>\n<p>    results.append(i * i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5c06\u8ba1\u7b97\u7ed3\u679c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u90fd\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>10.2\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868<\/p>\n<p>numbers = [i for i in range(1000)]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/h2>\n<p>numbers = (i for i in range(1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u751f\u6210\u5668\u800c\u4e0d\u662f\u5217\u8868\uff0c\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<strong>\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u7b49\u65b9\u6cd5<\/strong>\uff0c\u60a8\u53ef\u4ee5\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\u6267\u884c\u3002\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u65b9\u5f0f\u548c\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u65e0\u8bba\u662f\u904d\u5386\u5217\u8868\u3001\u5b9e\u73b0\u590d\u6742\u903b\u8f91\uff0c\u8fd8\u662f\u5904\u7406\u5927\u91cf\u6570\u636e\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5e2e\u52a9\u60a8\u5b9e\u73b0\u5faa\u73af\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5faa\u73af\uff0c\u4f8b\u5982<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u4e0d\u65ad\u6267\u884c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af\nfor i in range(5):\n    print(i)\n\n# \u4f7f\u7528while\u5faa\u73af\ncount = 0\nwhile count &lt; 5:\n    print(count)\n    count += 1\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u63a7\u5236\u5faa\u73af\u7684\u7ed3\u675f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u5f53\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8df3\u51fa\u5faa\u73af\u3002\u76f8\u53cd\uff0c<code>continue<\/code>\u8bed\u53e5\u5219\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u90e8\u5206\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i == 5:\n        break  # \u5f53i\u7b49\u4e8e5\u65f6\u7ed3\u675f\u5faa\u73af\n    print(i)\n\nfor i in range(10):\n    if i % 2 == 0:\n        continue  # \u8df3\u8fc7\u5076\u6570\n    print(i)  # \u6253\u5370\u5947\u6570\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u5faa\u73af\u4e2d\u4f7f\u7528\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5c06\u5faa\u73af\u4e0e\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u5faa\u73af\u4e2d\u8c03\u7528\u5b83\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def process_number(num):\n    return num * 2\n\nfor i in range(5):\n    result = process_number(i)\n    print(result)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u5faa\u73af\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4e00\u4e2aPython\u7a0b\u5e8f\u5faa\u73af\u6267\u884c\u3002\u5728\u8fd9\u5176\u4e2d\uff0cwhile\u5faa [&hellip;]","protected":false},"author":3,"featured_media":1065761,"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\/1065748"}],"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=1065748"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065748\/revisions"}],"predecessor-version":[{"id":1065764,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065748\/revisions\/1065764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065761"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}