{"id":1187638,"date":"2025-01-15T20:08:01","date_gmt":"2025-01-15T12:08:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187638.html"},"modified":"2025-01-15T20:08:04","modified_gmt":"2025-01-15T12:08:04","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%bb%a3%e7%a0%81%e9%83%bd%e7%bc%a9%e8%bf%9b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187638.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4ee3\u7801\u90fd\u7f29\u8fdb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140147\/4c0c7666-6342-43c4-a92c-1078f5649e5f.webp\" alt=\"python\u5982\u4f55\u8ba9\u4ee3\u7801\u90fd\u7f29\u8fdb\" \/><\/p>\n<p><p> <strong>Python\u4ee3\u7801\u7f29\u8fdb\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tab\u952e\u3001\u4f7f\u7528\u7a7a\u683c\u952e\u3001\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u81ea\u52a8\u7f29\u8fdb\u3001\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684<strong>\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/strong>\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\uff0c\u8fd8\u80fd\u81ea\u52a8\u5904\u7406\u7f29\u8fdb\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u5982Black\u6216YAPF\uff0c\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u548c\u4f18\u5316Python\u4ee3\u7801\u7684\u683c\u5f0f\u3002\u8fd9\u4e9b\u5de5\u5177\u4e0d\u4ec5\u80fd\u591f\u786e\u4fdd\u4ee3\u7801\u7f29\u8fdb\u6b63\u786e\uff0c\u8fd8\u80fd\u5904\u7406\u5176\u4ed6\u4ee3\u7801\u98ce\u683c\u95ee\u9898\uff0c\u6bd4\u5982\u884c\u957f\u5ea6\u3001\u7a7a\u884c\u7b49\u3002\u4ee5Black\u4e3a\u4f8b\uff0c\u5b83\u662f\u4e00\u4e2a\u65e0\u914d\u7f6e\u7684\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u80fd\u591f\u5feb\u901f\u5730\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u7b26\u5408\u7edf\u4e00\u7684\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tab\u952e\u548c\u7a7a\u683c\u952e<\/h3>\n<\/p>\n<p><p>Python\u8bed\u8a00\u4e25\u683c\u4f9d\u8d56\u7f29\u8fdb\u6765\u533a\u5206\u4ee3\u7801\u5757\uff0c\u56e0\u6b64\u6b63\u786e\u7684\u7f29\u8fdb\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u786e\u4fdd\u4ee3\u7801\u7f29\u8fdb\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><h4>1.1\u3001\u4f7f\u7528Tab\u952e<\/h4>\n<\/p>\n<p><p>Tab\u952e\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f29\u8fdb\u65b9\u6cd5\uff0c\u901a\u8fc7\u6309\u4e0bTab\u952e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u884c\u7684\u5f00\u5934\u63d2\u5165\u4e00\u4e2a\u5236\u8868\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u7f29\u8fdb\u3002\u4f46\u662f\uff0c\u4e0d\u5efa\u8bae\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528Tab\u952e\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u7f16\u8f91\u5668\u5bf9Tab\u952e\u7684\u89e3\u91ca\u53ef\u80fd\u4e0d\u540c\uff0c\u5bfc\u81f4\u7f29\u8fdb\u4e0d\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u4f7f\u7528\u7a7a\u683c\u952e<\/h4>\n<\/p>\n<p><p>Python\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u7a7a\u683c\u952e\u6765\u8fdb\u884c\u7f29\u8fdb\u3002\u901a\u5e38\uff0c\u6bcf\u4e00\u7ea7\u7f29\u8fdb\u4f7f\u75284\u4e2a\u7a7a\u683c\u3002\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u884c\u7684\u5f00\u5934\u624b\u52a8\u63d2\u5165\u7a7a\u683c\uff0c\u6216\u8005\u914d\u7f6e\u7f16\u8f91\u5668\u81ea\u52a8\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u81ea\u52a8\u7f29\u8fdb<\/h3>\n<\/p>\n<p><p>\u5927\u591a\u6570\u4ee3\u7801\u7f16\u8f91\u5668\u90fd\u652f\u6301\u81ea\u52a8\u7f29\u8fdb\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u7f16\u8f91\u5668\u7684\u8bbe\u7f6e\u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u4ee5\u786e\u4fdd\u6bcf\u6b21\u6309\u4e0bTab\u952e\u65f6\u63d2\u51654\u4e2a\u7a7a\u683c\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7f16\u8f91\u5668\u7684\u914d\u7f6e\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>2.1\u3001VS Code<\/h4>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6e\u81ea\u52a8\u7f29\u8fdb\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u8bbe\u7f6e\uff08File &gt; Preferences &gt; Settings\uff09\u3002<\/li>\n<li>\u641c\u7d22\u201cTab Size\u201d\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a4\u3002<\/li>\n<li>\u641c\u7d22\u201cInsert Spaces\u201d\u5e76\u786e\u4fdd\u5176\u88ab\u542f\u7528\u3002<\/li>\n<\/ol>\n<p><h4>2.2\u3001PyCharm<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6e\u81ea\u52a8\u7f29\u8fdb\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u8bbe\u7f6e\uff08File &gt; Settings\uff09\u3002<\/li>\n<li>\u5bfc\u822a\u5230Editor &gt; Code Style &gt; Python\u3002<\/li>\n<li>\u5728Tab and Indents\u9009\u9879\u5361\u4e2d\uff0c\u5c06Tab size\u548cIndent\u8bbe\u7f6e\u4e3a4\u3002<\/li>\n<\/ol>\n<p><h4>2.3\u3001Sublime Text<\/h4>\n<\/p>\n<p><p>\u5728Sublime Text\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6e\u81ea\u52a8\u7f29\u8fdb\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00Preferences &gt; Settings\u3002<\/li>\n<li>\u5728\u8bbe\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a\n<pre><code class=\"language-json\">{<\/p>\n<p>    &quot;tab_size&quot;: 4,<\/p>\n<p>    &quot;translate_tabs_to_spaces&quot;: true<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u683c\u5f0f\uff0c\u5305\u62ec\u7f29\u8fdb\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><h4>3.1\u3001Black<\/h4>\n<\/p>\n<p><p>Black\u662f\u4e00\u4e2a\u65e0\u914d\u7f6e\u7684\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574Python\u4ee3\u7801\u7684\u683c\u5f0f\u3002\u4f7f\u7528Black\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Black\uff1a\n<pre><code class=\"language-bash\">pip install black<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u683c\u5f0f\u5316\u4ee3\u7801\uff1a\n<pre><code class=\"language-bash\">black your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>Black\u5c06\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u7f29\u8fdb\u548c\u5176\u4ed6\u683c\u5f0f\uff0c\u4f7f\u5176\u7b26\u5408\u7edf\u4e00\u7684\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001YAPF<\/h4>\n<\/p>\n<p><p>YAPF\u662f\u53e6\u4e00\u4e2a\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u6839\u636ePEP 8\u89c4\u8303\u683c\u5f0f\u5316Python\u4ee3\u7801\u3002\u4f7f\u7528YAPF\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5YAPF\uff1a\n<pre><code class=\"language-bash\">pip install yapf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u683c\u5f0f\u5316\u4ee3\u7801\uff1a\n<pre><code class=\"language-bash\">yapf -i your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>YAPF\u5c06\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u6216\u9ed8\u8ba4\u8bbe\u7f6e\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u683c\u5f0f\uff0c\u5305\u62ec\u7f29\u8fdb\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001Python\u7f29\u8fdb\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7f29\u8fdb\u4e00\u81f4\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>4.1\u3001\u4f7f\u75284\u4e2a\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb<\/h4>\n<\/p>\n<p><p>Python\u5b98\u65b9\u63a8\u8350\u6bcf\u4e00\u7ea7\u7f29\u8fdb\u4f7f\u75284\u4e2a\u7a7a\u683c\uff0c\u8fd9\u662f\u5927\u591a\u6570Python\u9879\u76ee\u7684\u6807\u51c6\u505a\u6cd5\u3002\u4f7f\u7528\u7a7a\u683c\u800c\u4e0d\u662fTab\u952e\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u7f16\u8f91\u5668\u5bf9Tab\u952e\u89e3\u91ca\u4e0d\u540c\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u907f\u514d\u6df7\u7528Tab\u952e\u548c\u7a7a\u683c\u952e<\/h4>\n<\/p>\n<p><p>\u6df7\u7528Tab\u952e\u548c\u7a7a\u683c\u952e\u4f1a\u5bfc\u81f4\u7f29\u8fdb\u4e0d\u4e00\u81f4\uff0c\u8fdb\u800c\u5bfc\u81f4\u4ee3\u7801\u8fd0\u884c\u65f6\u51fa\u73b0\u9519\u8bef\u3002\u786e\u4fdd\u5728\u6574\u4e2a\u4ee3\u7801\u5e93\u4e2d\u4ec5\u4f7f\u7528\u4e00\u79cd\u7f29\u8fdb\u65b9\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528\u7a7a\u683c\u952e\u3002<\/p>\n<\/p>\n<p><h4>4.3\u3001\u914d\u7f6e\u7f16\u8f91\u5668\u81ea\u52a8\u8f6c\u6362Tab\u952e\u4e3a\u7a7a\u683c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u624b\u52a8\u8f93\u5165\u7a7a\u683c\uff0c\u53ef\u4ee5\u914d\u7f6e\u7f16\u8f91\u5668\u5c06Tab\u952e\u81ea\u52a8\u8f6c\u6362\u4e3a4\u4e2a\u7a7a\u683c\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u6309\u4e0bTab\u952e\u65f6\uff0c\u7f16\u8f91\u5668\u4f1a\u63d2\u51654\u4e2a\u7a7a\u683c\uff0c\u800c\u4e0d\u662f\u5236\u8868\u7b26\u3002<\/p>\n<\/p>\n<p><h4>4.4\u3001\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Black\u6216YAPF\u7b49\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u683c\u5f0f\uff0c\u5305\u62ec\u7f29\u8fdb\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u6216\u9ed8\u8ba4\u8bbe\u7f6e\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\uff0c\u4f7f\u5176\u7b26\u5408\u7edf\u4e00\u7684\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h4>4.5\u3001\u5b9a\u671f\u68c0\u67e5\u4ee3\u7801\u7f29\u8fdb<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5b9a\u671f\u68c0\u67e5\u4ee3\u7801\u7684\u7f29\u8fdb\uff0c\u786e\u4fdd\u6bcf\u4e00\u7ea7\u7f29\u8fdb\u90fd\u662f\u6b63\u786e\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u7f16\u8f91\u5668\u7684\u663e\u793a\u7a7a\u683c\u548cTab\u952e\u529f\u80fd\uff0c\u5e2e\u52a9\u68c0\u67e5\u7f29\u8fdb\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u7684Python\u7f29\u8fdb\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u5e38\u89c1\u7684\u7f29\u8fdb\u9519\u8bef\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><h4>5.1\u3001\u7f29\u8fdb\u4e0d\u4e00\u81f4<\/h4>\n<\/p>\n<p><p>\u7f29\u8fdb\u4e0d\u4e00\u81f4\u662f\u6307\u5728\u540c\u4e00\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u7f29\u8fdb\u65b9\u5f0f\uff0c\u4f8b\u5982\u6df7\u7528\u4e86Tab\u952e\u548c\u7a7a\u683c\u952e\u3002\u7f29\u8fdb\u4e0d\u4e00\u81f4\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fd0\u884c\u65f6\u51fa\u73b0IndentationError\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u7f3a\u5c11\u7f29\u8fdb<\/h4>\n<\/p>\n<p><p>\u7f3a\u5c11\u7f29\u8fdb\u662f\u6307\u5728\u9700\u8981\u7f29\u8fdb\u7684\u5730\u65b9\u6ca1\u6709\u8fdb\u884c\u7f29\u8fdb\uff0c\u4f8b\u5982\u5728\u5b9a\u4e49\u51fd\u6570\u3001\u5faa\u73af\u6216\u6761\u4ef6\u8bed\u53e5\u65f6\uff0c\u6ca1\u6709\u5728\u4ee3\u7801\u5757\u7684\u5f00\u5934\u8fdb\u884c\u7f29\u8fdb\u3002\u7f3a\u5c11\u7f29\u8fdb\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fd0\u884c\u65f6\u51fa\u73b0IndentationError\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>5.3\u3001\u591a\u4f59\u7684\u7f29\u8fdb<\/h4>\n<\/p>\n<p><p>\u591a\u4f59\u7684\u7f29\u8fdb\u662f\u6307\u5728\u4e0d\u9700\u8981\u7f29\u8fdb\u7684\u5730\u65b9\u8fdb\u884c\u4e86\u7f29\u8fdb\uff0c\u4f8b\u5982\u5728\u4ee3\u7801\u5757\u4e4b\u5916\u8fdb\u884c\u4e86\u7f29\u8fdb\u3002\u591a\u4f59\u7684\u7f29\u8fdb\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fd0\u884c\u65f6\u51fa\u73b0IndentationError\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u5982Black\u6216YAPF\uff0c\u6765\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u7f29\u8fdb\u548c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001Python\u7f29\u8fdb\u7684\u5177\u4f53\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3Python\u7f29\u8fdb\u7684\u91cd\u8981\u6027\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>6.1\u3001\u51fd\u6570\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u4f53\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570\u4f53\u7684print\u8bed\u53e5\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\uff0c\u4ee5\u8868\u793a\u5b83\u662f\u51fd\u6570\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u6761\u4ef6\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u65f6\uff0c\u6761\u4ef6\u5757\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if condition:<\/p>\n<p>    print(&quot;Condition is true&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Condition is false&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cif\u548celse\u5757\u7684print\u8bed\u53e5\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\uff0c\u4ee5\u8868\u793a\u5b83\u4eec\u662f\u6761\u4ef6\u8bed\u53e5\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>6.3\u3001\u5faa\u73af\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u65f6\uff0c\u5faa\u73af\u4f53\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\u3002\u4f8b\u5982\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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cfor\u5faa\u73af\u4f53\u7684print\u8bed\u53e5\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\uff0c\u4ee5\u8868\u793a\u5b83\u662f\u5faa\u73af\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>6.4\u3001\u5d4c\u5957\u4ee3\u7801\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5d4c\u5957\u4ee3\u7801\u5757\u65f6\uff0c\u6bcf\u4e00\u7ea7\u5d4c\u5957\u90fd\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        if i % 2 == 0:<\/p>\n<p>            print(f&quot;{i} is even&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;{i} is odd&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570\u4f53\u3001for\u5faa\u73af\u4f53\u548cif-else\u5757\u90fd\u9700\u8981\u8fdb\u884c\u7f29\u8fdb\uff0c\u4ee5\u8868\u793a\u5b83\u4eec\u662f\u5d4c\u5957\u4ee3\u7801\u5757\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4ee3\u7801\u7684\u7f29\u8fdb\u662f\u786e\u4fdd\u4ee3\u7801\u7ed3\u6784\u6b63\u786e\u548c\u53ef\u8bfb\u6027\u7684\u91cd\u8981\u56e0\u7d20\u3002\u901a\u8fc7\u4f7f\u7528Tab\u952e\u6216\u7a7a\u683c\u952e\u8fdb\u884c\u7f29\u8fdb\uff0c\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u81ea\u52a8\u7f29\u8fdb\uff0c\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u4ee5\u53ca\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7f29\u8fdb\u4e00\u81f4\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u907f\u514d\u5e38\u89c1\u7684\u7f29\u8fdb\u9519\u8bef\uff0c\u5b9a\u671f\u68c0\u67e5\u4ee3\u7801\u7f29\u8fdb\uff0c\u5e76\u5728\u5177\u4f53\u5e94\u7528\u573a\u666f\u4e2d\u6b63\u786e\u5e94\u7528\u7f29\u8fdb\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u9ad8\u8d28\u91cf\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7f29\u8fdb\uff1f<\/strong><br \/>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u4ee3\u7801\u7684\u7f29\u8fdb\u81f3\u5173\u91cd\u8981\uff0c\u786e\u4fdd\u6bcf\u4e2a\u4ee3\u7801\u5757\u7684\u5c42\u7ea7\u5173\u7cfb\u6e05\u6670\u3002\u4e3a\u4e86\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7f29\u8fdb\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6216\u6587\u672c\u7f16\u8f91\u5668\u7684\u683c\u5f0f\u5316\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyCharm\u6216Visual Studio Code\uff0c\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u63d0\u4f9b\u4e00\u952e\u683c\u5f0f\u5316\u4ee3\u7801\u7684\u9009\u9879\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9Python\u7684<code>autopep8<\/code>\u6216<code>black<\/code>\u5e93\uff0c\u8f7b\u677e\u5b9e\u73b0\u4ee3\u7801\u7684\u81ea\u52a8\u7f29\u8fdb\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u65f6\u6709\u54ea\u4e9b\u5feb\u6377\u952e\u53ef\u4ee5\u8c03\u6574\u7f29\u8fdb\uff1f<\/strong><br \/>\u5728\u8bb8\u591a\u6587\u672c\u7f16\u8f91\u5668\u548cIDE\u4e2d\uff0c\u5e38\u7528\u7684\u5feb\u6377\u952e\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u8c03\u6574\u4ee3\u7801\u7f29\u8fdb\u3002\u4f8b\u5982\uff0c\u5728Visual Studio Code\u4e2d\uff0c\u4f7f\u7528<code>Tab<\/code>\u952e\u589e\u52a0\u7f29\u8fdb\uff0c<code>Shift + Tab<\/code>\u5219\u53ef\u4ee5\u51cf\u5c11\u7f29\u8fdb\u3002\u5728Sublime Text\u4e2d\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>Tab<\/code>\u548c<code>Shift + Tab<\/code>\u8fdb\u884c\u7f29\u8fdb\u8c03\u6574\u3002\u638c\u63e1\u8fd9\u4e9b\u5feb\u6377\u952e\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7f16\u5199\u4ee3\u7801\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u7f29\u8fdb\u5728Python\u4e2d\u5982\u6b64\u91cd\u8981\uff1f<\/strong><br \/>\u7f29\u8fdb\u4e0d\u4ec5\u662fPython\u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u8fd8\u5f71\u54cd\u4ee3\u7801\u7684\u903b\u8f91\u7ed3\u6784\u3002\u5728Python\u4e2d\uff0c\u7f29\u8fdb\u7528\u4e8e\u5b9a\u4e49\u4ee3\u7801\u5757\uff0c\u6bd4\u5982\u51fd\u6570\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u7b49\u3002\u5982\u679c\u7f29\u8fdb\u4e0d\u6b63\u786e\uff0c\u4ee3\u7801\u5c06\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\uff0c\u751a\u81f3\u4f1a\u629b\u51fa<code>IndentationError<\/code>\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4ee3\u7801\u7f29\u8fdb\u7684\u4e00\u81f4\u6027\u662f\u7f16\u5199\u9ad8\u8d28\u91cfPython\u4ee3\u7801\u7684\u57fa\u672c\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4ee3\u7801\u7f29\u8fdb\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tab\u952e\u3001\u4f7f\u7528\u7a7a\u683c\u952e\u3001\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668\u81ea\u52a8\u7f29\u8fdb\u3001\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1187648,"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\/1187638"}],"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=1187638"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187638\/revisions"}],"predecessor-version":[{"id":1187652,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187638\/revisions\/1187652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}