{"id":1161513,"date":"2025-01-13T19:17:19","date_gmt":"2025-01-13T11:17:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161513.html"},"modified":"2025-01-13T19:17:21","modified_gmt":"2025-01-13T11:17:21","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e8%bf%90%e8%a1%8c10%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161513.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u8fd0\u884c10\u6b21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202740\/85844db5-c5b9-451b-b1cf-4ac9f8e183ba.webp\" alt=\"\u5982\u4f55\u8ba9python\u8fd0\u884c10\u6b21\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u6784\u5efa\u51fd\u6570\u3001\u4f7f\u7528\u6a21\u5757\u5316\u7f16\u7a0b\uff0c\u53ef\u4ee5\u8ba9Python\u8fd0\u884c10\u6b21\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5faa\u73af\u7ed3\u6784<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u6216while\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\u8ba9Python\u8fd0\u884c10\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5faa\u73af\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u6784\u662f\u7f16\u7a0b\u4e2d\u5e38\u7528\u7684\u63a7\u5236\u7ed3\u6784\u4e4b\u4e00\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u591a\u6b21\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u3002Python\u652f\u6301for\u5faa\u73af\u548cwhile\u5faa\u73af\u4e24\u79cd\u4e3b\u8981\u7684\u5faa\u73af\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001for \u5faa\u73af<\/h4>\n<\/p>\n<p><p>for\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\uff0c\u5e76\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    print(f&quot;\u8fd9\u662f\u7b2c {i+1} \u6b21\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>range(10)<\/code>\u751f\u6210\u4e00\u4e2a\u4ece0\u52309\u7684\u5e8f\u5217\uff0c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5c06\u6267\u884c10\u6b21\u3002\u6bcf\u6b21\u5faa\u73af\u65f6\uff0c\u53d8\u91cf<code>i<\/code>\u7684\u503c\u4f9d\u6b21\u4e3a0\u52309\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u8fc7<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u7684\u5185\u5bb9\u5c06\u4f9d\u6b21\u663e\u793a\u201c\u8fd9\u662f\u7b2c1\u6b21\u8fd0\u884c\u201d\u5230\u201c\u8fd9\u662f\u7b2c10\u6b21\u8fd0\u884c\u201d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001while \u5faa\u73af<\/h4>\n<\/p>\n<p><p>while\u5faa\u73af\u5728\u7ed9\u5b9a\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u3002\u5f53\u6761\u4ef6\u53d8\u4e3a\u5047\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 10:<\/p>\n<p>    print(f&quot;\u8fd9\u662f\u7b2c {count+1} \u6b21\u8fd0\u884c&quot;)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf<code>count<\/code>\u521d\u59cb\u503c\u4e3a0\uff0c\u6bcf\u6b21\u5faa\u73af\u65f6\u8f93\u51fa\u201c\u8fd9\u662f\u7b2ccount+1\u6b21\u8fd0\u884c\u201d\uff0c\u5e76\u5c06<code>count<\/code>\u7684\u503c\u52a01\u3002\u5f53<code>count<\/code>\u7684\u503c\u8fbe\u523010\u65f6\uff0c\u6761\u4ef6<code>count &lt; 10<\/code>\u53d8\u4e3a\u5047\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u9700\u8981\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801\u653e\u5165\u51fd\u6570\u4e2d\uff0c\u7136\u540e\u8c03\u7528\u8be5\u51fd\u657010\u6b21\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0Python\u8fd0\u884c10\u6b21\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5305\u542b\u6211\u4eec\u5e0c\u671b\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u8c03\u7528\u8be5\u51fd\u657010\u6b21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>my_function<\/code>\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u5c06\u88ab\u8c03\u7528\u548c\u6267\u884c10\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u5757\u5316\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u5316\u7f16\u7a0b\u662f\u4e00\u79cd\u901a\u8fc7\u5c06\u4ee3\u7801\u5206\u5272\u6210\u591a\u4e2a\u6a21\u5757\u6765\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u7684\u7f16\u7a0b\u65b9\u6cd5\u3002\u901a\u8fc7\u5b9a\u4e49\u6a21\u5757\u5e76\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u6587\u4ef6<code>my_module.py<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u6211\u4eec\u5e0c\u671b\u91cd\u590d\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<p>for i in range(10):<\/p>\n<p>    my_module.my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u4e0d\u4ec5\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u8fd8\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7ebf\u7a0b\uff08Threading\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5e76\u884c\u8fd0\u884c\u67d0\u6bb5\u4ee3\u780110\u6b21\u3002Python\u7684<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7ebf\u7a0b\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5305\u542b\u6211\u4eec\u5e0c\u671b\u5e76\u884c\u8fd0\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def my_thread_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5e76\u884c\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u5e76\u542f\u52a810\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">threads = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    thread = threading.Thread(target=my_thread_function)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u5e76\u884c\u8fd0\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u8fdb\u7a0b\uff08Multiprocessing\uff09<\/h3>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0cPython\u7684<code>multiprocessing<\/code>\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u5e76\u884c\u8fd0\u884c\u67d0\u6bb5\u4ee3\u7801\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\u7684\u662f\uff0c\u8fdb\u7a0b\u5177\u6709\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u9700\u8981\u9694\u79bb\u5185\u5b58\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u8fdb\u7a0b\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u8fdb\u7a0b\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5305\u542b\u6211\u4eec\u5e0c\u671b\u5e76\u884c\u8fd0\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Process<\/p>\n<p>def my_process_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5e76\u884c\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u542f\u52a8\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u5e76\u542f\u52a810\u4e2a\u8fdb\u7a0b\u6765\u6267\u884c\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">processes = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    process = Process(target=my_process_function)<\/p>\n<p>    processes.append(process)<\/p>\n<p>    process.start()<\/p>\n<p>for process in processes:<\/p>\n<p>    process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u5e76\u884c\u8fd0\u884c\uff0c\u5e76\u4e14\u8fdb\u7a0b\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u63d0\u9ad8\u4e86\u9694\u79bb\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u534f\u7a0b\uff08Asyncio\uff09<\/h3>\n<\/p>\n<p><p>Python\u7684<code>asyncio<\/code>\u6a21\u5757\u652f\u6301\u5f02\u6b65\u7f16\u7a0b\uff0c\u901a\u8fc7\u534f\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u534f\u7a0b\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u534f\u7a0b\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5305\u542b\u6211\u4eec\u5e0c\u671b\u5e76\u53d1\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def my_coroutine_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5e76\u53d1\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u8fd0\u884c\u534f\u7a0b<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u5e76\u8fd0\u884c10\u4e2a\u534f\u7a0b\u6765\u6267\u884c\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    tasks = [my_coroutine_function() for _ in range(10)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u5e76\u53d1\u8fd0\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u51fd\u6570\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5b9e\u73b0\u51fd\u6570\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u8be5\u88c5\u9970\u5668\u7528\u4e8e\u591a\u6b21\u8fd0\u884c\u88ab\u88c5\u9970\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def repeat(n):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            for _ in range(n):<\/p>\n<p>                func(*args, kwargs)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u88c5\u9970\u5668\u6765\u88c5\u9970\u6211\u4eec\u5e0c\u671b\u591a\u6b21\u8fd0\u884c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@repeat(10)<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u51fd\u6570\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u5728\u4e0b\u6b21\u8c03\u7528\u65f6\u7ee7\u7eed\u6267\u884c\u3002\u901a\u8fc7\u521b\u5efa\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u8be5\u751f\u6210\u5668\u6bcf\u6b21\u751f\u6210\u4e00\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    for _ in range(10):<\/p>\n<p>        yield &quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u751f\u6210\u5668\u6765\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for value in my_generator():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u751f\u6210\u5668\u5177\u6709\u60f0\u6027\u8ba1\u7b97\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7136\u540e\u901a\u8fc7\u521b\u5efa\u5bf9\u8c61\u6765\u8c03\u7528\u8be5\u65b9\u6cd5\u591a\u6b21\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def my_method(self):<\/p>\n<p>        print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5bf9\u8c61\u5e76\u8c03\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u8c03\u7528\u8be5\u65b9\u6cd510\u6b21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">obj = MyClass()<\/p>\n<p>for _ in range(10):<\/p>\n<p>    obj.my_method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u5177\u6709\u5c01\u88c5\u6027\u548c\u53ef\u91cd\u7528\u6027\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;) for _ in range(10)]<\/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\u542b10\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>print<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff08\u5373<code>None<\/code>\uff09\u3002\u867d\u7136\u751f\u6210\u7684\u5217\u8868\u6ca1\u6709\u5b9e\u9645\u7528\u9014\uff0c\u4f46<code>print<\/code>\u51fd\u6570\u7684\u4ee3\u7801\u88ab\u6267\u884c\u4e8610\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u7f16\u7a0b\u6280\u5de7\uff0c\u901a\u8fc7\u9012\u5f52\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u9012\u5f52\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u8c03\u7528\u81ea\u8eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_function(n):<\/p>\n<p>    if n &gt; 0:<\/p>\n<p>        print(f&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c {10 - n + 1} \u6b21&quot;)<\/p>\n<p>        recursive_function(n - 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8c03\u7528\u9012\u5f52\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u8c03\u7528\u9012\u5f52\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u4e00\u4e2a\u521d\u59cb\u503c10\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">recursive_function(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u9012\u5f52\u51fd\u6570\u5177\u6709\u7b80\u6d01\u548c\u4f18\u96c5\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u88c5\u9970\u5668\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u88c5\u9970\u5668\u548c\u9012\u5f52\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u591a\u6b21\u8fd0\u884c\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u88c5\u9970\u5668\u548c\u9012\u5f52\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u8be5\u88c5\u9970\u5668\u7528\u4e8e\u591a\u6b21\u8fd0\u884c\u88ab\u88c5\u9970\u7684\u9012\u5f52\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def repeat_decorator(n):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            if n &gt; 0:<\/p>\n<p>                func(*args, kwargs)<\/p>\n<p>                wrapper(*args, kwargs)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u88c5\u9970\u5668\u9012\u5f52<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u88c5\u9970\u5668\u6765\u88c5\u9970\u9012\u5f52\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@repeat_decorator(10)<\/p>\n<p>def my_recursive_function():<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p>my_recursive_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u9012\u5f52\u51fd\u6570\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u679a\u4e3e<\/h3>\n<\/p>\n<p><p>\u679a\u4e3e\u662f\u4e00\u79cd\u521b\u5efa\u4e00\u7ec4\u547d\u540d\u5e38\u91cf\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u679a\u4e3e\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u679a\u4e3e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u8be5\u679a\u4e3e\u7c7b\u5305\u542b\u6211\u4eec\u5e0c\u671b\u591a\u6b21\u8fd0\u884c\u7684\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from enum import Enum<\/p>\n<p>class RunTimes(Enum):<\/p>\n<p>    RUN_1 = 1<\/p>\n<p>    RUN_2 = 2<\/p>\n<p>    RUN_3 = 3<\/p>\n<p>    RUN_4 = 4<\/p>\n<p>    RUN_5 = 5<\/p>\n<p>    RUN_6 = 6<\/p>\n<p>    RUN_7 = 7<\/p>\n<p>    RUN_8 = 8<\/p>\n<p>    RUN_9 = 9<\/p>\n<p>    RUN_10 = 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u679a\u4e3e<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u679a\u4e3e\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for run in RunTimes:<\/p>\n<p>    print(f&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c {run.value} \u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u679a\u4e3e\u5177\u6709\u660e\u786e\u548c\u6613\u8bfb\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u552f\u4e00\u7684\u96c6\u5408\uff0c\u901a\u8fc7\u96c6\u5408\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {i for i in range(10)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u96c6\u5408\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for _ in my_set:<\/p>\n<p>    print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u96c6\u5408\u5177\u6709\u65e0\u5e8f\u548c\u552f\u4e00\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u901a\u8fc7\u5b57\u5178\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {i: f&quot;\u7b2c{i+1}\u6b21\u8fd0\u884c&quot; for i in range(10)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u5b57\u5178\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u5b57\u5178\u5177\u6709\u952e\u503c\u5bf9\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u961f\u5217<\/h3>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u961f\u5217\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u961f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from queue import Queue<\/p>\n<p>my_queue = Queue()<\/p>\n<p>for i in range(10):<\/p>\n<p>    my_queue.put(f&quot;\u7b2c{i+1}\u6b21\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u961f\u5217\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while not my_queue.empty():<\/p>\n<p>    print(my_queue.get())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u961f\u5217\u5177\u6709\u5148\u8fdb\u5148\u51fa\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u5806\u6808<\/h3>\n<\/p>\n<p><p>\u5806\u6808\u662f\u4e00\u79cd\u540e\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u5806\u6808\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5806\u6808<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u5806\u6808\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_stack = [f&quot;\u7b2c{i+1}\u6b21\u8fd0\u884c&quot; for i in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5806\u6808<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u5806\u6808\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while my_stack:<\/p>\n<p>    print(my_stack.pop())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u5806\u6808\u5177\u6709\u540e\u8fdb\u5148\u51fa\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u4f7f\u7528\u53cc\u7aef\u961f\u5217<\/h3>\n<\/p>\n<p><p>\u53cc\u7aef\u961f\u5217\u662f\u4e00\u79cd\u53ef\u4ee5\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u53cc\u7aef\u961f\u5217\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u53cc\u7aef\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u53cc\u7aef\u961f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>my_deque = deque([f&quot;\u7b2c{i+1}\u6b21\u8fd0\u884c&quot; for i in range(10)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u53cc\u7aef\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u53cc\u7aef\u961f\u5217\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while my_deque:<\/p>\n<p>    print(my_deque.popleft())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u53cc\u7aef\u961f\u5217\u5177\u6709\u4e24\u7aef\u64cd\u4f5c\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u4f7f\u7528\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u901a\u8fc7\u5143\u7ec4\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = tuple(f&quot;\u7b2c{i+1}\u6b21\u8fd0\u884c&quot; for i in range(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u904d\u5386\u5143\u7ec4\uff0c\u5e76\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for value in my_tuple:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\uff0c\u5e76\u4e14\u5143\u7ec4\u5177\u6709\u4e0d\u53ef\u53d8\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u96c6\u5408\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u96c6\u5408\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u591a\u6b21\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{print(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd0\u884c10\u6b21&quot;) for _ in range(10)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u96c6\u5408\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u96c6\u5408\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>print<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff08\u5373<code>None<\/code>\uff09\u3002\u867d\u7136\u751f\u6210\u7684\u96c6\u5408\u6ca1\u6709\u5b9e\u9645\u7528\u9014\uff0c\u4f46<code>print<\/code>\u51fd\u6570\u7684\u4ee3\u7801\u88ab\u6267\u884c\u4e8610\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0Python\u4ee3\u7801\u768410\u6b21\u8fd0\u884c\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u51fd\u6570\u8c03\u7528\u3001\u6a21\u5757\u5316\u7f16\u7a0b\uff0c\u8fd8\u662f\u4f7f\u7528\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u3001\u534f\u7a0b\u7b49\u9ad8\u7ea7\u6280\u672f\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u5347\u6267\u884c\u6548\u7387\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\u8fd0\u884c\u7279\u5b9a\u6b21\u6570\u7684\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6216<code>while<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\u4ee3\u7801\u5757\u7684\u591a\u6b21\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;\u6b21\u8fd0\u884c&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u4ee3\u7801\u5757\u8fd0\u884c10\u6b21\uff0c\u6bcf\u6b21\u53ef\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5e0c\u671b\u5728\u6bcf\u6b21\u8fd0\u884c\u4e4b\u95f4\u589e\u52a0\u5ef6\u8fdf\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>sleep()<\/code>\u51fd\u6570\u6765\u589e\u52a0\u6bcf\u6b21\u8fd0\u884c\u4e4b\u95f4\u7684\u5ef6\u8fdf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import time\n\nfor i in range(10):\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;\u6b21\u8fd0\u884c&quot;)\n    time.sleep(1)  # \u6bcf\u6b21\u8fd0\u884c\u4e4b\u95f4\u5ef6\u8fdf1\u79d2\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5728\u6bcf\u6b21\u8f93\u51fa\u540e\u4f1a\u6682\u505c1\u79d2\uff0c\u9002\u5408\u9700\u8981\u63a7\u5236\u8fd0\u884c\u901f\u5ea6\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8fd0\u884c10\u6b21\u540e\u6839\u636e\u6761\u4ef6\u505c\u6b62\u4ee3\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u7ed3\u5408<code>if<\/code>\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5728\u8fd0\u884c\u5230\u7b2c5\u6b21\u65f6\u505c\u6b62\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i == 4:  # \u5728\u7b2c5\u6b21\u65f6\u505c\u6b62\n        print(&quot;\u8fbe\u5230\u505c\u6b62\u6761\u4ef6\uff0c\u9000\u51fa\u8fd0\u884c&quot;)\n        break\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;\u6b21\u8fd0\u884c&quot;)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u5faa\u73af\uff0c\u9002\u7528\u4e8e\u52a8\u6001\u63a7\u5236\u7a0b\u5e8f\u8fd0\u884c\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u6784\u5efa\u51fd\u6570\u3001\u4f7f\u7528\u6a21\u5757\u5316\u7f16\u7a0b\uff0c\u53ef\u4ee5\u8ba9Python\u8fd0\u884c10\u6b21\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1161522,"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\/1161513"}],"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=1161513"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161513\/revisions"}],"predecessor-version":[{"id":1161524,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161513\/revisions\/1161524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161522"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}