{"id":194,"date":"2023-10-26T16:30:42","date_gmt":"2023-10-26T08:30:42","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=194"},"modified":"2023-10-27T09:34:10","modified_gmt":"2023-10-27T01:34:10","slug":"4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b4. \u6df1\u5165\u6d41\u7a0b\u63a7\u5236"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u9664\u4e86\u521a\u4ecb\u7ecd\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>&nbsp;\u8bed\u53e5\u5916, Python \u4e5f\u652f\u6301\u6709\u5176\u5b83\u8bed\u8a00\u4e2d\u901a\u89c1\u7684\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5, \u5f53\u7136\u6709\u70b9\u5c0f\u6539\u52a8.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.1.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u8bed\u53e5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#if\"><\/a><\/h2>\n\n\n\n<p>\u4e5f\u8bb8\u6700\u4e3a\u4eba\u6240\u77e5\u7684\u8bed\u53e5\u7c7b\u578b\u5c31\u662f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;\u8bed\u53e5\u4e86. \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong>x = int(input(&#8220;Please enter an integer: &#8220;)) Please enter an integer: 42 <strong>&gt;&gt;&gt; <\/strong><strong>if<\/strong> x &lt; 0: <strong>&#8230;<\/strong> x = 0 <strong>&#8230;<\/strong> print(&#8216;Negative changed to zero&#8217;) <strong>&#8230; <\/strong><strong>elif<\/strong> x == 0: <strong>&#8230;<\/strong> print(&#8216;Zero&#8217;) <strong>&#8230; <\/strong><strong>elif<\/strong> x == 1: <strong>&#8230;<\/strong> print(&#8216;Single&#8217;) <strong>&#8230; <\/strong><strong>else<\/strong>: <strong>&#8230;<\/strong> print(&#8216;More&#8217;) <strong>&#8230;<\/strong> More<\/p>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u6709\u96f6\u4e2a\u6216\u591a\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#elif\"><code>elif<\/code><\/a>&nbsp;\u5206\u652f, \u800c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#else\"><code>else<\/code><\/a>&nbsp;\u662f\u53ef\u9009\u7684. \u5173\u952e\u5b57 \u2018<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#elif\"><code>elif<\/code><\/a>\u2018 \u662f \u2018else if\u2019 \u7684\u7f29\u5199, \u5b83\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fc7\u5ea6\u7f29\u8fdb.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;&#8230;&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#elif\"><code>elif<\/code><\/a>&nbsp;&#8230;&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#elif\"><code>elif<\/code><\/a>&nbsp;&#8230; \u5e8f\u5217\u662f\u5176\u5b83\u8bed\u8a00\u4e2d&nbsp;<code>switch<\/code>&nbsp;\u6216&nbsp;<code>case<\/code>&nbsp;\u8bed\u53e5\u7684\u66ff\u4ee3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.2.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u8bed\u53e5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#for\"><\/a><\/h2>\n\n\n\n<p id=\"index-0\">Python \u4e2d\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u8bed\u53e5\u4e0e\u4f60\u5728 C \u6216\u662f Pascal \u4e2d\u4f7f\u7528\u7684\u7565\u6709\u4e0d\u540c. \u4e0d\u540c\u4e8e\u5728 Pascal \u4e2d\u603b\u662f\u4f9d\u636e\u4e00\u4e2a\u7b49\u5dee\u7684\u6570\u503c\u5e8f\u5217\u8fed\u4ee3, \u4e5f\u4e0d\u540c\u4e8e\u5728 C \u4e2d\u5141\u8bb8\u7528\u6237\u540c\u65f6\u5b9a\u4e49\u8fed\u4ee3\u6b65\u9aa4\u548c\u7ec8\u6b62\u6761\u4ef6, Python \u4e2d\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u8bed\u53e5\u5728\u4efb\u610f\u5e8f\u5217 (\u5217\u8868\u6216\u8005\u5b57\u7b26\u4e32) \u4e2d\u8fed\u4ee3\u65f6, \u603b\u662f\u6309\u7167\u5143\u7d20\u5728\u5e8f\u5217\u4e2d\u7684\u51fa\u73b0\u987a\u5e8f\u4f9d\u6b21\u8fed\u4ee3.<\/p>\n\n\n\n<p>for example (\u8fd9\u884c\u4e0d\u5faa\u73af;-):<strong>&gt;&gt;&gt; <\/strong><em># \u6d4b\u8bd5\u4e00\u4e9b\u5b57\u7b26\u4e32:<\/em><strong>&#8230; <\/strong>a = [&#8216;cat&#8217;, &#8216;window&#8217;, &#8216;defenestrate&#8217;] <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> x <strong>in<\/strong> a: <strong>&#8230;<\/strong> print(x, len(x)) <strong>&#8230;<\/strong> cat 3 window 6 defenestrate 12<\/p>\n\n\n\n<p>\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u4fee\u6539\u88ab\u8fed\u4ee3\u7684\u5bf9\u8c61\u662f\u4e0d\u5b89\u5168\u7684 (\u8fd9\u53ea\u53ef\u80fd\u53d1\u751f\u5728\u53ef\u53d8\u5e8f\u5217\u7c7b\u578b\u4e0a,\u5982\u5217\u8868).<\/p>\n\n\n\n<p>\u82e5\u60f3\u5728\u5faa\u73af\u4f53\u5185\u4fee\u6539\u4f60\u6b63\u8fed\u4ee3\u7684\u5e8f\u5217 (\u4f8b\u5982\u590d\u5236\u5e8f\u5217\u4e2d\u9009\u5b9a\u7684\u9879), \u6700\u597d\u662f\u5148\u5236\u4f5c\u4e00\u4e2a\u526f\u672c. \u4f46\u662f,\u5728\u5e8f\u5217\u4e0a\u7684\u8fed\u4ee3\u5e76\u4e0d\u4f1a\u81ea\u52a8\u9690\u5f0f\u5730\u521b\u5efa\u4e00\u4e2a\u526f\u672c.<\/p>\n\n\n\n<p>\u800c\u5207\u7247\u5219\u8ba9\u8fd9\u79cd\u64cd\u4f5c\u5341\u5206\u65b9\u4fbf:<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> x <strong>in<\/strong> a[:]: <em># \u5236\u9020\u6574\u4e2a\u5217\u8868\u7684\u5207\u7247\u590d\u672c<\/em><strong>&#8230; <\/strong><strong>if<\/strong> len(x) &gt; 6: a.insert(0, x) <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>a [&#8216;defenestrate&#8217;, &#8216;cat&#8217;, &#8216;window&#8217;, &#8216;defenestrate&#8217;]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.3.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#range\"><code>range()<\/code><\/a>&nbsp;\u51fd\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#range\"><\/a><\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u6570\u503c\u5e8f\u5217, \u4f7f\u7528\u5185\u5efa\u51fd\u5f0f&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#range\"><code>range()<\/code><\/a>&nbsp;\u4f1a\u5f88\u65b9\u4fbf. \u5b83\u4ea7\u751f\u7b49\u5dee\u7ea7\u6570\u5e8f\u5217:<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i <strong>in<\/strong> range(5): <strong>&#8230;<\/strong> print(i) <strong>&#8230;<\/strong> 0 1 2 3 4<\/p>\n\n\n\n<p>\u7ed9\u51fa\u7684\u7ec8\u6b62\u70b9\u4e0d\u4f1a\u5728\u751f\u6210\u7684\u5e8f\u5217\u91cc;&nbsp;<code>range(10)<\/code>&nbsp;\u751f\u6210 10 \u4e2a\u503c, \u7ec4\u6210\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5408\u6cd5\u5e8f\u5217. \u53ef\u4ee5\u8ba9&nbsp;<code>range<\/code>&nbsp;\u7684\u8d77\u59cb\u521d\u503c\u5b9a\u4e3a\u53e6\u4e00\u4e2a\u6570, \u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u4e0d\u540c\u7684\u589e\u91cf (\u751a\u81f3\u53ef\u4ee5\u4e3a\u8d1f; \u6709\u65f6\u8fd9\u88ab\u79f0\u4e3a \u2018\u6b65\u957f\u2019):range(5, 10) 5 through 9 range(0, 10, 3) 0, 3, 6, 9 range(-10, -100, -30) -10, -40, -70<\/p>\n\n\n\n<p>\u8981\u5bf9\u4e00\u4e2a\u5e8f\u5217\u7684\u7d22\u5f15\u8fdb\u884c\u8fed\u4ee3\u7684\u8bdd, \u7ec4\u5408\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#range\"><code>range()<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#len\"><code>len()<\/code><\/a>:<strong>&gt;&gt;&gt; <\/strong>a = [&#8216;Mary&#8217;, &#8216;had&#8217;, &#8216;a&#8217;, &#8216;little&#8217;, &#8216;lamb&#8217;] <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i <strong>in<\/strong> range(len(a)): <strong>&#8230;<\/strong> print(i, a[i]) <strong>&#8230;<\/strong> 0 Mary 1 had 2 a 3 little 4 lamb<\/p>\n\n\n\n<p>\u591a\u6570\u60c5\u51b5\u4e2d, \u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#enumerate\"><code>enumerate()<\/code><\/a>&nbsp;\u51fd\u5f0f\u66f4\u52a0\u65b9\u4fbf, \u53c2\u89c1&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-loopidioms\">\u904d\u5386\u6280\u5de7<\/a>.<\/p>\n\n\n\n<p>\u5f53\u4f60\u60f3\u6253\u5370 range \u65f6, \u4f1a\u5947\u602a:<strong>&gt;&gt;&gt; <\/strong>print(range(10)) range(0, 10)<\/p>\n\n\n\n<p>\u5728\u5f88\u591a\u65f6\u5019, \u7531&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#range\"><code>range()<\/code><\/a>&nbsp;\u8fd4\u56de\u7684\u5bf9\u8c61\u8868\u73b0\u5f97\u5c31\u50cf\u4e00\u4e2a\u5217\u8868, \u4f46\u5b9e\u9645\u4e0a\u5b83\u4e0d\u662f. \u5982\u679c\u4f60\u5bf9\u5176\u8fdb\u884c\u8fed\u4ee3\u65f6, \u5b83\u80fd\u8fd4\u56de\u6240\u9700\u8981\u7684\u8fde\u7eed\u9879, \u4f46\u5b9e\u9645\u4e0a\u4e3a\u4e86\u8282\u7701\u7a7a\u95f4\u5e76\u6ca1\u6709\u771f\u6b63\u5730\u751f\u6210\u5236\u9020\u4e00\u4e2a\u5217\u8868.<\/p>\n\n\n\n<p>\u6211\u4eec\u79f0\u8fd9\u79cd\u5bf9\u8c61\u53eb\u505a&nbsp;<em>iterable<\/em>&nbsp;, \u4e5f\u5c31\u662f\u8bf4, \u67d0\u4e9b\u51fd\u5f0f\u548c\u6784\u9020\u5668\u671f\u671b\u80fd\u4ece\u5bf9\u8c61\u8fde\u7eed\u63a5\u6536\u5143\u7d20\u76f4\u81f3\u7ec8\u7ed3, \u6211\u4eec\u79f0\u8fd9\u79cd\u5bf9\u8c61\u53eb\u505a&nbsp;<em>iterable<\/em>&nbsp;(\u53ef\u8fed\u4ee3\u7684).<\/p>\n\n\n\n<p>\u6211\u4eec\u5df2\u7ecf\u770b\u5230&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u8bed\u53e5\u5c31\u662f\u8fd9\u79cd\u8fed\u4ee3\u5668 (&nbsp;<em>iterator<\/em>&nbsp;).&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#list\"><code>list()<\/code><\/a>&nbsp;\u662f\u53e6\u4e00\u4e2a; \u5b83\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u751f\u6210\u5217\u8868:<strong>&gt;&gt;&gt; <\/strong>list(range(5)) [0, 1, 2, 3, 4]<\/p>\n\n\n\n<p>\u540e\u9762\u6211\u4eec\u5c06\u770b\u5230\u66f4\u591a\u8fd4\u56de&nbsp;<code>\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/code>&nbsp;\u548c\u5c06&nbsp;<code>\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/code>&nbsp;\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u5f0f.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.4.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#continue\"><code>continue<\/code><\/a>&nbsp;\u8bed\u53e5, \u4ee5\u53ca\u5faa\u73af\u4e2d\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#else\"><code>else<\/code><\/a>&nbsp;\u5b50\u53e5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#break-continue-else\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>&nbsp;\u8bed\u53e5, \u50cf C \u91cc\u7684\u4e00\u6837, \u8df3\u51fa\u6700\u5c0f\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;\u6216&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>&nbsp;\u5faa\u73af. \u5faa\u73af\u8bed\u53e5\u53ef\u4ee5\u6709\u4e00\u4e2a&nbsp;<code>else<\/code>&nbsp;\u5b50\u53e5; \u5f53\u5faa\u73af\u56e0\u8017\u5c3d\u6574\u4e2a\u5217\u8868\u800c\u7ec8\u6b62\u65f6 (\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>) \u6216\u8005\u5f53\u6761\u4ef6\u53d8\u4e3a\u5047\u65f6 (\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>), \u5b83\u5c31\u4f1a\u88ab\u6267\u884c, \u4f46\u662f, \u5982\u679c\u5faa\u73af\u56e0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>&nbsp;\u8bed\u53e5\u7ec8\u6b62\u7684\u8bdd, \u5b83\u4e0d\u4f1a\u88ab\u6267\u884c. \u4e0b\u9762\u7684\u641c\u7d22\u8d28\u6570\u7684\u4f8b\u5b50\u5c06\u8bc1\u660e\u8fd9\u70b9:<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> n <strong>in<\/strong> range(2, 10): <strong>&#8230; <\/strong><strong>for<\/strong> x <strong>in<\/strong> range(2, n): <strong>&#8230; <\/strong><strong>if<\/strong> n % x == 0: <strong>&#8230;<\/strong> print(n, &#8216;equals&#8217;, x, &#8216;*&#8217;, n\/\/x) <strong>&#8230; <\/strong><strong>break<\/strong><strong>&#8230; <\/strong><strong>else<\/strong>: <strong>&#8230; <\/strong><em># \u5faa\u73af\u56e0\u4e3a\u6ca1\u6709\u627e\u5230\u4e00\u4e2a\u56e0\u6570\u800c\u505c\u6b62<\/em><strong>&#8230;<\/strong> print(n, &#8216;is a prime number&#8217;) <strong>&#8230;<\/strong> 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3<\/p>\n\n\n\n<p>(\u662f\u7684, \u8fd9\u662f\u6b63\u786e\u7684\u4ee3\u7801. \u4ed4\u7ec6\u770b:&nbsp;else&nbsp;\u5b50\u53e5\u5c5e\u4e8e&nbsp;for&nbsp;\u5faa\u73af, \u800c\u975e\u662f&nbsp;if&nbsp;\u8bed\u53e5)<\/p>\n\n\n\n<p>\u4e0e\u5faa\u73af\u642d\u914d\u4f7f\u7528\u65f6,&nbsp;else&nbsp;\u5b50\u53e5\u7684\u884c\u4e3a\u548c\u5b83\u4e0e&nbsp;try&nbsp;\u8bed\u53e5\u7684\u642d\u914d\u65f6\u76f8\u5bf9\u4e8e\u4e0e&nbsp;if&nbsp;\u8bed\u53e5\u7684\u642d\u914d\u65f6\u6709\u66f4\u591a\u5171\u6027:&nbsp;try&nbsp;\u8bed\u53e5\u7684&nbsp;else&nbsp;\u5b50\u53e5\u5728\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\u65f6\u88ab\u6267\u884c, \u5faa\u73af\u7684&nbsp;else&nbsp;\u5b50\u53e5\u5728\u6ca1\u6709&nbsp;break&nbsp;\u8bed\u53e5\u662f\u88ab\u6267\u884c.<\/p>\n\n\n\n<p>\u67e5\u9605&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-errors\">\u5f02\u5e38\u5904\u7406<\/a>&nbsp;\u4e00\u8282\u83b7\u53d6\u66f4\u591a\u5173\u4e8e&nbsp;try&nbsp;\u8bed\u53e5\u548c\u5f02\u5e38\u7684\u4fe1\u606f.<\/p>\n\n\n\n<p>continue&nbsp;\u8bed\u53e5\u540c\u6837\u662f\u4ece C \u8bed\u8a00\u501f\u7528\u7684, \u5b83\u7ec8\u6b62\u5f53\u524d\u8fed\u4ee3\u800c\u8fdb\u884c\u5faa\u73af\u7684\u4e0b\u4e00\u6b21\u8fed\u4ee3.<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> num <strong>in<\/strong> range(2, 10): <strong>&#8230; <\/strong><strong>if<\/strong> num % 2 == 0: <strong>&#8230;<\/strong> print(&#8220;Found an even number&#8221;, num) <strong>&#8230; <\/strong><strong>continue<\/strong><strong>&#8230;<\/strong> print(&#8220;Found a number&#8221;, num) Found an even number 2 Found a number 3 Found an even number 4 Found a number 5 Found an even number 6 Found a number 7 Found an even number 8 Found a number 9<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.5.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;\u8bed\u53e5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#pass\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;\u8bed\u53e5\u4ec0\u4e48\u90fd\u4e0d\u505a. \u5f53\u8bed\u6cd5\u4e0a\u9700\u8981\u4e00\u4e2a\u8bed\u53e5, \u4f46\u7a0b\u5e8f\u4e0d\u8981\u52a8\u4f5c\u65f6, \u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83. \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong><strong>while<\/strong><strong>True<\/strong>: <strong>&#8230; <\/strong><strong>pass<\/strong><em># \u5fd9\u7b49\u5f85\u952e\u76d8\u4e2d\u65ad (Ctrl+C)<\/em><strong>&#8230;<\/strong><\/p>\n\n\n\n<p>\u4e00\u822c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6700\u5c0f\u7c7b:<strong>&gt;&gt;&gt; <\/strong><strong>class<\/strong><strong>MyEmptyClass<\/strong>: <strong>&#8230; <\/strong><strong>pass<\/strong><strong>&#8230;<\/strong><\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;\u7684\u5730\u65b9\u662f, \u4f5c\u4e3a\u51fd\u5f0f\u6216\u6761\u4ef6\u4f53\u7684\u5360\u4f4d\u7b26, \u5f53\u4f60\u5728\u65b0\u4ee3\u7801\u5de5\u4f5c\u65f6, \u5b83\u8ba9\u4f60\u80fd\u4fdd\u6301\u5728\u66f4\u62bd\u8c61\u7684\u7ea7\u522b\u601d\u8003.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;\u4f1a\u88ab\u9ed8\u9ed8\u5730\u88ab\u5ffd\u7565:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> initlog(*args): <strong>&#8230; <\/strong><strong>pass<\/strong><em># \u8bb0\u5f97\u5b9e\u73b0\u8fd9\u91cc!<\/em><strong>&#8230;<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.6. \u5b9a\u4e49\u51fd\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-functions\"><\/a><\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u51fd\u5f0f\u6765\u8f93\u51fa\u4efb\u610f\u6307\u5b9a\u8303\u56f4\u5185\u7684 \u83f2\u6ce2\u90a3\u5951(Fibonacci) \u6570\u5217:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> fib(n): <em># \u6253\u5370 Fibonacci \u5e8f\u5217\u5230 n<\/em><strong>&#8230; <\/strong><em>&#8220;&#8221;&#8221;\u6253\u5370\u5230 n \u7684 Fibonacci \u5e8f\u5217.&#8221;&#8221;&#8221;<\/em><strong>&#8230;<\/strong> a, b = 0, 1 <strong>&#8230; <\/strong><strong>while<\/strong> a &lt; n: <strong>&#8230;<\/strong> print(a, end=&#8217; &#8216;) <strong>&#8230;<\/strong> a, b = b, a+b <strong>&#8230;<\/strong> print() <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong><em># \u73b0\u5728\u8c03\u7528\u6211\u4eec\u521a\u5b9a\u4e49\u7684\u51fd\u5f0f:<\/em><strong>&#8230; <\/strong>fib(2000) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597<\/p>\n\n\n\n<p id=\"index-1\">\u5173\u952e\u5b57&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#def\"><code>def<\/code><\/a>&nbsp;\u5f15\u5165\u4e86\u4e00\u4e2a\u51fd\u5f0f&nbsp;<strong>\u5b9a\u4e49<\/strong>. \u540e\u9762\u5fc5\u987b\u8ddf\u4e0a\u51fd\u5f0f\u540d\u548c\u5728\u5706\u62ec\u53f7\u91cc\u7684\u53c2\u6570\u5e8f\u5217. \u51fd\u5f0f\u4f53\u4ece\u4e00\u884c\u5f00\u59cb, \u5e76\u4e14\u4e00\u5b9a\u8981\u7f29\u8fdb.<\/p>\n\n\n\n<p>\u51fd\u5f0f\u4f53\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u53ef\u4ee5\u662f\u5b57\u4e32; \u8fd9\u4e2a\u5b57\u4e32\u5c31\u662f\u51fd\u5f0f\u7684\u6587\u6863\u5b57\u7b26\u4e32, \u6216\u79f0\u4e3a&nbsp;<em>docstring<\/em>. (\u53ef\u4ee5\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-docstrings\">\u6587\u6863\u5b57\u4e32<\/a>&nbsp;\u4e00\u8282\u627e\u5230\u66f4\u591a\u4fe1\u606f) \u6709\u5f88\u591a\u80fd\u5c06\u6587\u6863\u5b57\u4e32\u81ea\u52a8\u8f6c\u6362\u4e3a\u5728\u7ebf\u6216\u53ef\u6253\u5370\u6587\u6863\u7684\u5de5\u5177, \u6216\u8ba9\u7528\u6237\u5728\u4ee3\u7801\u4e2d\u4ea4\u4e92\u5730\u6d4f\u89c8\u5b83\u7684\u5de5\u5177; \u5728\u4ee3\u7801\u91cc\u52a0\u4e0a\u6587\u6863\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u597d\u7684\u5b9e\u8df5, \u56e0\u6b64, \u8bf7\u517b\u6210\u8fd9\u4e2a\u4e60\u60ef.<\/p>\n\n\n\n<p><strong>\u6267\u884c<\/strong>&nbsp;\u51fd\u5f0f,\u4f1a\u5f15\u5165\u65b0\u7684\u7b26\u53f7\u8868(symbol table)\u7528\u4e8e\u8be5\u51fd\u5f0f\u7684\u5c40\u90e8\u53d8\u91cf. \u66f4\u7cbe\u786e\u5730\u8bf4, \u6240\u6709\u5728\u51fd\u5f0f\u4e2d\u88ab\u8d4b\u503c\u7684\u53d8\u91cf\u548c\u503c\u90fd\u5c06\u5b58\u50a8\u5728\u5c40\u90e8\u7b26\u53f7\u8868\u4e2d; \u9274\u4e8e\u53d8\u91cf\u5f15\u7528\u4f1a\u9996\u5148\u5728\u5c40\u90e8\u7b26\u53f7\u8868\u91cc\u5bfb\u627e, \u7136\u540e\u624d\u662f\u95ed\u5305\u51fd\u5f0f\u7684\u5c40\u90e8\u7b26\u53f7\u8868, \u518d\u7136\u540e\u662f\u5168\u5c40\u53d8\u91cf, \u6700\u540e\u662f\u5185\u5efa\u540d\u5b57\u8868. \u56e0\u6b64, \u5728\u51fd\u5f0f\u4e2d\u7684\u5c3d\u7ba1\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5f15\u7528,\u4f46\u662f\u4e0d\u53ef\u76f4\u63a5\u8d4b\u503c (\u9664\u975e\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;\u8bed\u53e5\u8fdb\u884c\u58f0\u660e).<\/p>\n\n\n\n<p>\u51fd\u5f0f\u7684\u5b9e\u53c2\u5728\u5b83\u88ab\u8c03\u7528\u65f6\u88ab\u5f15\u5165\u5230\u8fd9\u4e2a\u51fd\u5f0f\u7684\u5c40\u90e8\u53d8\u91cf\u8868; \u56e0\u6b64, \u53c2\u6570\u662f&nbsp;<strong>\u6309\u503c<\/strong>&nbsp;\u4f20\u9012\u7684 (<strong>\u503c<\/strong>&nbsp;\u603b\u662f\u5bf9\u8c61\u7684\u4e00\u4e2a&nbsp;<strong>\u5f15\u7528<\/strong>&nbsp;, \u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u7684\u503c).&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#id17\">[1]<\/a>&nbsp;\u5f53\u4e00\u4e2a\u51fd\u5f0f\u8c03\u7528\u53e6\u4e00\u4e2a\u65f6, \u5bf9\u5e94\u8fd9\u6b21\u8c03\u7528,\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u7b26\u53f7\u8868\u5c31\u4f1a\u88ab\u521b\u5efa.<\/p>\n\n\n\n<p>\u51fd\u5f0f\u5b9a\u4e49\u4f1a\u5728\u5f53\u524d\u7684\u7b26\u53f7\u8868\u91cc\u5f15\u5165\u8be5\u51fd\u5f0f\u7684\u540d\u5b57. \u51fd\u5f0f\u540d\u5bf9\u5e94\u7684\u503c\u88ab\u89e3\u91ca\u5668\u8ba4\u5b9a\u4e3a\u81ea\u5b9a\u4e49\u51fd\u5f0f\u7c7b\u578b \u51fd\u5f0f\u540d\u7684\u503c\u53ef\u4ee5\u88ab\u8d4b\u4e88\u53e6\u4e00\u4e2a\u540d\u5b57, \u4f7f\u5176\u4e5f\u80fd\u4f5c\u4e3a\u51fd\u5f0f\u4f7f\u7528. \u8fd9\u662f\u5e38\u89c4\u7684\u91cd\u547d\u540d\u673a\u5236:<strong>&gt;&gt;&gt; <\/strong>fib &lt;function fib at 10042ed0&gt; <strong>&gt;&gt;&gt; <\/strong>f = fib <strong>&gt;&gt;&gt; <\/strong>f(100) 0 1 1 2 3 5 8 13 21 34 55 89<\/p>\n\n\n\n<p>\u6839\u636e\u5176\u5b83\u8bed\u8a00\u7684\u7ecf\u9a8c, \u4f60\u53ef\u80fd\u4f1a\u6307\u51fa&nbsp;<code>fib<\/code>&nbsp;\u4e0d\u662f\u4e00\u4e2a\u51fd\u5f0f, \u800c\u662f\u4e00\u4e2a\u7a0b\u5e8f, \u56e0\u4e3a\u5b83\u4e0d\u8fd4\u56de\u503c. \u4e8b\u5b9e\u4e0a, \u5373\u4f7f\u6ca1\u6709&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;\u8bed\u53e5\u7684\u51fd\u5f0f\u4e5f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u503c, \u5c3d\u7ba1\u8fd9\u4e2a\u503c\u76f8\u5f53\u65e0\u804a. \u8fd9\u4e2a\u503c\u540d\u4e3a&nbsp;<code>None<\/code>&nbsp;(\u5b83\u662f\u4e2a\u5185\u5efa\u540d\u5b57). \u5982\u679c\u8981\u552f\u4e00\u8f93\u51fa\u7684\u503c\u662f&nbsp;<code>None<\/code>, \u90a3\u4e48\u89e3\u91ca\u5668\u4f1a\u6b63\u5f53\u7684\u6291\u5236\u8fd9\u6b21\u8fd4\u56de. \u5982\u4f60\u5b9e\u5728\u60f3\u770b\u770b\u8fd9\u4e2a\u503c,\u53ef\u4ee5\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>:<strong>&gt;&gt;&gt; <\/strong>fib(0) <strong>&gt;&gt;&gt; <\/strong>print(fib(0)) None<\/p>\n\n\n\n<p>\u5199\u4e2a\u8fd4\u56de Fibonacci \u5e8f\u5217\u800c\u4e0d\u662f\u6253\u5370\u8f93\u51fa\u7684\u51fd\u5f0f, \u5f88\u7b80\u5355:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> fib2(n): <em># \u653e\u56de\u76f4\u5230 n \u7684 Fibonacci \u5e8f\u5217<\/em><strong>&#8230; <\/strong><em>&#8220;&#8221;&#8221;\u8fd4\u56de\u4e00\u4e2a\u5217\u8868, \u5305\u542b\u76f4\u5230 n \u7684 Fibonacci \u5e8f\u5217.&#8221;&#8221;&#8221;<\/em><strong>&#8230;<\/strong> result = [] <strong>&#8230;<\/strong> a, b = 0, 1 <strong>&#8230; <\/strong><strong>while<\/strong> a &lt; n: <strong>&#8230;<\/strong> result.append(a) <em># \u89c1\u4e0b\u6587<\/em><strong>&#8230;<\/strong> a, b = b, a+b <strong>&#8230; <\/strong><strong>return<\/strong> result <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>f100 = fib2(100) <em># \u8c03\u7528<\/em><strong>&gt;&gt;&gt; <\/strong>f100 <em># \u8f93\u51fa\u7ed3\u679c<\/em> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]<\/p>\n\n\n\n<p>\u50cf\u5f80\u5e38\u4e00\u6837, \u8fd9\u91cc\u4ecb\u7ecd\u4e86\u4e00\u4e9b Python \u7279\u6027:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;\u8bed\u53e5\u4ece\u51fd\u5f0f\u4e2d\u8fd4\u56de\u4e00\u4e2a\u503c. \u6ca1\u6709\u8868\u8fbe\u5f0f\u53c2\u6570\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;\u8bed\u53e5\u8fd4\u56de&nbsp;<code>None<\/code>. \u76f4\u5230\u51fd\u5f0f\u7ed3\u675f\u4e5f\u6ca1\u6709&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;\u8bed\u53e5\u4e5f\u8fd4\u56de&nbsp;<code>None<\/code>.<\/li>\n\n\n\n<li>\u8bed\u53e5&nbsp;<code>result.append(a)<\/code>&nbsp;\u8c03\u7528\u4e86\u5217\u8868\u5bf9\u8c61&nbsp;<code>result<\/code>&nbsp;\u7684\u4e00\u4e2a\u65b9\u6cd5. \u6240\u8c13&nbsp;<code>\u65b9\u6cd5<\/code>&nbsp;\u5c31\u662f \u2018\u5c5e\u4e8e\u2019 \u5bf9\u8c61\u7684\u51fd\u5f0f, \u8c03\u7528\u5f62\u5f0f\u4e3a&nbsp;<code>obj.methodname<\/code>, \u5728\u8fd9\u91cc&nbsp;<code>obj<\/code>&nbsp;\u662f\u67d0\u5bf9\u8c61\u7684\u540d\u5b57 (\u8fd9\u53ef\u80fd\u662f\u4e2a\u8868\u8fbe\u5f0f),&nbsp;<code>methodname<\/code>&nbsp;\u662f\u6b64\u5bf9\u8c61\u7c7b\u578b\u5b9a\u4e49\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u7684\u540d\u5b57. \u5728\u4e0d\u540c\u7c7b\u578b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u662f\u4e0d\u540c\u7684. \u4e0d\u540c\u7c7b\u578b\u4e2d\u5b9a\u4e49\u76f8\u540c\u540d\u5b57\u7684\u65b9\u6cd5\u4e0d\u4f1a\u5f15\u8d77\u6b67\u4e49. (\u4f60\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u8c61\u7c7b\u578b\u548c\u65b9\u6cd5, \u53c2\u9605&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-classes\">\u7c7b<\/a>) \u4f8b\u5b50\u4e2d\u7684&nbsp;<code>append()<\/code>&nbsp;\u65b9\u6cd5\u662f\u4e3a\u5217\u8868\u5bf9\u8c61\u5b9a\u4e49\u7684; \u80fd\u5728\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u65b0\u7684\u5143\u7d20. \u5728\u672c\u4f8b\u4e2d, \u7b49\u4ef7\u4e8e&nbsp;<code>result&nbsp;=&nbsp;result&nbsp;+&nbsp;[a]<\/code>, \u4f46\u76f8\u5bf9\u800c\u8a00\u66f4\u52a0\u9ad8\u6548.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4.7. \u6df1\u5165\u51fd\u5f0f\u5b9a\u4e49<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-defining\"><\/a><\/h2>\n\n\n\n<p>\u51fd\u5f0f\u5b9a\u4e49\u65f6\u5019\u53ef\u4ee5\u5e26\u82e5\u5e72\u53c2\u6570, \u6709\u4e09\u79cd\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\u7684\u4e0d\u540c\u5f62\u5f0f.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.1. \u9ed8\u8ba4\u53c2\u6570<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-defaultargs\"><\/a><\/h3>\n\n\n\n<p>\u6700\u6709\u7528\u7684\u5f62\u5f0f\u662f\u4e3a\u4e00\u4e2a\u6216\u66f4\u591a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c. \u8fd9\u6837\u521b\u5efa\u7684\u51fd\u5f0f\u8c03\u7528\u65f6\u53ef\u4ee5\u4e0d\u7528\u7ed9\u8db3\u53c2\u6570. \u4f8b\u5982:<strong>def<\/strong> ask_ok(prompt, retries=4, complaint=&#8217;Yes or no, please!&#8217;): <strong>while<\/strong><strong>True<\/strong>: ok = input(prompt) <strong>if<\/strong> ok <strong>in<\/strong> (&#8216;y&#8217;, &#8216;ye&#8217;, &#8216;yes&#8217;): <strong>return<\/strong><strong>True<\/strong><strong>if<\/strong> ok <strong>in<\/strong> (&#8216;n&#8217;, &#8216;no&#8217;, &#8216;nop&#8217;, &#8216;nope&#8217;): <strong>return<\/strong><strong>False<\/strong> retries = retries &#8211; 1 <strong>if<\/strong> retries &lt; 0: <strong>raise<\/strong> IOError(&#8216;refusenik user&#8217;) print(complaint)<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u5f0f\u6709\u4ee5\u4e0b\u51e0\u79cd\u5408\u6cd5\u8c03\u7528\u5f62\u5f0f:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ec5\u7ed9\u51fa\u5f3a\u5236\u7684\u53c2\u6570:&nbsp;<code>ask_ok('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;quit?')<\/code><\/li>\n\n\n\n<li>\u591a\u51fa\u4e00\u4e2a\u53ef\u9009\u53c2\u6570:&nbsp;<code>ask_ok('OK&nbsp;to&nbsp;overwrite&nbsp;the&nbsp;file?',&nbsp;2)<\/code><\/li>\n\n\n\n<li>\u6216\u7ed9\u51fa\u6240\u6709\u53c2\u6570:&nbsp;<code>ask_ok('OK&nbsp;to&nbsp;overwrite&nbsp;the&nbsp;file?',&nbsp;2,&nbsp;'Come&nbsp;on,&nbsp;only&nbsp;yes&nbsp;or&nbsp;no!')<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e5f\u5f15\u5165\u4e86\u4e00\u4e2a\u5173\u952e\u5b57,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/expressions.html#in\"><code>in<\/code><\/a>&nbsp;\u7528\u4ee5\u6d4b\u8bd5\u5e8f\u5217\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e00\u503c.<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u7b49\u4e8e\u51fd\u5f0f&nbsp;<strong>\u5b9a\u4e49\u57df<\/strong>&nbsp;\u4e2d\u7684\u503c, \u56e0\u6b64i = 5 <strong>def<\/strong> f(arg=i): print(arg) i = 6 f()<\/p>\n\n\n\n<p>\u5c06\u6253\u5370&nbsp;<code>5<\/code>.<\/p>\n\n\n\n<p><strong>\u91cd\u8981\u8b66\u544a:<\/strong>&nbsp;\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u53ea\u4f1a\u88ab\u6c42\u4e00\u6b21\u503c. \u4f46\u8fd9\u5728\u9ed8\u8ba4\u53c2\u6570\u662f\u53ef\u53d8\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u5c31\u4e0d\u4e00\u6837\u4e86, \u5982\u5217\u8868, \u5b57\u5178, \u6216\u5927\u591a\u7c7b\u7684\u5bf9\u8c61\u65f6. \u4f8b\u5982, \u4e0b\u9762\u7684\u51fd\u5f0f\u5728\u968f\u540e\u7684\u8c03\u7528\u4e2d\u4f1a\u7d2f\u79ef\u53c2\u6570\u503c:<strong>def<\/strong> f(a, L=[]): L.append(a) <strong>return<\/strong> L print(f(1)) print(f(2)) print(f(3))<\/p>\n\n\n\n<p>\u5c06\u4f1a\u6253\u5370[1] [1, 2] [1, 2, 3]<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u8ba9\u53c2\u6570\u503c\u88ab\u540e\u6765\u7684\u8c03\u7528\u5171\u4eab, \u4f60\u53ef\u4ee5\u6539\u5199\u6210\u8fd9\u6837:<strong>def<\/strong> f(a, L=<strong>None<\/strong>): <strong>if<\/strong> L <strong>is<\/strong><strong>None<\/strong>: L = [] L.append(a) <strong>return<\/strong> L<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.2. \u5173\u952e\u5b57\u53c2\u6570<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-keywordargs\"><\/a><\/h3>\n\n\n\n<p>\u51fd\u5f0f\u4e5f\u53ef\u4ee5\u901a\u8fc7&nbsp;<code>keyword&nbsp;=&nbsp;value<\/code>&nbsp;\u5f62\u5f0f\u7684\u5173\u952e\u5b57\u53c2\u6570\u6765\u8c03\u7528. \u4f8b\u5982, \u4e0b\u9762\u7684\u51fd\u5f0f:<strong>def<\/strong> parrot(voltage, state=&#8217;a stiff&#8217;, action=&#8217;voom&#8217;, type=&#8217;Norwegian Blue&#8217;): print(&#8220;&#8211; This parrot wouldn&#8217;t&#8221;, action, end=&#8217; &#8216;) print(&#8220;if you put&#8221;, voltage, &#8220;volts through it.&#8221;) print(&#8220;&#8211; Lovely plumage, the&#8221;, type) print(&#8220;&#8211; It&#8217;s&#8221;, state, &#8220;!&#8221;)<\/p>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0b\u4efb\u4e00\u65b9\u6cd5\u8c03\u7528:parrot(1000) parrot(action = &#8216;VOOOOOM&#8217;, voltage = 1000000) parrot(&#8216;a thousand&#8217;, state = &#8216;pushing up the daisies&#8217;) parrot(&#8216;a million&#8217;, &#8216;bereft of life&#8217;, &#8216;jump&#8217;)<\/p>\n\n\n\n<p>\u4f46\u5982\u4e0b\u7684\u8c03\u7528\u662f\u975e\u6cd5\u7684:parrot() <em># \u7f3a\u5c11\u5fc5\u8981\u7684\u53c2\u6570<\/em> parrot(voltage=5.0, &#8216;dead&#8217;) <em># \u5728\u5173\u952e\u5b57\u540e\u9762\u8ddf\u7740\u975e\u5173\u952e\u5b57\u53c2\u6570<\/em> parrot(110, voltage=220) <em># \u540c\u4e00\u53c2\u6570\u7ed9\u4e86\u591a\u4e2a\u503c<\/em> parrot(actor=&#8217;John Cleese&#8217;) <em># \u672a\u77e5\u5173\u952e\u5b57<\/em><\/p>\n\n\n\n<p>\u5728\u51fd\u5f0f\u8c03\u7528\u65f6, \u5173\u952e\u5b57\u53c2\u6570\u5fc5\u987b\u8ddf\u5728\u4f4d\u7f6e\u53c2\u6570\u4e4b\u540e. \u6240\u6709\u7684\u5173\u952e\u5b57\u53c2\u6570\u90fd\u5fc5\u987b\u4e0e\u51fd\u5f0f\u63a5\u53d7\u7684\u5f62\u5f0f\u53c2\u6570\u5339\u914d (\u4f8b\u5982,&nbsp;<code>actor<\/code>&nbsp;\u5728\u51fd\u5f0f&nbsp;<code>parrot<\/code>&nbsp;\u770b\u6765\u5c31\u662f\u975e\u6cd5\u53c2\u6570), \u4f46\u4ed6\u4eec\u7684\u987a\u5e8f\u662f\u65e0\u5173\u7d27\u8981\u7684. \u8fd9\u6761\u89c4\u5219\u4e5f\u9002\u7528\u4e8e\u975e\u53ef\u9009\u53c2\u6570 (\u4f8b\u5982,&nbsp;<code>parrot(voltage=1000)<\/code>&nbsp;\u4e5f\u53ef\u4ee5\u7684). \u4efb\u4f55&nbsp;<code>\u5f62\u53c2<\/code>&nbsp;\u90fd\u4e0d\u80fd\u591a\u6b21\u63a5\u53d7\u4f20\u503c. \u4e0b\u9762\u7684\u4f8b\u5b50\u4ea7\u751f\u9519\u8bef\u7684\u539f\u56e0\u6b63\u662f\u8fdd\u53cd\u4e86\u8fd9\u4e00\u7ea6\u5b9a:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> function(a): <strong>&#8230; <\/strong><strong>pass<\/strong><strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>function(0, a=0) Traceback (most recent call last): File &#8220;&lt;stdin&gt;&#8221;, line 1, in ? TypeError: function() got multiple values for keyword argument &#8216;a&#8217;<\/p>\n\n\n\n<p>\u5f53\u6700\u540e\u4e00\u4e2a\u5f62\u53c2\u7684\u5f62\u5f0f\u4e3a&nbsp;<code>**name<\/code>&nbsp;\u65f6, \u5219\u6392\u9664\u5176\u4ed6\u7684\u5f62\u53c2\u7684\u503c, \u5b83\u5c06\u4ee5\u5b57\u5178 (\u53c2\u9605&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#typesmapping\">\u6620\u5c04\u7c7b\u578b\u2014\u2014\u5b57\u5178<\/a>) \u7684\u5f62\u5f0f\u5305\u542b\u6240\u6709\u5269\u4f59\u5173\u952e\u5b57\u53c2\u6570. \u8fd9\u79cd\u8c03\u7528\u53ef\u4ee5\u4e0e\u5177\u6709&nbsp;<code>*name<\/code>&nbsp;\u5f62\u5f0f\u7684\u5f62\u5f0f\u53c2\u6570 (\u5728\u4e0b\u4e00\u5c0f\u8282\u4e2d\u4ecb\u7ecd) \u8054\u5408\u4f7f\u7528, \u8fd9\u79cd\u5f62\u53c2\u63a5\u53d7\u6240\u6709\u8d85\u51fa\u51fd\u5f0f\u63a5\u53d7\u8303\u56f4\u7684\u4f4d\u7f6e\u53c2\u6570. (&nbsp;<code>*name<\/code>&nbsp;\u5fc5\u987b\u5728&nbsp;<code>**name<\/code>&nbsp;\u4e4b\u524d\u4f7f\u7528) \u4f8b\u5982, \u5982\u679c\u6211\u4eec\u50cf\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u51fd\u5f0f:<strong>def<\/strong> cheeseshop(kind, *arguments, **keywords): print(&#8220;&#8211; Do you have any&#8221;, kind, &#8220;?&#8221;) print(&#8220;&#8211; I&#8217;m sorry, we&#8217;re all out of&#8221;, kind) <strong>for<\/strong> arg <strong>in<\/strong> arguments: print(arg) print(&#8220;-&#8221; * 40) keys = sorted(keywords.keys()) <strong>for<\/strong> kw <strong>in<\/strong> keys: print(kw, &#8220;:&#8221;, keywords[kw])<\/p>\n\n\n\n<p>\u5b83\u53ef\u4ee5\u5982\u4e0b\u5730\u8c03\u7528:cheeseshop(&#8220;Limburger&#8221;, &#8220;It&#8217;s very runny, sir.&#8221;, &#8220;It&#8217;s really very, VERY runny, sir.&#8221;, shopkeeper=&#8221;Michael Palin&#8221;, client=&#8221;John Cleese&#8221;, sketch=&#8221;Cheese Shop Sketch&#8221;)<\/p>\n\n\n\n<p>\u5f53\u7136\u5b83\u5c06\u6253\u5370:&#8211; Do you have any Limburger ? &#8212; I&#8217;m sorry, we&#8217;re all out of Limburger It&#8217;s very runny, sir. It&#8217;s really very, VERY runny, sir. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- client : John Cleese shopkeeper : Michael Palin sketch : Cheese Shop Sketch<\/p>\n\n\n\n<p>\u6ce8\u610f, \u5173\u952e\u5b57\u53c2\u6570\u540d\u7684\u5217\u8868\u662f\u901a\u8fc7\u4e4b\u524d\u5bf9\u5b57\u5178&nbsp;<code>keys()<\/code>&nbsp;\u8fdb\u884c\u6392\u5e8f\u64cd\u4f5c\u800c\u521b\u5efa\u7684; \u5982\u679c\u4e0d\u8fd9\u6837\u505a, \u53c2\u6570\u6253\u5370\u7684\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.3. \u4efb\u610f\u53c2\u6570\u8868<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-arbitraryargs\"><\/a><\/h3>\n\n\n\n<p id=\"index-2\">\u6700\u540e, \u6700\u4e0d\u5e38\u7528\u7684\u9009\u62e9, \u662f\u6307\u5b9a\u51fd\u5f0f\u80fd\u591f\u5728\u8c03\u7528\u65f6\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570. \u8fd9\u4e9b\u53c2\u6570\u4f1a\u88ab\u5305\u88c5\u8fdb\u4e00\u4e2a\u5143\u7ec4 (\u53c2\u770b&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/datastructures.html#tut-tuples\">\u5143\u7ec4\u548c\u5e8f\u5217<\/a>). \u5728\u53d8\u957f\u53c2\u6570\u4e4b\u524d, \u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u591a\u4e2a\u6b63\u5e38\u53c2\u6570<strong>def<\/strong> write_multiple_items(file, separator, *args): file.write(separator.join(args))<\/p>\n\n\n\n<p>\u4e00\u822c\u5730, \u8fd9\u79cd&nbsp;<code>variadic<\/code>&nbsp;\u53c2\u6570\u5fc5\u987b\u5728\u5f62\u53c2\u5217\u8868\u7684\u672b\u5c3e, \u56e0\u4e3a\u5b83\u4eec\u5c06\u63a5\u6536\u4f20\u9012\u7ed9\u51fd\u5f0f\u7684\u6240\u6709\u5269\u4f59\u8f93\u5165\u53c2\u6570. \u4efb\u4f55\u51fa\u73b0\u5728&nbsp;*arg&nbsp;\u4e4b\u540e\u7684\u5f62\u5f0f\u53c2\u6570\u53ea\u80fd\u662f\u5173\u952e\u5b57\u53c2\u6570, \u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53ea\u80fd\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u7684\u65b9\u5f0f\u63a5\u6536\u4f20\u503c, \u800c\u4e0d\u80fd\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570.<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> concat(*args, sep=&#8221;\/&#8221;): <strong>&#8230; <\/strong><strong>return<\/strong> sep.join(args) <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>concat(&#8220;earth&#8221;, &#8220;mars&#8221;, &#8220;venus&#8221;) &#8216;earth\/mars\/venus&#8217; <strong>&gt;&gt;&gt; <\/strong>concat(&#8220;earth&#8221;, &#8220;mars&#8221;, &#8220;venus&#8221;, sep=&#8221;.&#8221;) &#8216;earth.mars.venus&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.4. \u53c2\u6570\u5217\u8868\u89e3\u5305<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-unpacking-arguments\"><\/a><\/h3>\n\n\n\n<p>\u4e5f\u5b58\u5728\u76f8\u53cd\u7684\u60c5\u5f62: \u5f53\u53c2\u6570\u5b58\u5728\u4e8e\u4e00\u4e2a\u65e2\u5b58\u7684\u5217\u8868\u6216\u8005\u5143\u7ec4\u4e4b\u4e2d, \u4f46\u5374\u9700\u8981\u89e3\u5305\u4ee5\u82e5\u5e72\u4f4d\u7f6e\u53c2\u6570\u7684\u5f62\u5f0f\u88ab\u51fd\u6570\u8c03\u7528. \u4f8b\u5982, \u5185\u5efa\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#range\"><code>range()<\/code><\/a>&nbsp;\u51fd\u6570\u671f\u671b\u63a5\u6536\u5206\u522b\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u4f4d\u7f6e\u53c2\u6570. \u5982\u679c\u5b83\u4eec\u4e0d\u662f\u5206\u522b\u53ef\u7528 (\u800c\u662f\u540c\u65f6\u5b58\u5728\u4e8e\u4e00\u4e2a\u5217\u8868\u6216\u8005\u5143\u7ec4\u4e2d), \u4e0b\u9762\u662f\u4e00\u4e2a\u5229\u7528&nbsp;*`-\u64cd\u4f5c\u7b26\u89e3\u4ece\u5217\u8868\u6216\u8005\u5143\u7ec4\u4e2d\u89e3\u5305\u53c2\u6570\u4ee5\u4f9b\u51fd\u6570\u8c03\u7528\u7684\u4f8b\u5b50:<strong>&gt;&gt;&gt; <\/strong>list(range(3, 6)) <em># \u4f7f\u7528\u5206\u79bb\u7684\u53c2\u6570\u6b63\u5e38\u8c03\u7528<\/em> [3, 4, 5] <strong>&gt;&gt;&gt; <\/strong>args = [3, 6] <strong>&gt;&gt;&gt; <\/strong>list(range(*args)) <em># \u901a\u8fc7\u89e3\u5305\u5217\u8868\u53c2\u6570\u8c03\u7528<\/em> [3, 4, 5]<\/p>\n\n\n\n<p id=\"index-3\">\u540c\u6837\u7684, \u5b57\u5178\u53ef\u4ee5\u901a\u8fc7&nbsp;<code>**<\/code>-\u64cd\u4f5c\u7b26\u6765\u89e3\u5305\u53c2\u6570:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> parrot(voltage, state=&#8217;a stiff&#8217;, action=&#8217;voom&#8217;): <strong>&#8230;<\/strong> print(&#8220;&#8211; This parrot wouldn&#8217;t&#8221;, action, end=&#8217; &#8216;) <strong>&#8230;<\/strong> print(&#8220;if you put&#8221;, voltage, &#8220;volts through it.&#8221;, end=&#8217; &#8216;) <strong>&#8230;<\/strong> print(&#8220;E&#8217;s&#8221;, state, &#8220;!&#8221;) <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>d = {&#8220;voltage&#8221;: &#8220;four million&#8221;, &#8220;state&#8221;: &#8220;bleedin&#8217; demised&#8221;, &#8220;action&#8221;: &#8220;VOOM&#8221;} <strong>&gt;&gt;&gt; <\/strong>parrot(**d) &#8212; This parrot wouldn&#8217;t VOOM if you put four million volts through it. E&#8217;s bleedin&#8217; demised !<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.5. Lambda \u5f62\u5f0f<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#lambda\"><\/a><\/h3>\n\n\n\n<p>\u6839\u636e\u5927\u4f17\u7684\u9700\u8981, \u4e00\u4e9b\u901a\u5e38\u51fa\u73b0\u5728\u8bf8\u5982 Lisp \u7b49\u51fd\u5f0f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7279\u6027\u4e5f\u5df2\u88ab\u52a0\u5165\u5230\u4e86 Python. \u4f7f\u7528\u5173\u952e\u5b57&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/expressions.html#lambda\"><code>lambda<\/code><\/a>, \u5c31\u53ef\u4ee5\u521b\u5efa\u77ed\u5c0f\u7684\u533f\u540d\u51fd\u5f0f. \u8fd9\u5c31\u662f\u80fd\u8fd4\u56de\u5b83\u4e24\u4e2a\u53c2\u6570\u548c\u7684\u51fd\u5f0f:&nbsp;<code>lambda&nbsp;a,&nbsp;b:&nbsp;a+b<\/code>. Lambda \u5f62\u5f0f\u53ef\u4ee5\u5728\u4efb\u610f\u9700\u8981\u51fd\u5f0f\u5bf9\u8c61\u7684\u5730\u65b9\u4f7f\u7528. \u8bed\u6cd5\u4e0a\u9650\u5236\u5b83\u4eec\u4e3a\u5355\u4e00\u7684\u8868\u8fbe\u5f0f. \u50cf\u5185\u5d4c\u51fd\u5f0f\u4e00\u6837, lambda \u5f62\u5f0f\u53ef\u4ee5\u5f15\u7528\u5f53\u524d\u57df\u91cc\u7684\u53d8\u91cf:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> make_incrementor(n): <strong>&#8230; <\/strong><strong>return<\/strong><strong>lambda<\/strong> x: x + n <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>f = make_incrementor(42) <strong>&gt;&gt;&gt; <\/strong>f(0) 42 <strong>&gt;&gt;&gt; <\/strong>f(1) 43<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.7.6. \u6587\u6863\u5b57\u7b26\u4e32<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-docstrings\"><\/a><\/h3>\n\n\n\n<p id=\"index-4\">\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u4e9b\u6587\u6863\u5b57\u4e32\u6709\u5173\u5185\u5bb9\u548c\u683c\u5f0f\u7684\u7ea6\u5b9a.<\/p>\n\n\n\n<p>\u7b2c\u4e00\u884c\u603b\u5e94\u5f53\u662f\u5bf9\u8be5\u5bf9\u8c61\u7684\u76ee\u7684\u8fdb\u884c\u7b80\u8ff0. \u4e3a\u4e86\u7b80\u77ed, \u5b83\u4e0d\u7528\u663e\u5f0f\u5730\u9648\u8ff0\u5bf9\u8c61\u7684\u540d\u5b57\u6216\u7c7b\u578b, \u56e0\u4e3a\u90fd\u662f\u53ef\u4ee5\u7528\u5176\u5b83\u624b\u6bb5\u83b7\u5f97 (\u9664\u975e\u8fd9\u540d\u5b57\u6070\u5de7\u662f\u63cf\u8ff0\u51fd\u5f0f\u64cd\u4f5c\u7684\u52a8\u8bcd). \u8fd9\u884c\u5e94\u5f53\u4ee5\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd\u5f00\u59cb, \u5e76\u4ee5\u53e5\u53f7\u7ed3\u675f.&nbsp;<sup>\u8bd1\u6ce8:\u51fa\u4e8e\u826f\u597d\u7684\u7f16\u7a0b\u7d20\u517b\u8003\u8651,\u5c3d\u53ef\u80fd\u7684\u7528E\u6587\u6ce8\u91ca\u5427.<\/sup><\/p>\n\n\n\n<p>\u5982\u679c\u8fd9\u4e2a\u6587\u6863\u5b57\u7b26\u4e32\u4e0d\u53ea\u4e00\u884c, \u90a3\u4e48\u7b2c\u4e8c\u884c\u5e94\u5f53\u4e3a\u7a7a, \u4ee5\u80fd\u4ece\u89c6\u89c9\u4e0a\u5206\u9694\u6982\u8ff0\u548c\u5176\u5b83\u90e8\u5206. \u63a5\u4e0b\u6765\u7684\u884c\u5e94\u5f53\u4e3a\u4e00\u4e2a\u6216\u66f4\u591a\u6bb5\u6765\u63cf\u8ff0\u8be5\u5bf9\u8c61\u7684\u8c03\u7528\u6761\u4ef6, \u5b83\u7684\u8fb9\u754c\u6548\u5e94\u7b49\u7b49.<\/p>\n\n\n\n<p>Python \u7684\u8bed\u6cd5\u5206\u6790\u5668\u5e76\u4e0d\u4f1a\u53bb\u9664\u591a\u884c\u5b57\u7b26\u4e32\u91cc\u7684\u7f29\u8fdb, \u6240\u4ee5\u5fc5\u8981\u7684\u65f6\u5019, \u5c31\u4e0d\u5f97\u4e0d\u4f7f\u7528\u5904\u7406\u6587\u6863\u7684\u5de5\u5177\u6765\u53bb\u9664\u7f29\u8fdb. \u4f7f\u7528\u4e0b\u9762\u8fd9\u6761\u7ea6\u5b9a. \u5728\u6587\u6863\u5b57\u7b26\u4e32\u7b2c\u4e00\u884c&nbsp;<strong>\u540e<\/strong>&nbsp;\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u884c\u51b3\u5b9a\u6574\u4e2a\u6587\u6863\u5b57\u7b26\u4e32\u7f29\u8fdb\u7684\u6570\u91cf. (\u6211\u4eec\u4e0d\u4f7f\u7528\u7b2c\u4e00\u884c\u7684\u539f\u56e0\u662f\u5b83\u901a\u5e38\u4e0e\u5b57\u7b26\u4e32\u7684\u5916\u5f15\u53f7\u76f8\u8fde\u800c\u4f7f\u5f97\u5b83\u7684\u7f29\u8fdb\u4e0d\u660e\u663e.) \u7559\u767d \u201c\u76f8\u5f53\u4e8e\u201d \u662f\u6587\u6863\u5b57\u4e32\u7684\u8d77\u59cb\u7f29\u8fdb\u5c06\u4f1a\u88ab\u6e05\u9664. \u6bcf\u884c\u4e0d\u5e94\u8be5\u5f53\u6709\u4e0d\u8db3\u7684\u7f29\u8fdb, \u5982\u679c\u6709\u524d\u5bfc\u7a7a\u767d,\u5c06\u4f1a\u5168\u90e8\u6e05\u9664. \u7531\u5236\u8868\u7b26\u6269\u5c55\u6210\u7684\u7a7a\u767d\u5e94\u8be5\u6d4b\u8bd5\u662f\u5426\u53ef\u7528(\u4e00\u822c\u88ab\u5151\u6362\u6210 8 \u4e2a\u7a7a\u683c).<\/p>\n\n\n\n<p>\u8fd9\u6709\u4e00\u4e2a\u591a\u884c\u6587\u6863\u7684\u4f8b\u5b50:<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> my_function(): <strong>&#8230; <\/strong><em>&#8220;&#8221;&#8221;Do nothing, but document it.<\/em><strong>&#8230;<\/strong><em><\/em><strong>&#8230; <\/strong><em>No, really, it doesn&#8217;t do anything.<\/em><strong>&#8230; <\/strong><em>&#8220;&#8221;&#8221;<\/em><strong>&#8230; <\/strong><strong>pass<\/strong><strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>print(my_function.__doc__) Do nothing, but document it. No, really, it doesn&#8217;t do anything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.8. \u63d2\u66f2: \u4ee3\u7801\u98ce\u683c<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#tut-codingstyle\"><\/a><\/h2>\n\n\n\n<p id=\"index-5\">\u4ece\u73b0\u5728\u5f00\u59cb, \u4f60\u5c06\u5199\u66f4\u957f\u66f4\u590d\u6742\u7684 Python \u4ee3\u7801, \u662f\u65f6\u5019\u8c08\u8bba&nbsp;<strong>\u4ee3\u7801\u98ce\u683c<\/strong>&nbsp;\u4e86. \u5927\u591a\u8bed\u8a00\u53ef\u4ee5\u7528\u4e0d\u540c\u98ce\u683c\u5199 (\u7b80\u6d01\u5730\u8bf4:&nbsp;<strong>\u683c\u5f0f\u5316<\/strong>) \u4ee3\u7801; \u603b\u662f\u6709\u4e00\u4e9b\u4f1a\u6bd4\u5176\u5b83\u7684\u66f4\u5177\u53ef\u8bfb\u6027. \u4f7f\u5176\u5b83\u4eba\u80fd\u591f\u8f7b\u677e\u8bfb\u61c2\u4f60\u7684\u4ee3\u7801\u901a\u5e38\u662f\u4e2a\u597d\u4e3b\u610f, \u800c\u63a5\u53d7\u4e00\u4e2a\u6f02\u4eae\u7684\u4ee3\u7801\u98ce\u683c\u4f1a\u5bf9\u90a3\u6709\u5f88\u5927\u7684\u5e2e\u52a9.<\/p>\n\n\n\n<p>\u5bf9\u4e8e Python,&nbsp;<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\"><strong>PEP 8<\/strong><\/a>&nbsp;\u5df2\u7ecf\u5448\u73b0\u4e86\u5927\u591a\u6570\u9879\u76ee\u9075\u5faa\u7684\u98ce\u683c; \u5b83\u5ba3\u4f20\u4e86\u4e00\u79cd\u5341\u5206\u53ef\u8bfb\u800c\u60a6\u76ee\u7684\u4ee3\u7801\u98ce\u683c. \u6bcf\u4e2a Python \u5f00\u53d1\u8005\u90fd\u5e94\u5f53\u5728\u67d0\u4e2a\u65f6\u523b\u9605\u8bfb\u5b83; \u8fd9\u91cc\u4e3a\u4f60\u8403\u53d6\u4e86\u6700\u91cd\u8981\u7684\u51e0\u70b9:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 4-\u7a7a\u683c \u7f29\u8fdb, \u4e14\u6ca1\u6709\u5236\u8868\u7b26.4 \u7a7a\u683c\u662f\u5728\u5c0f\u7f29\u8fdb (\u5141\u8bb8\u66f4\u591a\u5d4c\u5957) \u548c\u5927\u7f29\u8fdb (\u66f4\u6613\u8bfb) \u4e4b\u95f4\u7684\u597d\u7684\u59a5\u534f. \u5236\u8868\u7b26\u4f1a\u5e26\u6765\u6df7\u4e71, \u6700\u597d\u4e0d\u8981\u4f7f\u7528.<\/li>\n\n\n\n<li>\u8bbe\u5b9a\u81ea\u52a8\u6362\u884c (Wrap),\u4f7f\u5b83\u4eec\u4e0d\u8d85\u8fc7 79 \u4e2a\u5b57\u7b26.\u8fd9\u4f1a\u5e2e\u52a9\u5c0f\u5c4f\u5e55\u7684\u7528\u6237, \u800c\u4e14\u4f7f\u5f97\u53ef\u4ee5\u5728\u5927\u5c4f\u5e55\u4e0a\u540c\u65f6\u663e\u793a\u51e0\u4e2a\u4ee3\u7801\u6587\u4ef6\u6210\u4e3a\u53ef\u80fd.<\/li>\n\n\n\n<li>\u4f7f\u7528\u7a7a\u884c\u5206\u9694\u51fd\u5f0f\u548c\u7c7b, \u4ee5\u53ca\u51fd\u5f0f\u4e2d\u7684\u5927\u7684\u4ee3\u7801\u5757.<\/li>\n\n\n\n<li>\u5c3d\u53ef\u80fd\u4ee4\u6ce8\u91ca\u72ec\u5360\u4e00\u884c.<\/li>\n\n\n\n<li>\u4f7f\u7528\u6587\u6863\u5b57\u4e32.<\/li>\n\n\n\n<li>\u5728\u64cd\u4f5c\u7b26\u4e24\u8fb9, \u9017\u53f7\u540e\u9762\u4f7f\u7528\u7a7a\u683c, \u4f46\u662f\u62ec\u53f7\u5185\u90e8\u4e0e\u62ec\u53f7\u4e4b\u95f4\u76f4\u63a5\u76f8\u8fde\u7684\u90e8\u5206\u4e0d\u8981\u7a7a\u683c:&nbsp;<code>a&nbsp;=&nbsp;f(1,&nbsp;2)&nbsp;+&nbsp;g(3,&nbsp;4)<\/code>.<\/li>\n\n\n\n<li>\u4fdd\u6301\u7c7b\u540d\u548c\u51fd\u5f0f\u540d\u7684\u4e00\u81f4\u6027; \u7ea6\u5b9a\u662f, \u7c7b\u540d\u4f7f\u7528&nbsp;<code>CamelCase<\/code>&nbsp;\u683c\u5f0f, \u65b9\u6cd5\u540d\u548c\u51fd\u5f0f\u540d\u4f7f\u7528&nbsp;<code>lower_case_with_underscres<\/code>&nbsp;\u5f62\u5f0f. \u6c38\u8fdc\u4f7f\u7528&nbsp;<code>self<\/code>&nbsp;\u4f5c\u4e3a\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u540d (\u53c2\u9605&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-firstclasses\">\u7c7b\u7684\u521d\u5370\u8c61<\/a>&nbsp;\u83b7\u5f97\u66f4\u591a\u6709\u5173\u7c7b\u548c\u65b9\u6cd5\u7684\u4fe1\u606f).<\/li>\n\n\n\n<li>\u82e5\u4ee3\u7801\u6253\u7b97\u7528\u5728\u56fd\u9645\u5316\u7684\u73af\u5883\u4e2d, \u90a3\u4e48\u4e0d\u8981\u4f7f\u7528\u5947\u7279\u7684\u7f16\u7801. Python \u9ed8\u8ba4\u7684 UTF-8, \u6216\u8005\u751a\u81f3\u662f\u7b80\u5355\u7684 ASCII \u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u5de5\u4f5c\u5f97\u6700\u597d.<\/li>\n\n\n\n<li>\u540c\u6837\u5730, \u5982\u679c\u4ee3\u7801\u7684\u8bfb\u8005\u6216\u7ef4\u62a4\u8005\u53ea\u6709\u5f88\u5c0f\u7684\u6982\u7387\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u8a00, \u90a3\u4e48\u4e0d\u8981\u5728\u6807\u8bc6\u7b26\u91cc\u4f7f\u7528 \u975eASCII \u5b57\u7b26.<\/li>\n<\/ul>\n\n\n\n<p>Footnotes<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#id9\">[1]<\/a><\/td><td>\u5b9e\u9645\u4e0a,&nbsp;<em>\u901a\u8fc7\u5bf9\u8c61\u5f15\u7528\u8c03\u7528<\/em>&nbsp;\u4f1a\u662f\u4e2a\u66f4\u597d\u7684\u63cf\u8ff0, \u56e0\u4e3a\u5982\u679c\u4f20\u5165\u4e86\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570, \u8c03\u7528\u8005\u5c06\u770b\u5230\u88ab\u8c03\u7528\u8005\u5bf9\u5b83\u4f5c\u51fa\u7684\u4efb\u4f55\u6539\u53d8 (\u9879\u88ab\u63d2\u5165\u5230\u5217\u8868).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4.9. Function Annotations<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/controlflow.html#function-annotations\"><\/a><\/h2>\n\n\n\n<p>Function annotations are completely optional, arbitrary metadata information about user-defined functions. Neither Python itself nor the standard library use function annotations in any way; this section just shows the syntax. Third-party projects are free to use function annotations for documentation, type checking, and other uses.<\/p>\n\n\n\n<p>Annotations are stored in the __annotations__ attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal -&gt;, followed by an expression, between the parameter list and the colon denoting the end of the def statement. The following example has a positional argument, a keyword argument, and the return value annotated with nonsense: &gt;&gt;&gt;<strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> f(ham: 42, eggs: int = &#8216;spam&#8217;) -&gt; &#8220;Nothing to see here&#8221;: <strong>&#8230;<\/strong> print(&#8220;Annotations:&#8221;, f.__annotations__) <strong>&#8230;<\/strong> print(&#8220;Arguments:&#8221;, ham, eggs) <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>f(&#8216;wonderful&#8217;) Annotations: {&#8216;eggs&#8217;: &lt;class &#8216;int&#8217;&gt;, &#8216;return&#8217;: &#8216;Nothing to see here&#8217;, &#8216;ham&#8217;: 42} Arguments: wonderful spam<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9664\u4e86\u521a\u4ecb\u7ecd\u7684&nbsp;while&nbsp;\u8bed\u53e5\u5916, Python \u4e5f\u652f\u6301\u6709\u5176\u5b83\u8bed\u8a00\u4e2d\u901a\u89c1\u7684\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5,  [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-194","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":3,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":249,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions\/249"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}