{"id":81,"date":"2026-08-06T00:25:56","date_gmt":"2026-08-05T16:25:56","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=81"},"modified":"2026-08-06T00:42:52","modified_gmt":"2026-08-05T16:42:52","slug":"day-8-%e5%87%bd%e6%95%b0%e5%85%a5%e9%97%a8%ef%bc%9adef%e3%80%81%e5%8f%82%e6%95%b0%e3%80%81%e8%bf%94%e5%9b%9e%e5%80%bc%e4%b8%8e%e4%bd%9c%e7%94%a8%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-8-%e5%87%bd%e6%95%b0%e5%85%a5%e9%97%a8%ef%bc%9adef%e3%80%81%e5%8f%82%e6%95%b0%e3%80%81%e8%bf%94%e5%9b%9e%e5%80%bc%e4%b8%8e%e4%bd%9c%e7%94%a8%e5%9f%9f\/","title":{"rendered":"| Day 8 | \u51fd\u6570\u5165\u95e8\uff1adef\u3001\u53c2\u6570\u3001\u8fd4\u56de\u503c\u4e0e\u4f5c\u7528\u57df |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u51fd\u6570\u662f\u4ec0\u4e48 \u2014\u2014 \u628a\u300c\u590d\u5236\u7c98\u8d34\u300d\u5347\u7ea7\u6210\u300c\u547d\u540d\u590d\u7528\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u51fd\u6570<\/strong>\uff08function\uff09\u662f<strong>\u7ed9\u4e00\u6bb5\u4ee3\u7801\u8d77\u4e2a\u540d\u5b57\uff0c\u4ee5\u540e\u901a\u8fc7\u540d\u5b57\u53cd\u590d\u8c03\u7528\u5b83<\/strong>\u7684\u8bed\u6cd5\u7ed3\u6784\u3002\u4f60\u6709\u6ca1\u6709\u53d1\u73b0\uff0c\u524d\u9762\u5199\u7684\u4ee3\u7801\u91cc\u6709\u4e9b\u529f\u80fd\u53cd\u590d\u51fa\u73b0\uff1a\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u3001\u7b97\u5e73\u5747\u2026\u2026\u6bcf\u6b21\u90fd\u8981\u91cd\u65b0\u5199\u4e00\u904d\u3002\u51fd\u6570\u5c31\u662f\u300c\u7ed9\u4e00\u6bb5\u4ee3\u7801\u8d77\u4e2a\u540d\u5b57\uff0c\u4ee5\u540e\u60f3\u7528\u5c31\u53eb\u540d\u5b57\u300d\u2014\u2014\u4e00\u6b21\u5199\u597d\uff0c\u5230\u5904\u4f7f\u7528\u3002\u5b83\u662f\u7f16\u7a0b\u91cc\u6700\u91cd\u8981\u7684\u300c\u590d\u7528\u300d\u5de5\u5177\uff0c\u4e5f\u662f\u62c6\u89e3\u590d\u6742\u4efb\u52a1\u7684\u552f\u4e00\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>def<\/code> \u5b9a\u4e49\u51fd\u6570\u65f6\uff0cPython \u5e76\u4e0d\u4f1a\u6267\u884c\u51fd\u6570\u4f53\uff0c\u800c\u662f\u628a\u300c\u51fd\u6570\u4f53\u4ee3\u7801\u300d\u548c\u300c\u53c2\u6570\u4fe1\u606f\u300d\u6253\u5305\u6210\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u7ed1\u5b9a\u5230\u51fd\u6570\u540d\u4e0a\uff1b<strong>\u53ea\u6709\u8c03\u7528\uff08\u5199 <code>\u51fd\u6570\u540d()<\/code>\uff09\u65f6<\/strong>\uff0cPython \u624d\u4f1a\u65b0\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u300c\u6267\u884c\u7a7a\u95f4\u300d\uff08\u6808\u5e27\uff09\uff0c\u628a\u51fd\u6570\u4f53\u4ece\u5934\u6267\u884c\u4e00\u904d\uff0c\u6267\u884c\u5b8c\u9500\u6bc1\u8fd9\u4e2a\u7a7a\u95f4\u3002\u8fd9\u5c31\u662f\u300c\u5b9a\u4e49\u4e0d\u6267\u884c\u3001\u8c03\u7528\u624d\u6267\u884c\u300d\u7684\u5e95\u5c42\u539f\u56e0\uff0c\u4e5f\u662f\u51fd\u6570\u5185\u53d8\u91cf\u4e0e\u51fd\u6570\u5916\u53d8\u91cf\u4e92\u4e0d\u5e72\u6270\u7684\u6839\u6e90\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u672f\u8bed\u5c0f\u8bcd\u5178\uff1a<strong>\u6808\u5e27<\/strong>\uff08stack frame\uff09\u5c31\u662f\u300c\u51fd\u6570\u8fd0\u884c\u65f6\u4e34\u65f6\u642d\u8d77\u7684\u5de5\u4f5c\u53f0\u300d\uff0c\u51fd\u6570\u8dd1\u5b8c\u5de5\u4f5c\u53f0\u5c31\u62c6\u6389\uff0c\u4e0a\u9762\u7684\u5c40\u90e8\u6570\u636e\u4e5f\u968f\u4e4b\u6d88\u5931\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u50cf<strong>\u83dc\u8c31<\/strong>\uff1a\u628a\u83dc\u8c31\u5199\u8fdb\u7b14\u8bb0\u672c\uff08def \u5b9a\u4e49\uff09\u4e0d\u7b49\u4e8e\u83dc\u505a\u597d\u4e86\uff1b\u7167\u7740\u83dc\u8c31\u505a\u4e00\u904d\uff08\u8c03\u7528\uff09\u624d\u51fa\u83dc\u3002\u540c\u4e00\u672c\u83dc\u8c31\u53ef\u4ee5\u88ab\u4e0d\u540c\u7684\u4eba\u3001\u4e0d\u540c\u7684\u65f6\u95f4\u53cd\u590d\u4f7f\u7528\u2014\u2014\u8fd9\u5c31\u662f\u300c\u4e00\u6b21\u7f16\u5199\u3001\u5230\u5904\u8c03\u7528\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u624b\u5199\u4e09\u904d vs \u51fd\u6570\u5199\u4e00\u904d\u8c03\u4e09\u904d<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def say_hello():\n    print(\"\u4f60\u597d\uff01\")\n\nsay_hello()\nsay_hello()\nsay_hello()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f60\u597d\uff01\n\u4f60\u597d\uff01\n\u4f60\u597d\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u4f53\u53ea\u5199\u4e00\u4efd\uff0c\u60f3\u8c03\u7528\u591a\u5c11\u6b21\u90fd\u884c\u2014\u2014\u8fd9\u5c31\u662f\u590d\u7528\u7684\u610f\u4e49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u51fd\u6570\u4f53\u53ef\u4ee5\u6709\u591a\u884c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def print_plan():\n    print(\"\u4eca\u5929\u7684\u5b66\u4e60\u8ba1\u5212\uff1a\")\n    print(\"1. \u590d\u4e60\u5217\u8868\")\n    print(\"2. \u5b66\u51fd\u6570\")\n\nprint_plan()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u4e3a\u4ec0\u4e48\u7528\u51fd\u6570<\/strong>\uff1a\u4e00\u662f<strong>\u590d\u7528<\/strong>\uff08\u540c\u4e00\u6bb5\u903b\u8f91\u53ea\u5199\u4e00\u6b21\uff09\uff1b\u4e8c\u662f<strong>\u5206\u5de5<\/strong>\uff08\u4e00\u4e2a\u5927\u95ee\u9898\u62c6\u6210\u51e0\u4e2a\u5c0f\u51fd\u6570\uff0c\u6bcf\u4e2a\u53ea\u7ba1\u4e00\u4ef6\u4e8b\uff09\uff1b\u4e09\u662f<strong>\u597d\u6539<\/strong>\uff08\u8981\u6539\u903b\u8f91\u53ea\u6539\u4e00\u4e2a\u5730\u65b9\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>def say_hello()<\/code> \u884c\u5c3e\u5fd8\u4e86\u5192\u53f7 \u2192 \u95ee\u9898\uff1a\u62a5 <code>SyntaxError: invalid syntax<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1adef \u884c\u5c3e\u5fc5\u987b\u6709\u5192\u53f7\uff1a<code>def say_hello():<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8c03\u7528\u5199\u5728\u5b9a\u4e49\u4e4b\u524d\uff08<code>say_hello()<\/code> \u653e\u7b2c\u4e00\u884c\uff09 \u2192 \u95ee\u9898\uff1a\u62a5 <code>NameError<\/code>\uff0c\u56e0\u4e3a def \u8fd8\u6ca1\u6267\u884c\u3001\u540d\u5b57\u8fd8\u4e0d\u5b58\u5728 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u5b9a\u4e49\u3001\u540e\u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>f = say_hello<\/code> \u8bef\u5199\u6210 <code>f = say_hello()<\/code> \u2192 \u95ee\u9898\uff1a\u524d\u8005\u62ff\u5230\u51fd\u6570\u672c\u8eab\uff1b\u540e\u8005\u6267\u884c\u51fd\u6570\u5e76\u628a\u8fd4\u56de\u503c\uff08None\uff09\u8d4b\u7ed9 f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u4f20\u51fd\u6570\u672c\u8eab\u4e0d\u5199\u62ec\u53f7\uff1b\u60f3\u62ff\u7ed3\u679c\u624d\u5199\u62ec\u53f7\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1adef \u767b\u8bb0\u4e0d\u52a8\u624b\uff0c\u62ec\u53f7\u8c03\u7528\u624d\u6267\u884c\uff1b\u540d\u5b57\u7ed1\u5b9a\u51fd\u6570\u4f53\uff0c\u4e00\u6b21\u7f16\u5199\u5904\u5904\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 \u53c2\u6570 \u2014\u2014 \u5b9e\u53c2\u6309\u4f4d\u7f6e\u300c\u5bf9\u53f7\u5165\u5ea7\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u6570<\/strong>\uff08parameter\uff09\u662f\u51fd\u6570\u7684\u300c\u5165\u53e3\u300d\uff1a\u5b9a\u4e49\u65f6\u62ec\u53f7\u91cc\u5199\u7684\u540d\u5b57\u662f<strong>\u5f62\u53c2<\/strong>\uff08\u5360\u4f4d\u7b26\uff09\uff0c\u8c03\u7528\u65f6\u4f20\u8fdb\u53bb\u7684\u503c\u662f<strong>\u5b9e\u53c2<\/strong>\uff08\u771f\u6570\u636e\uff09\u3002\u6ca1\u6709\u53c2\u6570\u7684\u51fd\u6570\u53ea\u80fd\u505a\u56fa\u5b9a\u7684\u4e8b\uff0c\u6709\u4e86\u53c2\u6570\uff0c\u540c\u4e00\u4e2a\u51fd\u6570\u624d\u80fd\u5904\u7406\u4e0d\u540c\u7684\u6570\u636e\u2014\u2014\u8fd9\u662f\u51fd\u6570\u901a\u7528\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528 <code>greet(\"\u5c0f\u660e\")<\/code> \u65f6\uff0cPython \u6309<strong>\u4f4d\u7f6e\u4e00\u4e00\u5bf9\u5e94<\/strong>\uff1a\u7b2c 1 \u4e2a\u5b9e\u53c2\u7ed9\u7b2c 1 \u4e2a\u5f62\u53c2\uff0c\u7b2c 2 \u4e2a\u5b9e\u53c2\u7ed9\u7b2c 2 \u4e2a\u5f62\u53c2\u3002\u5f62\u53c2\u5728\u51fd\u6570\u6267\u884c\u65f6\u6210\u4e3a\u300c\u5c40\u90e8\u540d\u5b57\u300d\uff0c\u6307\u5411\u5b9e\u53c2\u4f20\u5165\u7684\u5bf9\u8c61\u3002\u6ce8\u610f Python \u4f20\u7684\u662f\u300c\u5bf9\u8c61\u7684\u5f15\u7528\u300d\u2014\u2014\u5f62\u53c2\u548c\u5b9e\u53c2\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff08\u597d\u6bd4\u628a\u540c\u4e00\u5f20\u5feb\u9012\u5355\u53f7\u6284\u4e86\u4e00\u4efd\u9012\u7ed9\u51fd\u6570\uff0c\u5355\u53f7\u6307\u5411\u540c\u4e00\u4e2a\u5305\u88f9\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u6570\u50cf<strong>\u70b9\u9910\u5355<\/strong>\uff1a\u83dc\u5355\u4e0a\u5199\u300c\u6c49\u5821\uff08\u5939\u4ec0\u4e48\u3001\u52a0\u4e0d\u52a0\u8fa3\uff09\u300d\u662f\u5f62\u53c2\uff1b\u4f60\u4e0b\u5355\u5199\u300c\u6c49\u5821\u5939\u725b\u8089\u3001\u52a0\u8fa3\u300d\u662f\u5b9e\u53c2\u3002\u540e\u53a8\u6309\u4f60\u586b\u7684\u5355\u5b50\u505a\u2014\u2014\u6bcf\u4e00\u9879\u90fd\u8981\u5bf9\u4e0a\uff0c\u5199\u9519\u4f4d\u7f6e\u3001\u6f0f\u5199\u4e00\u9879\uff0c\u83dc\u5c31\u505a\u4e0d\u5bf9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u591a\u4e2a\u53c2\u6570\u6309\u987a\u5e8f\u4e00\u4e00\u5bf9\u5e94<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def describe(name, age):\n    print(name, \"\u4eca\u5e74\", age, \"\u5c81\")\n\ndescribe(\"\u5c0f\u660e\", 18)\ndescribe(\"\u5c0f\u7ea2\", 17)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c0f\u660e \u4eca\u5e74 18 \u5c81\n\u5c0f\u7ea2 \u4eca\u5e74 17 \u5c81\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528\u65f6\u5b9e\u53c2<strong>\u6309\u4f4d\u7f6e\u914d\u5bf9<\/strong>\u4f20\u7ed9\u5f62\u53c2\uff1a<code>\"\u5c0f\u660e\"<\/code> \u7ed9 name\u3001<code>18<\/code> \u7ed9 age\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5f62\u53c2\u548c\u5168\u5c40\u53d8\u91cf\u540c\u540d\u4e5f\u4e92\u4e0d\u5e72\u6270<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def show(x):\n    print(\"\u51fd\u6570\u5185 x =\", x)\n\nx = 100          # \u5168\u5c40\u7684 x\nshow(7)          # \u4f20\u5165\u7684 7 \u53ea\u7ed9\u51fd\u6570\u91cc\u7684 x\nprint(\"\u51fd\u6570\u5916 x =\", x)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u51fd\u6570\u5185 x = 7\n\u51fd\u6570\u5916 x = 100\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5185\u7684 x \u662f\u5f62\u53c2\uff08\u5c40\u90e8\u540d\u5b57\uff09\uff0c\u548c\u51fd\u6570\u5916\u7684\u5168\u5c40 x \u53ea\u662f<strong>\u540c\u540d\u4e0d\u540c\u7269<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u5b9e\u53c2\u4e2a\u6570\u548c\u5f62\u53c2\u4e2a\u6570\u5fc5\u987b\u4e00\u81f4\uff08\u9519\u8bef\u793a\u8303\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(a, b):\n    return a + b\n\n# add(3)          # \u62a5\u9519\uff01TypeError: add() missing 1 required positional argument: 'b'\n# add(3, 5, 7)    # \u62a5\u9519\uff01TypeError: add() takes 2 positional arguments but 3 were given\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u62a5\u9519\u4fe1\u606f\u91cc\u4f1a\u660e\u786e\u544a\u8bc9\u4f60\u300c\u7f3a\u4e86\u54ea\u4e2a\u53c2\u6570\u300d\u6216\u300c\u591a\u4e86\u51e0\u4e2a\u53c2\u6570\u300d\u2014\u2014\u5b66\u4f1a\u8bfb\u62a5\u9519\u4fe1\u606f\uff0c\u5c31\u80fd\u81ea\u5df1\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>def greet(name, age)<\/code> \u8c03\u7528\u5199\u6210 <code>greet(18, \"\u5c0f\u660e\")<\/code> \u2192 \u95ee\u9898\uff1a\u53c2\u6570\u987a\u5e8f\u53cd\u4e86\uff0cname \u62ff\u5230 18\uff0c\u903b\u8f91\u9519\u4e71\u4e14\u4e0d\u62a5\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5b9e\u53c2\u987a\u5e8f\u5fc5\u987b\u4e0e\u5f62\u53c2\u5b9a\u4e49\u987a\u5e8f\u4e00\u81f4\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8c03\u7528\u65f6\u5c11\u4f20\u4e00\u4e2a\u53c2\u6570 \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: missing 1 required positional argument<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u68c0\u67e5\u8c03\u7528\u5904\u53c2\u6570\u4e2a\u6570\u662f\u5426\u4e0e\u5b9a\u4e49\u4e00\u81f4\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5f62\u53c2\u540d\u5199\u9519\uff08<code>def greet(nmae)<\/code>\uff0c\u51fd\u6570\u4f53\u5185\u7528 <code>name<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u62a5 <code>NameError<\/code>\uff0c\u51fd\u6570\u4f53\u5185\u627e\u4e0d\u5230\u8fd9\u4e2a\u540d\u5b57 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5f62\u53c2\u540d\u5b9a\u4e49\u548c\u4f7f\u7528\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5f62\u53c2\u5360\u4f4d\u5b9e\u53c2\u503c\uff0c\u8c03\u7528\u6309\u4f4d\u5bf9\u53f7\u5750\uff1b\u4e2a\u6570\u987a\u5e8f\u90fd\u5bf9\u9f50\uff0c\u51fd\u6570\u624d\u80fd\u8dd1\u5f97\u5bf9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 return \u2014\u2014 \u51fd\u6570\u4e0e\u8c03\u7528\u8005\u7684\u300c\u4ea4\u63a5\u68d2\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>return<\/code> \u662f\u51fd\u6570<strong>\u628a\u7ed3\u679c\u4ea4\u8fd8\u7ed9\u8c03\u7528\u8005<\/strong>\u7684\u8bed\u53e5\u3002<code>print<\/code> \u53ea\u662f\u628a\u5185\u5bb9\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u770b\u5b8c\u5c31\u6ca1\u4e86\uff1b<code>return<\/code> \u628a\u7ed3\u679c\u771f\u6b63\u300c\u9012\u300d\u51fa\u6765\uff0c\u8c03\u7528\u5904\u80fd\u628a\u5b83\u5b58\u8fdb\u53d8\u91cf\u3001\u7ee7\u7eed\u53c2\u4e0e\u8ba1\u7b97\u3002\u60f3\u8ba9\u51fd\u6570\u300c\u6709\u7528\u300d\uff0c\u5341\u6709\u516b\u4e5d\u8981\u9760 return\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u8c03\u7528\u4f1a\u521b\u5efa\u72ec\u7acb\u7684\u6267\u884c\u7a7a\u95f4\uff0c\u51fd\u6570\u91cc\u7684\u8ba1\u7b97\u7ed3\u679c\u9ed8\u8ba4\u5e26\u4e0d\u51fa\u6765\u3002<code>return<\/code> \u7684\u4f5c\u7528\u5c31\u662f\u628a\u4e00\u4e2a\u503c\u4ece\u51fd\u6570\u5185\u90e8\u300c\u9012\u4ea4\u300d\u56de\u8c03\u7528\u8005\u7684\u6267\u884c\u7a7a\u95f4\uff0c\u7136\u540e<strong>\u7acb\u5373\u7ed3\u675f<\/strong>\u51fd\u6570\u3002\u5982\u679c\u51fd\u6570\u6ca1\u6709 return\uff0cPython \u4f1a\u81ea\u52a8\u8865\u4e00\u4e2a\u300c\u8fd4\u56de None\u300d\u2014\u2014None \u662f\u8868\u793a\u300c\u4ec0\u4e48\u90fd\u6ca1\u6709\u300d\u7684\u7279\u6b8a\u503c\u3002\u53e6\u5916 <code>return a, b<\/code> \u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u4ea4\u56de\u591a\u4e2a\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u98df\u5802\u6253\u996d\u7a97\u53e3\uff1a<strong>print<\/strong> \u662f\u7a97\u53e3\u4e0a\u65b9\u7684\u663e\u793a\u5c4f\u2014\u2014\u5927\u5bb6\u90fd\u770b\u5f97\u89c1\uff0c\u4f46\u4f60\u62ff\u4e0d\u5230\uff1b<strong>return<\/strong> \u662f\u6253\u996d\u963f\u59e8\u4ece\u7a97\u53e3\u9012\u51fa\u6765\u7684\u9910\u76d8\u2014\u2014<strong>\u53ea\u6709\u9012\u5230\u4f60\u624b\u91cc\uff08\u5b58\u8fdb\u53d8\u91cf\uff09\uff0c\u4f60\u624d\u80fd\u5403\uff08\u7ee7\u7eed\u4f7f\u7528\uff09<\/strong>\u3002\u6ca1\u6709 return \u7684\u51fd\u6570\u5c31\u50cf\u53ea\u7ed9\u4f60\u770b\u4e86\u4e00\u773c\u83dc\uff0c\u4ec0\u4e48\u4e5f\u6ca1\u9012\u51fa\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>return \u7684\u7ed3\u679c\u53ef\u4ee5\u5b58\u8fdb\u53d8\u91cf\u7ee7\u7eed\u7528<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(a, b):\n    return a + b\n\nresult = add(3, 5)      # \u628a\u8fd4\u56de\u7684 8 \u63a5\u4f4f\nprint(result)           # 8\nprint(result * 10)      # \u8fd8\u80fd\u7ee7\u7eed\u53c2\u4e0e\u8fd0\u7b97\uff1a80\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>8\n80\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u6bd4\uff1a\u5982\u679c add \u91cc\u7528\u7684\u662f print \u800c\u4e0d\u662f return\uff0cresult \u4f1a\u62ff\u5230 None\uff0c\u7b2c 6 \u884c\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6ca1\u6709 return \u7684\u51fd\u6570\uff0c\u8c03\u7528\u7ed3\u679c\u662f None<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def show():\n    print(\"\u6211\u5728\u6253\u5370\")\n\nr = show()\nprint(r)          # None\nprint(r is None)  # True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6211\u5728\u6253\u5370\nNone\nTrue\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>is None<\/code> \u662f\u5224\u65ad\u300c\u662f\u4e0d\u662f None\u300d\u7684\u6807\u51c6\u5199\u6cd5\u3002\u8c03\u8bd5\u65f6\u53d1\u73b0\u300c\u51fd\u6570\u7ed3\u679c\u7528\u4e0d\u4e86\u300d\uff0c\u5148\u68c0\u67e5\u51fd\u6570\u91cc\u6709\u6ca1\u6709\u5199 return\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>return \u8fd4\u56de\u591a\u4e2a\u503c\uff08\u81ea\u52a8\u6253\u5305\u6210\u5143\u7ec4\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_min_max(nums):\n    return min(nums), max(nums)\n\na, b = get_min_max(&#91;3, 8, 1, 9])   # \u4e00\u6b21\u63a5\u4f4f\u4e24\u4e2a\u7ed3\u679c\nprint(\"\u6700\u5c0f\u503c\uff1a\", a)\nprint(\"\u6700\u5927\u503c\uff1a\", b)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6700\u5c0f\u503c\uff1a 1\n\u6700\u5927\u503c\uff1a 9\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>return min(nums), max(nums)<\/code> \u770b\u8d77\u6765\u8fd4\u56de\u4e24\u4e2a\u503c\uff0c\u5b9e\u9645\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4 <code>(1, 9)<\/code>\uff1b<code>a, b = ...<\/code> \u662f<strong>\u5143\u7ec4\u89e3\u5305<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4 <strong>\u9047\u5230 return \u51fd\u6570\u7acb\u5373\u7ed3\u675f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def check(score):\n    if score &lt; 0:\n        return \"\u5206\u6570\u4e0d\u5408\u6cd5\"   # \u63d0\u524d\u8fd4\u56de\uff0c\u540e\u9762\u7684\u4ee3\u7801\u4e0d\u6267\u884c\n    return \"\u5206\u6570\uff1a \" + str(score)\n\nprint(check(-5))\nprint(check(85))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5206\u6570\u4e0d\u5408\u6cd5\n\u5206\u6570\uff1a 85\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 print \u4ee3\u66ff return\uff08<code>def add(a, b): print(a + b)<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u8c03\u7528\u5904\u62ff\u5230 None\uff0c\u7ed3\u679c\u65e0\u6cd5\u53c2\u4e0e\u8ba1\u7b97 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u7ed3\u679c\u5c31 <code>return a + b<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>return a, b<\/code> \u4f46\u53ea\u7528 <code>x = get_min_max(...)<\/code> \u63a5\u6536 \u2192 \u95ee\u9898\uff1ax \u62ff\u5230\u7684\u662f\u5143\u7ec4 (a, b)\uff0c\u4e0d\u662f\u4e24\u4e2a\u72ec\u7acb\u53d8\u91cf \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>a, b = get_min_max(...)<\/code> \u89e3\u5305\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u4e86\u5199 return \u2192 \u95ee\u9898\uff1a\u51fd\u6570\u300c\u7b97\u5b8c\u5c31\u4e22\u300d\uff0c\u8c03\u7528\u5904\u62ff\u5230 None \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u68c0\u67e5\u51fd\u6570\u662f\u5426\u6709\u8fd4\u56de\u503c\u9700\u6c42\uff0c\u9700\u8981\u5c31\u5199 return\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aprint \u53ea\u5c55\u793a\uff0creturn \u771f\u9012\u51fa\uff1breturn \u4e00\u5230\u5c31\u6536\u5de5\uff0c\u6ca1\u5199\u9ed8\u8ba4\u8fd4 None\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 \u9ed8\u8ba4\u53c2\u6570\u4e0e\u53d8\u91cf\u4f5c\u7528\u57df \u2014\u2014 \u4e24\u4e2a\u7ecf\u5178\u5751\u63d0\u524d\u907f\u5f00<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9ed8\u8ba4\u53c2\u6570<\/strong>\uff08default parameter\uff09\u8ba9\u67d0\u4e9b\u53c2\u6570\u300c\u4e0d\u4f20\u4e5f\u6709\u503c\u300d\uff1b<strong>\u4f5c\u7528\u57df<\/strong>\uff08scope\uff09\u51b3\u5b9a\u300c\u53d8\u91cf\u5728\u54ea\u4e9b\u5730\u65b9\u53ef\u89c1\u300d\u3002\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f\u65b0\u624b\u5199\u51fd\u6570\u65f6\u8e29\u5751\u6700\u591a\u7684\u4e24\u4e2a\u5730\u65b9\uff0c\u4eca\u5929\u4e00\u6b21\u8bb2\u900f\uff0c\u540e\u9762\u80fd\u5c11\u6389\u5f88\u591a\u5934\u53d1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u5728 <strong>def \u5b9a\u4e49\u7684\u90a3\u4e00\u523b\u53ea\u8ba1\u7b97\u4e00\u6b21<\/strong>\uff0c\u4e4b\u540e\u6bcf\u6b21\u8c03\u7528\u5171\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u2014\u2014\u5982\u679c\u9ed8\u8ba4\u503c\u662f\u5217\u8868\u3001\u5b57\u5178\u8fd9\u7c7b\u300c\u53ef\u53d8\u5bf9\u8c61\u300d\uff0c\u591a\u6b21\u8c03\u7528\u4f1a\u4e92\u76f8\u6c61\u67d3\u3002\u4f5c\u7528\u57df\u65b9\u9762\uff0cPython \u67e5\u627e\u53d8\u91cf\u6309 <strong>LEGB<\/strong> \u987a\u5e8f\uff1a\u5c40\u90e8\uff08Local\uff09\u2192 \u5d4c\u5957\u51fd\u6570\uff08Enclosing\uff0cDay 9 \u624d\u63a5\u89e6\uff09\u2192 \u5168\u5c40\uff08Global\uff09\u2192 \u5185\u7f6e\uff08Built-in\uff09\u3002\u51fd\u6570\u91cc<strong>\u8bfb\u53d6<\/strong>\u5168\u5c40\u53d8\u91cf\u6ca1\u95ee\u9898\uff1b\u4f46\u51fd\u6570\u91cc<strong>\u8d4b\u503c<\/strong>\uff08<code>count = count + 1<\/code>\uff09\u65f6\uff0cPython \u4f1a\u8ba4\u4e3a count \u662f\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u2014\u2014\u60f3\u6539\u5168\u5c40\u5fc5\u987b\u663e\u5f0f\u58f0\u660e <code>global<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u53c2\u6570\u50cf<strong>\u9152\u5e97\u623f\u95f4\u7684\u9ed8\u8ba4\u914d\u7f6e<\/strong>\uff1a\u4f60\u4e0d\u7279\u522b\u8981\u6c42\uff0c\u524d\u53f0\u5c31\u6309\u300c\u5927\u5e8a\u623f\u3001\u65e0\u70df\u300d\u5b89\u6392\uff1b\u4f60\u63d0\u4e86\u8981\u6c42\uff0c\u5c31\u6309\u4f60\u7684\u6765\u3002\u4f5c\u7528\u57df\u50cf<strong>\u516c\u53f8\u7684\u50a8\u7269\u67dc<\/strong>\uff1a\u5927\u5385\u7684\u516c\u5171\u67dc\uff08\u5168\u5c40\uff09\u8c01\u90fd\u80fd\u770b\uff1b\u5de5\u4f4d\u7684\u79c1\u4eba\u62bd\u5c49\uff08\u5c40\u90e8\uff09\u53ea\u6709\u4f60\u81ea\u5df1\u80fd\u7528\u2014\u2014\u60f3\u52a8\u516c\u5171\u67dc\u91cc\u7684\u4e1c\u897f\uff08\u6539\u5168\u5c40\u53d8\u91cf\uff09\uff0c\u5f97\u5148\u548c\u7ba1\u7406\u5458\u6253\u62db\u547c\uff08\u5199 global\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u9ed8\u8ba4\u53c2\u6570\u2014\u2014\u4e0d\u4f20\u7528\u9ed8\u8ba4\uff0c\u4f20\u4e86\u7528\u4f60\u7684<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name, greeting=\"\u4f60\u597d\"):\n    print(greeting, name)\n\ngreet(\"\u5c0f\u660e\")              # \u4e0d\u4f20 greeting\uff0c\u7528\u9ed8\u8ba4\u300c\u4f60\u597d\u300d\ngreet(\"\u5c0f\u7ea2\", \"\u65e9\u4e0a\u597d\")     # \u4f20\u4e86\u5c31\u7528\u4f20\u5165\u7684\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f60\u597d \u5c0f\u660e\n\u65e9\u4e0a\u597d \u5c0f\u7ea2\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u5fc5\u987b\u5199\u5728\u6ca1\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u540e\u9762<\/strong>\uff08<code>def greet(name, greeting=\"\u4f60\u597d\")<\/code> \u5bf9\uff1b<code>def greet(name=\"\u5c0f\u660e\", greeting)<\/code> \u4f1a\u76f4\u63a5\u62a5\u8bed\u6cd5\u9519\u8bef\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u53ef\u53d8\u9ed8\u8ba4\u503c\u9677\u9631\uff08\u9519\u8bef\u793a\u8303\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_item(item, bag=&#91;]):\n    bag.append(item)\n    return bag\n\nprint(add_item(\"\u82f9\u679c\"))    # \u7b2c\u4e00\u6b21\uff1a&#91;'\u82f9\u679c']\nprint(add_item(\"\u9999\u8549\"))    # \u671f\u671b &#91;'\u9999\u8549']\uff0c\u5b9e\u9645 &#91;'\u82f9\u679c', '\u9999\u8549']\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'\u82f9\u679c']\n&#91;'\u82f9\u679c', '\u9999\u8549']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>bag=[]<\/code> \u8fd9\u4e2a\u5217\u8868\u5728 <strong>def \u5b9a\u4e49\u65f6\u53ea\u521b\u5efa\u4e00\u6b21<\/strong>\uff0c\u6240\u6709\u6ca1\u4f20 bag \u7684\u8c03\u7528\u5171\u7528\u5b83\u3002\u6b63\u786e\u505a\u6cd5\u662f\u9ed8\u8ba4\u503c\u7528 None\uff0c\u51fd\u6570\u91cc\u518d\u521b\u5efa\u65b0\u5217\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_item(item, bag=None):\n    if bag is None:\n        bag = &#91;]\n    bag.append(item)\n    return bag\n\nprint(add_item(\"\u82f9\u679c\"))    # &#91;'\u82f9\u679c']\nprint(add_item(\"\u9999\u8549\"))    # &#91;'\u9999\u8549']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89c4\u5219\uff1a\u9ed8\u8ba4\u503c\u53ea\u7528\u4e0d\u53ef\u53d8\u7c7b\u578b\uff08\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001None\uff09\uff0c\u7edd\u4e0d\u7528\u5217\u8868\/\u5b57\u5178\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u4f5c\u7528\u57df\u2014\u2014\u51fd\u6570\u91cc\u8bfb\u5168\u5c40\u3001\u5199\u5c40\u90e8\uff08\u540c\u540d\u906e\u853d\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10              # \u5168\u5c40 x\n\ndef show():\n    x = 20          # \u8fd9\u662f\u51fd\u6570\u91cc\u7684\u5c40\u90e8 x\uff0c\u548c\u5168\u5c40 x \u6ca1\u5173\u7cfb\n    print(\"\u51fd\u6570\u5185 x =\", x)\n\nshow()              # \u51fd\u6570\u5185 x = 20\nprint(\"\u51fd\u6570\u5916 x =\", x)   # \u5168\u5c40 x \u6ca1\u88ab\u6539\u52a8\uff0c\u8fd8\u662f 10\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u51fd\u6570\u5185 x = 20\n\u51fd\u6570\u5916 x = 10\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u91cc <code>x = 20<\/code> \u521b\u5efa\u7684\u662f\u5c40\u90e8 x\uff0c\u53ea\u662f\u300c\u906e\u853d\u300d\u4e86\u5168\u5c40 x \u7684\u540d\u5b57\uff0c\u5e76\u4e0d\u4f1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002\u60f3\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u5fc5\u987b\u52a0 <code>global<\/code> \u58f0\u660e\uff0c\u4f46<strong>\u6700\u4f73\u5b9e\u8df5\u662f\u5c3d\u91cf\u522b\u7528 global<\/strong>\u2014\u2014\u7528\u53c2\u6570\u4f20\u8fdb\u53bb\u3001\u7528 return \u4f20\u51fa\u6765\uff0c\u51fd\u6570\u624d\u72ec\u7acb\u3001\u597d\u7ef4\u62a4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>def f(a=1, b):<\/code> \u9ed8\u8ba4\u53c2\u6570\u5199\u5728\u666e\u901a\u53c2\u6570\u524d\u9762 \u2192 \u95ee\u9898\uff1a\u76f4\u63a5\u62a5 <code>SyntaxError: non-default argument follows default argument<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9ed8\u8ba4\u53c2\u6570\u4e00\u5f8b\u5199\u5728\u666e\u901a\u53c2\u6570\u540e\u9762\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>def f(lst=[])<\/code> \u4e14\u51fd\u6570\u91cc\u4fee\u6539 lst \u2192 \u95ee\u9898\uff1a\u591a\u6b21\u8c03\u7528\u5171\u7528\u540c\u4e00\u4e2a\u5217\u8868\uff0c\u6570\u636e\u4e92\u76f8\u6c61\u67d3 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9ed8\u8ba4\u503c\u5199 None\uff0c\u51fd\u6570\u91cc <code>if lst is None: lst = []<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u51fd\u6570\u91cc\u5199 <code>count = count + 1<\/code> \u60f3\u6539\u5168\u5c40 count \u2192 \u95ee\u9898\uff1a\u62a5 <code>UnboundLocalError<\/code>\uff08\u6216\u884c\u4e3a\u5f02\u5e38\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u51fd\u6570\u5185\u58f0\u660e <code>global count<\/code>\uff0c\u6216\u6539\u7528\u53c2\u6570 + return\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a\u51fd\u6570\u91cc\u7684\u5c40\u90e8\u53d8\u91cf\u5728\u51fd\u6570\u5916\u8fd8\u80fd\u7528 \u2192 \u95ee\u9898\uff1a\u62a5 <code>NameError<\/code>\uff0c\u5c40\u90e8\u53d8\u91cf\u968f\u51fd\u6570\u7ed3\u675f\u800c\u9500\u6bc1 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u7ed3\u679c\u5c31\u7528 return \u4f20\u51fa\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u9ed8\u8ba4\u53c2\u6570\u653e\u540e\u9762\uff0c\u53ef\u53d8\u9ed8\u8ba4\u7528 None\uff1b\u5168\u5c40\u4fee\u6539\u52a0 global\uff0c\u80fd\u4e0d\u7528\u5c31\u5c3d\u91cf\u4e0d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u52a8\u624b\u5b9e\u8df5\uff1a\u6570\u5b66\u5de5\u5177\u51fd\u6570\u5e93<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u628a\u5e38\u7528\u7684\u6570\u5b66\u8fd0\u7b97\u5c01\u88c5\u6210\u4e00\u4e2a\u4e2a\u5c0f\u51fd\u6570\uff0c\u653e\u8fdb\u4e00\u4e2a\u6587\u4ef6\uff0c\u8fd9\u5c31\u662f\u300c\u51fd\u6570\u5e93\u300d\u7684\u96cf\u5f62\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6570\u5b66\u5de5\u5177\u51fd\u6570\u5e93\nimport math\n\n\ndef add(a, b):\n    \"\"\"\u4e24\u6570\u76f8\u52a0\"\"\"\n    return a + b\n\n\ndef multiply(a, b):\n    \"\"\"\u4e24\u6570\u76f8\u4e58\"\"\"\n    return a * b\n\n\ndef average(nums):\n    \"\"\"\u6c42\u5217\u8868\u7684\u5e73\u5747\u503c\uff08\u7a7a\u5217\u8868\u8fd4\u56de 0\uff09\"\"\"\n    if len(nums) == 0:\n        return 0\n    return sum(nums) \/ len(nums)\n\n\ndef factorial(n):\n    \"\"\"\u8ba1\u7b97 n \u7684\u9636\u4e58\uff081*2*3*...*n\uff09\"\"\"\n    result = 1\n    for i in range(2, n + 1):\n        result = result * i\n    return result\n\n\ndef circle_area(radius):\n    \"\"\"\u8ba1\u7b97\u5706\u7684\u9762\u79ef\uff08\u03c0r\u00b2\uff09\"\"\"\n    return math.pi * radius * radius\n\n\n# \u8c03\u7528\u6d4b\u8bd5\nprint(add(3, 4))                    # 7\nprint(multiply(3, 4))               # 12\nprint(average(&#91;85, 92, 78]))        # 85.0\nprint(average(&#91;]))                  # 0\nprint(factorial(5))                 # 120\nprint(round(circle_area(2), 2))     # 12.57\uff08\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff09\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b8c\u6210\u6807\u51c6<\/strong>\uff1a\u6bcf\u4e2a\u51fd\u6570\u90fd\u80fd\u72ec\u7acb\u8c03\u7528\u3001\u8f93\u51fa\u7b26\u5408\u9884\u671f\uff1b\u518d\u81ea\u5df1\u7ed9\u51fd\u6570\u5e93\u52a0\u4e00\u4e2a\u300c\u6c42\u5217\u8868\u6700\u5927\u503c\u300d\u7684\u51fd\u6570\uff08\u63d0\u793a\uff1a\u51fd\u6570\u5185\u53ef\u4ee5\u7528\u5faa\u73af\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528 <code>max<\/code>\uff09\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5c0f\u77e5\u8bc6\uff1a<code>calc(price, rate=0.1)<\/code> \u91cc\u7528 <code>round(price * (1 + rate), 2)<\/code> \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u2014\u2014\u8ba1\u7b97\u673a\u7528\u4e8c\u8fdb\u5236\u5b58\u5c0f\u6570\uff0c<code>0.1<\/code> \u8fd9\u79cd\u5c0f\u6570\u65e0\u6cd5\u7cbe\u786e\u8868\u793a\uff0c\u6240\u4ee5 <code>100 * 1.1<\/code> \u7684\u771f\u5b9e\u7ed3\u679c\u662f <code>110.00000000000001<\/code>\uff0c\u7528 <code>round(..., 2)<\/code> \u7ed3\u679c\u624d\u548c\u65e5\u5e38\u8ba1\u7b97\u4e00\u81f4\uff0c\u5199\u91d1\u989d\u8ba1\u7b97\u90fd\u5efa\u8bae\u8fd9\u4e48\u505a\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u662f\u4ec0\u4e48\uff1a<code>def<\/code> \u5b9a\u4e49\u3001<code>\u51fd\u6570\u540d()<\/code> \u8c03\u7528\uff0c\u5b9a\u4e49\u4e0d\u6267\u884c\u3001\u8c03\u7528\u624d\u6267\u884c\uff1b\u51fd\u6570\u672c\u8eab\u4e5f\u662f\u300c\u503c\u300d\uff0c\u53ef\u4ee5\u8d4b\u503c\u7ed9\u53d8\u91cf\n\u51fd\u6570\u7684\u5e95\u5c42\uff1adef \u628a\u51fd\u6570\u4f53\u6253\u5305\u6210\u5bf9\u8c61\uff0c\u8c03\u7528\u65f6\u624d\u521b\u5efa\u72ec\u7acb\u6267\u884c\u7a7a\u95f4\uff08\u6808\u5e27\uff09\n\u53c2\u6570\uff1a\u5f62\u53c2\u662f\u5360\u4f4d\u7b26\u3001\u5b9e\u53c2\u662f\u771f\u5b9e\u6570\u636e\uff0c\u6309\u4f4d\u7f6e\u4e00\u4e00\u5bf9\u5e94\uff0c\u4e2a\u6570\u5fc5\u987b\u4e00\u81f4\n\u8fd4\u56de\u503c\uff1aprint \u53ea\u663e\u793a\u3001return \u771f\u9012\u51fa\uff1breturn \u4e00\u6267\u884c\u51fd\u6570\u7acb\u5373\u7ed3\u675f\uff1b\u6ca1\u5199 return \u8fd4\u56de None\uff1b<code>return a, b<\/code> \u8fd4\u56de\u5143\u7ec4\u53ef\u89e3\u5305\n\u9ed8\u8ba4\u53c2\u6570\uff1a\u5199\u5728\u666e\u901a\u53c2\u6570\u540e\u9762\uff0c\u4e0d\u4f20\u6709\u9ed8\u8ba4\u503c\uff1b\u53ef\u53d8\u9ed8\u8ba4\u503c\uff08\u5217\u8868\/\u5b57\u5178\uff09\u5fc5\u987b\u7528 None \u4ee3\u66ff\n\u4f5c\u7528\u57df\uff1a\u5c40\u90e8\u53d8\u91cf\u51fd\u6570\u5916\u8bbf\u95ee\u4e0d\u5230\uff1b\u51fd\u6570\u5185\u8bfb\u53d6\u5168\u5c40\u6ca1\u95ee\u9898\uff0c\u4fee\u6539\u5168\u5c40\u8981 <code>global<\/code>\uff0c\u6700\u597d\u7528\u53c2\u6570 + return \u4ee3\u66ff\n\u5b8c\u6210\u6570\u5b66\u5de5\u5177\u51fd\u6570\u5e93\uff0c\u81f3\u5c11\u542b 5 \u4e2a\u51fd\u6570\u5e76\u901a\u8fc7\u81ea\u6d4b<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>SyntaxError: invalid syntax<\/code><\/td><td>def \u884c\u5c3e\u5fd8\u4e86\u5192\u53f7\uff0c\u6216\u51fd\u6570\u540d\/\u53c2\u6570\u540d\u6709\u975e\u6cd5\u5b57\u7b26<\/td><td>\u68c0\u67e5 <code>def \u51fd\u6570\u540d(\u53c2\u6570):<\/code> \u7684\u5192\u53f7\u548c\u547d\u540d<\/td><\/tr><tr><td><code>IndentationError: expected an indented block<\/code><\/td><td>\u51fd\u6570\u4f53\u5fd8\u4e86\u7f29\u8fdb<\/td><td>def \u4e0b\u4e00\u884c\u7edf\u4e00\u7f29\u8fdb 4 \u4e2a\u7a7a\u683c<\/td><\/tr><tr><td><code>NameError: name 'x' is not defined<\/code><\/td><td>\u5728\u51fd\u6570\u5916\u8bbf\u95ee\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf<\/td><td>\u9700\u8981\u7ed3\u679c\u5c31\u7528 return \u4f20\u51fa\u6765\uff0c\u6216\u628a\u53d8\u91cf\u5b9a\u4e49\u5728\u51fd\u6570\u5916<\/td><\/tr><tr><td><code>TypeError: add() missing 1 required positional argument: 'b'<\/code><\/td><td>\u8c03\u7528\u65f6\u5c11\u4f20\u4e86\u4e00\u4e2a\u53c2\u6570<\/td><td>\u68c0\u67e5\u8c03\u7528\u662f\u5426\u4e0e\u5b9a\u4e49\u53c2\u6570\u4e2a\u6570\u4e00\u81f4<\/td><\/tr><tr><td><code>UnboundLocalError: local variable 'count' referenced before assignment<\/code><\/td><td>\u51fd\u6570\u91cc\u7ed9\u5168\u5c40\u53d8\u91cf\u8d4b\u503c\u4f46\u6ca1\u58f0\u660e global<\/td><td>\u5728\u51fd\u6570\u5185\u5148\u5199 <code>global \u53d8\u91cf\u540d<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u51fd\u6570\u662f\u4ec0\u4e48 \u2014\u2014 \u628a\u300c\u590d\u5236\u7c98\u8d34\u300d\u5347\u7ea7\u6210\u300c\u547d\u540d\u590d\u7528\u300d \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u51fd\u6570\uff08function\uff09\u662f\u7ed9\u4e00\u6bb5\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}