{"id":3470,"date":"2023-06-17T19:54:04","date_gmt":"2023-06-17T11:54:04","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3470"},"modified":"2023-06-17T19:54:07","modified_gmt":"2023-06-17T11:54:07","slug":"python%e5%87%bd%e6%95%b0%e7%94%a8%e9%80%94%e3%80%81%e5%ae%9a%e4%b9%89%e6%96%b9%e6%b3%95%e3%80%81%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%e3%80%81%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0%e3%80%81%e9%80%92","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3470","title":{"rendered":"python\u51fd\u6570\u7528\u9014\u3001\u5b9a\u4e49\u65b9\u6cd5\u3001\u53c2\u6570\u7c7b\u578b\u3001\u8c03\u7528\u51fd\u6570\u3001\u9012\u5f52\u51fd\u6570\u3001lambda\u51fd\u6570"},"content":{"rendered":"\n<p><a>python\u51fd\u6570\u7528\u9014\u3001\u5b9a\u4e49\u65b9\u6cd5\u3001\u53c2\u6570\u7c7b\u578b\u3001\u8c03\u7528\u51fd\u6570\u3001\u9012\u5f52\u51fd\u6570\u3001lambda\u51fd\u6570<\/a><a><strong>Python\u51fd\u6570\u7684\u5b9a\u4e49<\/strong><\/a><a>Python\u51fd\u6570\u7684\u53c2\u6570\u6709\u4ee5\u4e0b\u56db\u79cd\u7c7b\u578b\uff1a<\/a><a><strong>\u4e3e\u4f8b\u8bf4\u660e\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/strong><\/a><a>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\uff1f<\/a><a>\u4ec0\u4e48\u662f\u9012\u5f52\u51fd\u6570\uff1f<\/a><a>lambda\u8868\u8fbe\u5f0f<\/a><a>\u5982\u4f55\u5c06\u6ce8\u91caDocstring\u6dfb\u52a0\u5230Python\u51fd\u6570<\/a><a>\u4f7f\u7528<code>main\uff08\uff09<\/code>\u4f5c\u4e3a\u51fd\u6570<\/a><a>\u51fd\u6570\u4e0e\u65b9\u6cd5\u7684\u533a\u522b<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python\u51fd\u6570\u7684\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>\u662f\u6307\u7528def\u5173\u952e\u5b57\u6765\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\uff0c\u7ed9\u5b83\u4e00\u4e2a\u540d\u79f0\u548c\u53c2\u6570\u5217\u8868\uff0c\u7136\u540e\u5728\u7f29\u8fdb\u7684\u4ee3\u7801\u5757\u4e2d\u7f16\u5199\u51fd\u6570\u4f53\uff0c\u53ef\u4ee5\u7528return\u8bed\u53e5\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u3002Python\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5b9e\u73b0\u4e00\u6bb5\u5177\u6709\u7279\u5b9a\u529f\u80fd\u7684\u3001\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u00b3\u2074\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u548c\u62ec\u53f7\u4e2d\u7684\u5b9e\u53c2\u6765\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u6267\u884c\u5b83\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4f60\u80fd\u7ed9\u6211\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f8b\u5b50\u5417?<\/p>\n\n\n\n<p>\u5f53\u7136\u53ef\u4ee5\uff0c\u6211\u4e3a\u4f60\u627e\u5230\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c<br>def add(a, b):<br> &nbsp;# \u8fd4\u56dea\u548cb\u7684\u548c<br> &nbsp;return a + b<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e24\u4e2a\u5b9e\u53c2<br>result = add(3, 5)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fadd\uff0c\u5b83\u6709\u4e24\u4e2a\u53c2\u6570a\u548cb\uff0c\u5b83\u7684\u529f\u80fd\u662f\u8fd4\u56dea\u548cb\u7684\u548c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7add(3, 5)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de8\uff0c\u5e76\u8d4b\u503c\u7ed9result\u53d8\u91cf\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u7528print(result)\u6765\u6253\u5370\u7ed3\u679c\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u548c\u62ec\u53f7\u4e2d\u7684\u5b9e\u53c2\u6765\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u6267\u884c\u5b83\u7684\u529f\u80fd\u00b2\u00b3\u3002\u4e3e\u4f8b\u8bf4\u660ePython\u51fd\u6570\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370\u4e00\u53e5\u95ee\u5019\u8bed<br>def greet(name):<br> &nbsp;# \u6253\u5370\"Hello, name!\"<br> &nbsp;print(\"Hello, \" + name + \"!\")<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2a\u5b9e\u53c2<br>greet(\"Alice\")<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>Hello, Alice!<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fgreet\uff0c\u5b83\u6709\u4e00\u4e2a\u53c2\u6570name\uff0c\u5b83\u7684\u529f\u80fd\u662f\u6253\u5370\u4e00\u53e5\u95ee\u5019\u8bed\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7greet(&#8220;Alice&#8221;)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u6253\u5370&#8221;Hello, Alice!&#8221;\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Python\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python\u51fd\u6570\u7684\u53c2\u6570\u6709\u4ee5\u4e0b\u56db\u79cd\u7c7b\u578b\uff1a<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f4d\u7f6e\u53c2\u6570<\/strong>\uff1a\u6309\u7167\u53c2\u6570\u4f4d\u7f6e\uff0c\u4f9d\u6b21\u4f20\u9012\u53c2\u6570\uff0c\u8fd9\u662f\u6700\u666e\u901a\u7684\u65b9\u5f0f\u00b9\u00b2\u3002<\/li>\n\n\n\n<li><strong>\u5173\u952e\u5b57\u53c2\u6570<\/strong>\uff1a\u5982\u679c\u4e0d\u60f3\u4e25\u683c\u6309\u7167\u987a\u5e8f\u4f20\u9012\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u6309\u5173\u952e\u5b57\u4f20\u9012\u00b9\u00b2\u3002<\/li>\n\n\n\n<li><strong>\u9ed8\u8ba4\u503c\u53c2\u6570<\/strong>\uff1a\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u7ed9\u67d0\u4e2a\u53c2\u6570\u8d4b\u503c\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u5c31\u53eb\u505a\u9ed8\u8ba4\u503c\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u53d8\u53c2\u6570<\/strong>\uff1a\u5f53\u51fd\u6570\u9700\u8981\u5904\u7406\u7684\u53c2\u6570\u4e2a\u6570\u4e0d\u786e\u5b9a\u65f6\uff0c\u53ef\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u3002Python\u4e2d\u6709\u4e24\u79cd\u53ef\u53d8\u53c2\u6570\uff1a\u63a5\u6536\u5143\u7ec4\u7684\u53c2\u6570\u540d\u524d\u52a0\u4e00\u4e2a*\uff0c\u63a5\u6536\u5b57\u5178\u7684\u53c2\u6570\u540d\u524d\u52a0\u4e24\u4e2a**\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e3e\u4f8b\u8bf4\u660e\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/strong><\/h3>\n\n\n\n<p>\u4e00\u3001<strong>\u4f4d\u7f6e\u53c2\u6570<\/strong>\uff1a\u6309\u7167\u53c2\u6570\u4f4d\u7f6e\uff0c\u4f9d\u6b21\u4f20\u9012\u53c2\u6570\uff0c\u8fd9\u662f\u6700\u666e\u901a\u7684\u65b9\u5f0f\u00b2\u00b3\u3002<\/p>\n\n\n\n<p>Python\u51fd\u6570\u4f4d\u7f6e\u53c2\u6570\u4e3e\u4f8b\u662f\u6307\u7528\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660ePython\u51fd\u6570\u7684\u4f4d\u7f6e\u53c2\u6570\u7684\u7528\u6cd5\u548c\u7279\u70b9\u3002\u4f4d\u7f6e\u53c2\u6570\u662f\u6307\u5728\u51fd\u6570\u5b9a\u4e49\u548c\u8c03\u7528\u65f6\uff0c\u6309\u7167\u53c2\u6570\u7684\u4f4d\u7f6e\u6765\u4f20\u9012\u548c\u63a5\u6536\u7684\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u987a\u5e8f\u4e0d\u80fd\u6539\u53d8\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u9519\u8bef\u6216\u8005\u4e0d\u540c\u7684\u7ed3\u679c\u00b9\u00b2\u00b3\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u5dee<br>def subtract(a, b):<br> &nbsp;# \u8fd4\u56dea\u51cfb\u7684\u7ed3\u679c<br> &nbsp;return a - b<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e24\u4e2a\u4f4d\u7f6e\u53c2\u6570<br>result = subtract(10, 5)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fsubtract\uff0c\u5b83\u6709\u4e24\u4e2a\u4f4d\u7f6e\u53c2\u6570a\u548cb\uff0c\u5b83\u7684\u529f\u80fd\u662f\u8fd4\u56dea\u51cfb\u7684\u7ed3\u679c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7subtract(10, 5)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de5\uff0c\u5e76\u8d4b\u503c\u7ed9result\u53d8\u91cf\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u7528print(result)\u6765\u6253\u5370\u7ed3\u679c\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4f4d\u7f6e\u53c2\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u4f4d\u7f6e\u53c2\u6570\u7684\u987a\u5e8f\uff0c\u6bd4\u5982subtract(5, 10)\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u4f1a\u53d8\u6210-5\uff0c\u800c\u4e0d\u662f5\u3002\u5982\u679c\u4f60\u4f20\u5165\u4e86\u591a\u4e8e\u6216\u5c11\u4e8e\u4e24\u4e2a\u53c2\u6570\uff0c\u6bd4\u5982subtract(10)\u6216\u8005subtract(10, 5, 2)\uff0c\u90a3\u4e48\u5c31\u4f1a\u62a5\u9519TypeError\uff0c\u63d0\u793a\u53c2\u6570\u6570\u91cf\u4e0d\u5339\u914d\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u65f6\u8981\u6ce8\u610f\u4fdd\u6301\u53c2\u6570\u7684\u6570\u91cf\u548c\u987a\u5e8f\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u4e8c\u3001<strong>\u5173\u952e\u5b57\u53c2\u6570<\/strong>\uff1a\u5982\u679c\u4e0d\u60f3\u4e25\u683c\u6309\u7167\u987a\u5e8f\u4f20\u9012\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u6309\u5173\u952e\u5b57\u4f20\u9012<\/p>\n\n\n\n<p>Python\u51fd\u6570\u5173\u952e\u5b57\u53c2\u6570\u4e3e\u4f8b\u662f\u6307\u7528\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660ePython\u51fd\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570\u7684\u7528\u6cd5\u548c\u7279\u70b9\u3002\u5173\u952e\u5b57\u53c2\u6570\u662f\u6307\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u6309\u7167\u53c2\u6570\u7684\u540d\u79f0\u548c\u503c\u6765\u4f20\u9012\u7684\u53c2\u6570\uff0c\u5b83\u4eec\u7684\u987a\u5e8f\u53ef\u4ee5\u6539\u53d8\uff0c\u4f46\u662f\u540d\u79f0\u5fc5\u987b\u5339\u914d \u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370\u4e24\u4e2a\u4eba\u7684\u4fe1\u606f<br>def printinfo(name, age):<br> &nbsp;# \u6253\u5370\"name is age years old\"<br> &nbsp;print(name + \" is \" + str(age) + \" years old\")<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e24\u4e2a\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5141\u8bb8\u4f20\u9001\u7684\u53c2\u6570\u4f4d\u7f6e\u4e0d\u540c<br>printinfo(name=\"Bob\", age=20)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>Bob is 20 years old<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fprintinfo\uff0c\u5b83\u6709\u4e24\u4e2a\u4f4d\u7f6e\u53c2\u6570name\u548cage\uff0c\u5b83\u7684\u529f\u80fd\u662f\u6253\u5370\u4e24\u4e2a\u4eba\u7684\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7printinfo(name=&#8221;Bob&#8221;, age=20)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u6253\u5370&#8221;Bob is 20 years old&#8221;\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5173\u952e\u5b57\u53c2\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u5173\u952e\u5b57\u53c2\u6570\u7684\u987a\u5e8f\uff0c\u6bd4\u5982printinfo(age=20, name=&#8221;Bob&#8221;)\uff0c\u90a3\u4e48\u7ed3\u679c\u4e0d\u4f1a\u53d8\u5316\uff0c\u56e0\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u7684\u987a\u5e8f\u4e0d\u5f71\u54cd\u51fd\u6570\u7684\u529f\u80fd\u3002\u4f46\u662f\u5982\u679c\u4f60\u6539\u53d8\u4e86\u5173\u952e\u5b57\u53c2\u6570\u7684\u540d\u79f0\uff0c\u6bd4\u5982printinfo(n=&#8221;Bob&#8221;, a=20)\uff0c\u90a3\u4e48\u5c31\u4f1a\u62a5\u9519NameError\uff0c\u63d0\u793a\u6ca1\u6709\u5b9a\u4e49n\u548ca\u8fd9\u4e24\u4e2a\u53c2\u6570\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u65f6\u8981\u6ce8\u610f\u4fdd\u6301\u53c2\u6570\u7684\u540d\u79f0\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u4e09\u3001<strong>\u9ed8\u8ba4\u503c\u53c2\u6570<\/strong>\uff1a\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u7ed9\u67d0\u4e2a\u53c2\u6570\u8d4b\u503c\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u5c31\u53eb\u505a\u9ed8\u8ba4\u503c\u53c2\u6570\u00b2\u00b3\u2074\u3002<\/p>\n\n\n\n<p>Python\u51fd\u6570\u9ed8\u8ba4\u503c\u53c2\u6570\u4e3e\u4f8b\u662f\u6307\u7528\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660ePython\u51fd\u6570\u7684\u9ed8\u8ba4\u503c\u53c2\u6570\u7684\u7528\u6cd5\u548c\u7279\u70b9\u3002\u9ed8\u8ba4\u503c\u53c2\u6570\u662f\u6307\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\uff0c\u7ed9\u67d0\u4e2a\u53c2\u6570\u8d4b\u503c\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5373\u4fbf\u8c03\u7528\u51fd\u6570\u65f6\u6ca1\u6709\u7ed9\u62e5\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u53c2\u4f20\u9012\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b9a\u4e49\u51fd\u6570\u65f6\u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370\u4e00\u53e5\u95ee\u5019\u8bed<br>def greet(name=\"\u4e54\u5e03\u65af\", message=\"\u6b22\u8fce\u6765\u5230C\u8bed\u8a00\u4e2d\u6587\u7f51\"):<br> &nbsp;# \u6253\u5370\"name, \u60a8\u597d\"<br> &nbsp;print(name + \", \u60a8\u597d\")<br> &nbsp;# \u6253\u5370\"\u6d88\u606f\u662f\uff1amessage\"<br> &nbsp;print(\"\u6d88\u606f\u662f\uff1a\" + message)<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u5168\u90e8\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570<br>greet()<br>print(\"-\"*30)<br># \u8c03\u7528\u51fd\u6570\uff0c\u53ea\u6709message\u53c2\u6570\u4f7f\u7528\u9ed8\u8ba4\u503c<br>greet(\"\u5e93\u514b\")<br>print(\"-\"*30)<br># \u8c03\u7528\u51fd\u6570\uff0c\u4e24\u4e2a\u53c2\u6570\u90fd\u4e0d\u4f7f\u7528\u9ed8\u8ba4\u503c<br>greet(\"\u5e93\u514b\", \"\u6b22\u8fce\u5b66\u4e60Python\")<br>print(\"-\"*30)<br># \u8c03\u7528\u51fd\u6570\uff0c\u53ea\u6709name\u53c2\u6570\u4f7f\u7528\u9ed8\u8ba4\u503c<br>greet(message=\"\u6b22\u8fce\u5b66\u4e60Python\")<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fgreet\uff0c\u5b83\u6709\u4e24\u4e2a\u53c2\u6570name\u548cmessage\uff0c\u5b83\u4eec\u90fd\u6709\u9ed8\u8ba4\u503c\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f20\u5165\u4e0d\u540c\u7684\u53c2\u6570\u503c\uff0c\u4e5f\u53ef\u4ee5\u7701\u7565\u67d0\u4e9b\u53c2\u6570\uff0c\u8ba9\u5b83\u4eec\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u6ce8\u610f\uff0c\u5728\u4f7f\u7528\u6b64\u683c\u5f0f\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u6307\u5b9a\u6709\u9ed8\u8ba4\u503c\u7684\u5f62\u5f0f\u53c2\u6570\u5fc5\u987b\u5728\u6240\u6709\u6ca1\u9ed8\u8ba4\u503c\u53c2\u6570\u7684\u6700\u540e\uff0c\u5426\u5219\u4f1a\u4ea7\u751f\u8bed\u6cd5\u9519\u8bef\u00b9\u00b2\u3002<\/p>\n\n\n\n<p>\u56db\u3001<strong>\u53ef\u53d8\u53c2\u6570<\/strong>\uff1a\u5f53\u51fd\u6570\u9700\u8981\u5904\u7406\u7684\u53c2\u6570\u4e2a\u6570\u4e0d\u786e\u5b9a\u65f6\uff0c\u53ef\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u3002Python\u4e2d\u6709\u4e24\u79cd\u53ef\u53d8\u53c2\u6570\uff1a\u63a5\u6536\u5143\u7ec4\u7684\u53c2\u6570\u540d\u524d\u52a0\u4e00\u4e2a*<strong>\uff0c\u63a5\u6536\u5b57\u5178\u7684\u53c2\u6570\u540d\u524d\u52a0\u4e24\u4e2a<\/strong>**\u3002<\/p>\n\n\n\n<p>Python\u51fd\u6570\u7684\u53ef\u53d8\u53c2\u6570\u4e3e\u4f8b\u662f\u6307\u7528\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660ePython\u51fd\u6570\u7684\u53ef\u53d8\u53c2\u6570\u7684\u7528\u6cd5\u548c\u7279\u70b9\u3002\u53ef\u53d8\u53c2\u6570\u662f\u6307\u5f53\u51fd\u6570\u9700\u8981\u5904\u7406\u7684\u53c2\u6570\u4e2a\u6570\u4e0d\u786e\u5b9a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7684\u4e00\u79cd\u53c2\u6570\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u4e2a\u6570\u548c\u7c7b\u578b\u7684\u53c2\u6570\u00b9\u00b2\u00b3\u3002Python\u4e2d\u6709\u4e24\u79cd\u53ef\u53d8\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63a5\u6536\u5143\u7ec4\u7684\u53ef\u53d8\u53c2\u6570<\/strong>\uff1a\u5728\u53c2\u6570\u540d\u524d\u52a0\u4e00\u4e2a*\uff0c\u8868\u793a\u8be5\u53c2\u6570\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a\u6216\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u5b83\u4eec\u4f1a\u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u00b9\u00b2\u00b3\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba1\u7b97\u4efb\u610f\u4e2a\u6570\u7684\u5e73\u5747\u503c<br>def average(*numbers):<br> &nbsp; &nbsp;# \u5982\u679c\u6ca1\u6709\u4f20\u5165\u4efb\u4f55\u53c2\u6570\uff0c\u8fd4\u56de0<br> &nbsp; &nbsp;if len(numbers) == 0:<br> &nbsp; &nbsp; &nbsp; &nbsp; return 0<br> &nbsp; &nbsp;# \u5426\u5219\uff0c\u8ba1\u7b97\u6240\u6709\u53c2\u6570\u7684\u548c<br> &nbsp; &nbsp;sum = 0<br> &nbsp; &nbsp;for n in numbers:<br> &nbsp; &nbsp; &nbsp; &nbsp;sum += n<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u8fd4\u56de\u548c\u9664\u4ee5\u53c2\u6570\u4e2a\u6570\u7684\u7ed3\u679c<br> &nbsp; &nbsp;return sum \/ len(numbers)<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e0d\u540c\u4e2a\u6570\u7684\u4f4d\u7f6e\u53c2\u6570<br>print(average()) # \u8f93\u51fa0<br>print(average(1, 2, 3)) # \u8f93\u51fa2.0<br>print(average(1, 2, 3, 4, 5)) # \u8f93\u51fa3.0<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662faverage\uff0c\u5b83\u6709\u4e00\u4e2a\u63a5\u6536\u5143\u7ec4\u7684\u53ef\u53d8\u53c2\u6570*numbers\uff0c\u5b83\u7684\u529f\u80fd\u662f\u8ba1\u7b97\u4efb\u610f\u4e2a\u6570\u7684\u5e73\u5747\u503c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7average(1, 2, 3)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de2.0\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u6536\u5143\u7ec4\u7684\u53ef\u53d8\u53c2\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63a5\u6536\u5b57\u5178\u7684\u53ef\u53d8\u53c2\u6570<\/strong>\uff1a\u5728\u53c2\u6570\u540d\u524d\u52a0\u4e24\u4e2a**\uff0c\u8868\u793a\u8be5\u53c2\u6570\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a\u6216\u591a\u4e2a\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5b83\u4eec\u4f1a\u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6253\u5370\u4efb\u610f\u4e2a\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570<br>def printinfo(**kwargs):<br> &nbsp;# \u5982\u679c\u6ca1\u6709\u4f20\u5165\u4efb\u4f55\u53c2\u6570\uff0c\u6253\u5370\"\u6ca1\u6709\u4fe1\u606f\"<br> &nbsp;if len(kwargs) == 0:<br> &nbsp; &nbsp;print(\"\u6ca1\u6709\u4fe1\u606f\")<br> &nbsp;# \u5426\u5219\uff0c\u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u6253\u5370\u51fa\u6765<br> &nbsp;else:<br> &nbsp; &nbsp;for key, value in kwargs.items():<br> &nbsp; &nbsp; &nbsp;print(key + \": \" + value)<br>\u200b<br># \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u5165\u4e0d\u540c\u4e2a\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570<br>printinfo() # \u8f93\u51fa\"\u6ca1\u6709\u4fe1\u606f\"<br>printinfo(name=\"\u5f20\u4e09\", age=\"18\") # \u8f93\u51fa\"name: \u5f20\u4e09\"\u548c\"age: 18\"<br>printinfo(name=\"\u674e\u56db\", age=\"20\", gender=\"\u7537\") # \u8f93\u51fa\"name: \u674e\u56db\"\u3001\"age: 20\"\u548c\"gender: \u7537\"<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662fprintinfo\uff0c\u5b83\u6709\u4e00\u4e2a\u63a5\u6536\u5b57\u5178\u7684\u53ef\u53d8\u53c2\u6570**kwargs\uff0c\u5b83\u7684\u529f\u80fd\u662f\u6253\u5370\u4efb\u610f\u4e2a\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7printinfo(name=&#8221;\u5f20\u4e09&#8221;, age=&#8221;18&#8243;)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u6253\u5370&#8221;name: \u5f20\u4e09&#8221;\u548c&#8221;age: 18&#8243;\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u6536\u5b57\u5178\u7684\u53ef\u53d8\u53c2\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\uff1f<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u51fd\u6570\u540d\u548c\u62ec\u53f7\uff0c\u7136\u540e\u5728\u62ec\u53f7\u4e2d\u4f20\u5165\u53c2\u6570\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aprintme\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">printme(\"Hello World\")<\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u4f1a\u6267\u884c\u51fd\u6570\u7684\u529f\u80fd\uff0c\u5e76\u6253\u5370\u51fa&#8221;Hello World&#8221;\u3002<\/p>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u6307\u5b9a\u53c2\u6570\u7684\u540d\u79f0\u548c\u503c\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u8003\u8651\u53c2\u6570\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aprintinfo\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570name\u548cage\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">printinfo(name=\"miki\", age=50)<\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u4f1a\u6253\u5370\u51famiki\u7684\u540d\u5b57\u548c\u5e74\u9f84\u3002<\/p>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u7ed9\u67d0\u4e9b\u53c2\u6570\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u6bcf\u6b21\u90fd\u4f20\u5165\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aaddnum\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570num1\u548cnum2\uff0c\u5e76\u7ed9num2\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u503c\u4e3a10\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addnum(5) # \u76f8\u5f53\u4e8e addnum(5, 10)<\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u4f1a\u8fd4\u56de5\u548c10\u7684\u548c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u9012\u5f52\u51fd\u6570\uff1f<\/h2>\n\n\n\n<p>Python\u9012\u5f52\u51fd\u6570\u7684\u4f7f\u7528\u662f\u6307\u5229\u7528\u51fd\u6570\u5728\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u7279\u6027\uff0c\u6765\u89e3\u51b3\u4e00\u4e9b\u5177\u6709\u9012\u63a8\u5173\u7cfb\u6216\u53cd\u5411\u6027\u7684\u95ee\u9898\u00b9\u00b2\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3001\u9636\u4e58\u3001\u6c49\u8bfa\u5854\u7b49\u95ee\u9898\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4f53\u5185\u8c03\u7528\u81ea\u8eab\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5927\u578b\u590d\u6742\u7684\u95ee\u9898\u5c42\u5c42\u8f6c\u5316\u4e3a\u4e00\u4e2a\u4e0e\u539f\u95ee\u9898\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u89e3\u51b3\u7684\u57fa\u672c\u60c5\u51b5\u3002\u9012\u5f52\u51fd\u6570\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u6e05\u6670\u3001\u6613\u61c2\uff0c\u9002\u5408\u5904\u7406\u5177\u6709\u9012\u63a8\u5173\u7cfb\u3001\u53cd\u5411\u6027\u6216\u9012\u5f52\u6027\u8d28\u7684\u6570\u636e\u7ed3\u6784\u7684\u95ee\u9898\u3002\u9012\u5f52\u51fd\u6570\u7684\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u91cd\u590d\u8ba1\u7b97\u3001\u6808\u6ea2\u51fa\u6216\u6548\u7387\u4f4e\u4e0b\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u65f6\u8981\u6ce8\u610f\u5b9a\u4e49\u597d\u7ed3\u675f\u6761\u4ef6\u548c\u9012\u63a8\u5173\u7cfb\uff0c\u907f\u514d\u65e0\u9650\u5faa\u73af\u6216\u8fc7\u591a\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u597d\u57fa\u672c\u60c5\u51b5\u548c\u9012\u5f52\u60c5\u51b5<\/strong>\uff1a\u57fa\u672c\u60c5\u51b5\u662f\u6307\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\uff0c\u800c\u4e0d\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u9012\u5f52\u8c03\u7528\u3002\u9012\u5f52\u60c5\u51b5\u662f\u6307\u9012\u5f52\u51fd\u6570\u9700\u8981\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u66f4\u5c0f\u89c4\u6a21\u7684\u5b50\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u548c\u6808\u6ea2\u51fa<\/strong>\uff1a\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u91cd\u590d\u8ba1\u7b97\u3001\u6808\u6ea2\u51fa\u6216\u6548\u7387\u4f4e\u4e0b\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6808\u5e27\uff0c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u4f18\u5316\u65b9\u6cd5\uff0c\u5982\u5c3e\u9012\u5f52\u3001\u7f13\u5b58\u3001\u5faa\u73af\u7b49\uff0c\u6765\u63d0\u9ad8\u9012\u5f52\u51fd\u6570\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e3e\u4f8b\u8bf4\u660ePython\u9012\u5f52\u51fd\u6570\u7684\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u6c42\u89e3\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2cn\u9879<br>def fibonacci(n):<br> &nbsp;# \u57fa\u672c\u60c5\u51b5\uff1an\u4e3a1\u62162\u65f6\uff0c\u8fd4\u56de1<br> &nbsp;if n == 1 or n == 2:<br> &nbsp; &nbsp;return 1<br> &nbsp;# \u9012\u5f52\u60c5\u51b5\uff1an\u5927\u4e8e2\u65f6\uff0c\u8fd4\u56de\u524d\u4e24\u9879\u4e4b\u548c<br> &nbsp;else:<br> &nbsp; &nbsp;return fibonacci(n-1) + fibonacci(n-2)<br>\u200b<br># \u8c03\u7528\u9012\u5f52\u51fd\u6570\uff0c\u6c42\u89e3\u7b2c10\u9879<br>result = fibonacci(10)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<br>\u200b<br># \u6253\u53701\u905310\u4e4b\u95f4\uff0cfibnacci\u6570\u5217<br>for i in range(1,11):<br> &nbsp; &nbsp;print(fibonacci(i),end=\" \")<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u662ffibonacci\uff0c\u5b83\u6709\u4e00\u4e2a\u53c2\u6570n\uff0c\u5b83\u7684\u529f\u80fd\u662f\u8fd4\u56de\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2cn\u9879\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7fibonacci(10)\u6765\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de55\uff0c\u5e76\u8d4b\u503c\u7ed9result\u53d8\u91cf\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u7528print(result)\u6765\u6253\u5370\u7ed3\u679c\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u51fd\u6570\u3002\u4f60\u89c9\u5f97\u8fd9\u4e2a\u4f8b\u5b50\u600e\u4e48\u6837\uff1f? (1) Python \u9012\u5f52\u51fd\u6570<em>python\u9012\u5f52\u51fd\u6570<\/em>\u963f\u7d2b_PP\u7684\u535a\u5ba2-CSDN\u535a\u5ba2. <a href=\"https:\/\/blog.csdn.net\/ruanxingzi123\/article\/details\/82658669\">https:\/\/blog.csdn.net\/ruanxingzi123\/article\/details\/82658669<\/a> \u8bbf\u95ee\u65f6\u95f4 2023\/6\/15. (2) Python\u9012\u5f52\u51fd\u6570 &#8211; Python\u6559\u7a0b &#8211; C\u8bed\u8a00\u7f51. <a href=\"https:\/\/www.dotcpp.com\/course\/262\">https:\/\/www.dotcpp.com\/course\/262<\/a> \u8bbf\u95ee\u65f6\u95f4 2023\/6\/15. (3) python\u4e4b\u51fd\u6570\u7684\u9012\u5f52\u8c03\u7528 &#8211; \u77e5\u4e4e &#8211; \u77e5\u4e4e\u4e13\u680f. <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/357312132\">https:\/\/zhuanlan.zhihu.com\/p\/357312132<\/a> \u8bbf\u95ee\u65f6\u95f4 2023\/6\/15. (4) Python \u4f7f\u7528\u9012\u5f52\u6590\u6ce2\u90a3\u5951\u6570\u5217 | \u83dc\u9e1f\u6559\u7a0b. <a href=\"https:\/\/www.runoob.com\/python3\/python3-fibonacci-recursion.html\">https:\/\/www.runoob.com\/python3\/python3-fibonacci-recursion.html<\/a> \u8bbf\u95ee\u65f6\u95f4 2023\/6\/15.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">lambda\u8868\u8fbe\u5f0f<\/h2>\n\n\n\n<p>lambda\u51fd\u6570\u662f\u4e00\u79cd\u5728Python\u4e2d\u4f7f\u7528\u7684\u533f\u540d\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u3001\u5355\u884c\u7684\u3001\u53ef\u91cd\u7528\u7684\u51fd\u6570\u00b9\u00b2\u3002lambda\u51fd\u6570\u7684\u8bed\u6cd5\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lambda \u53c2\u6570\u5217\u8868: \u8868\u8fbe\u5f0f<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0clambda\u662fPython\u7684\u5173\u952e\u5b57\uff0c\u53c2\u6570\u5217\u8868\u662f\u51fd\u6570\u63a5\u6536\u7684\u53c2\u6570\uff0c\u8868\u8fbe\u5f0f\u662f\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u679c\u00b9\u00b2\u3002lambda\u51fd\u6570\u53ef\u4ee5\u6709\u4ee5\u4e0b\u51e0\u79cd\u7528\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06lambda\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u95f4\u63a5\u8c03\u7528\u8be5lambda\u51fd\u6570\u00b3\u2075\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2alambda\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c<br>add = lambda x, y: x + y<br>\u200b<br># \u8c03\u7528lambda\u51fd\u6570\uff0c\u4f20\u5165\u4e24\u4e2a\u5b9e\u53c2<br>result = add(3, 5)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06lambda\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4f8b\u5982map(), filter(), reduce()\u7b49\u9ad8\u9636\u51fd\u6570\u00b9\u00b2\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<br>numbers = [1, 2, 3, 4, 5]<br>\u200b<br># \u4f7f\u7528map()\u548clambda\u51fd\u6570\uff0c\u8ba1\u7b97\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9<br>squares = map(lambda x: x ** 2, numbers)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(list(squares))<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06lambda\u51fd\u6570\u8d4b\u503c\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4ece\u800c\u5c06\u5176\u4ed6\u51fd\u6570\u7528\u8be5lambda\u51fd\u6570\u66ff\u6362\u00b2\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5bfc\u5165time\u6a21\u5757<br>import time<br>\u200b<br># \u4e3a\u4e86\u628atime\u6a21\u5757\u4e2d\u7684sleep()\u51fd\u6570\u7684\u529f\u80fd\u5c4f\u853d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u521d\u59cb\u5316\u65f6\u8c03\u7528\uff1a<br>time.sleep = lambda x: None<br>\u200b<br># \u8fd9\u6837\uff0c\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u8c03\u7528time.sleep()\u51fd\u6570\u5c06\u4e0d\u4f1a\u6267\u884c\u539f\u6709\u7684\u529f\u80fd\u3002<br># \u4f8b\u5982\uff1a<br>time.sleep(3) # \u7a0b\u5e8f\u4e0d\u4f1a\u4f11\u77203\u79d2\u949f\uff0c\u800c\u662f\u4ec0\u4e48\u90fd\u4e0d\u505a<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7acb\u5373\u8c03\u7528lambda\u51fd\u6570\uff0c\u4e5f\u79f0\u4e3a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u00b2\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u5e76\u7acb\u5373\u8c03\u7528\u4e00\u4e2alambda\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u4e58\u79ef<br>result = (lambda x, y: x * y)(2, 3)<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u95ed\u5305\u4e2d\u4f7f\u7528lambda\u51fd\u6570\u00b9\u00b2\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570<br>def outer_func():<br> &nbsp;# \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf<br> &nbsp;leader = \"Yang Zhou\"<br> &nbsp;<br> &nbsp;# \u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u7684lambda\u51fd\u6570<br> &nbsp;return lambda location=\"\": leader + \" in the \" + location<br>\u200b<br># \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff0c\u5f97\u5230\u4e00\u4e2a\u95ed\u5305<br>lead = outer_func()<br>\u200b<br># \u8c03\u7528\u95ed\u5305\uff0c\u4f20\u5165\u4e00\u4e2a\u5b9e\u53c2<br>result = lead(\"UK\")<br>\u200b<br># \u6253\u5370\u7ed3\u679c<br>print(result)<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e00\u4e9blambda\u51fd\u6570\u7684\u4e3e\u4f8b\u8bf4\u660e\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u7f51\u9875\u4e86\u89e3\u66f4\u591a\u7ec6\u8282\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/134024110\">\u5728Python\u4e2d\u4f7f\u7528Lambda\u51fd\u6570\u7528\u6cd5\u5b9e\u4f8b<\/a><\/li>\n\n\n\n<li>[Python \u4e2d\u7684 Lambda \u51fd\u6570\u2014\u793a\u4f8b\u8bed\u6cd5]<a href=\"https:\/\/blog.csdn.net\/pythondby\/article\/details\/121905620\">https:\/\/blog.csdn.net\/pythondby\/article\/details\/121905620<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/blog.csdn.net\/Karven_\/article\/details\/90272420\">Python3 lambda\u8868\u8fbe\u5f0f\uff08\u533f\u540d\u51fd\u6570\uff09\u4f7f\u7528\u8be6\u89e3<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/www.datacamp.com\/tutorial\/python-list-function\">Functions vs Methods<\/a><\/p>\n\n\n\n<p>A method refers to a function which is part of a class. You access it with an instance or object of the class. A function doesn\u2019t have this restriction: it just refers to a standalone function. This means that all methods are functions, but not all functions are methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5c06\u6ce8\u91caDocstring\u6dfb\u52a0\u5230Python\u51fd\u6570<\/h2>\n\n\n\n<p>\u7528Python\u7f16\u5199\u51fd\u6570\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u662f\uff1a\u6ce8\u91cadocstring\u3002\u6587\u6863\u5b57\u7b26\u4e32\u63cf\u8ff0\u51fd\u6570\u7684\u4f5c\u7528\uff0c\u4f8b\u5982\u5b83\u6267\u884c\u7684\u8ba1\u7b97\u6216\u8fd4\u56de\u503c\u3002\u8fd9\u4e9b\u63cf\u8ff0\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u6587\u6863\uff0c\u8fd9\u6837\u4efb\u4f55\u9605\u8bfb\u51fd\u6570\u6587\u6863\u5b57\u7b26\u4e32\u7684\u4eba\u90fd\u53ef\u4ee5\u7406\u89e3\u51fd\u6570\u7684\u4f5c\u7528\uff0c\u800c\u65e0\u9700\u8ddf\u8e2a\u51fd\u6570\u5b9a\u4e49\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002 \u51fd\u6570\u6587\u6863\u5b57\u7b26\u4e32\u653e\u5728\u51fd\u6570\u5934\u540e\u7684\u76f4\u63a5\u4e00\u884c\uff0c\u5e76\u653e\u5728\u4e09\u4e2a\u5f15\u53f7\u4e4b\u95f4\u3002\u201chello\uff08\uff09\u201d\u51fd\u6570\u7684\u4e00\u4e2a\u5408\u9002\u7684Docstring\u662f\u201c\u6253\u5370\u201dhello World\u201c\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u200b<br>def hello():<br>\u200b<br>\"\"\"<br>Prints \"Hello World\".<br>Returns:<br>  None<br>  \u51fd\u6570\u6ce8\u91ca<br>\"\"\"<br>\u200b<br> &nbsp; &nbsp; print(\"Hello World\") <br> &nbsp; &nbsp; return <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528<code>main\uff08\uff09<\/code>\u4f5c\u4e3a\u51fd\u6570<\/h2>\n\n\n\n<p><strong>Using<\/strong> <strong><code>main()<\/code><\/strong> <strong>as a Function<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u60a8\u5bf9Java\u7b49\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u6709\u4efb\u4f55\u7ecf\u9a8c\uff0c\u60a8\u5c31\u4f1a\u77e5\u9053\u6267\u884c\u51fd\u6570\u9700\u8981\u201cmain\u201d\u51fd\u6570\u3002\u6b63\u5982\u60a8\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u6240\u770b\u5230\u7684\uff0cPython\u4e0d\u4e00\u5b9a\u9700\u8981\u8fd9\u6837\u505a\u3002\u7136\u800c\uff0c\u5728Python\u7a0b\u5e8f\u4e2d\u5305\u542b\u4e00\u4e2a\u201cmain\uff08\uff09\u201d\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u903b\u8f91\u4e0a\u6784\u5efa\u4ee3\u7801\u2014\u2014\u6240\u6709\u6700\u91cd\u8981\u7684\u7ec4\u4ef6\u90fd\u5305\u542b\u5728\u8fd9\u4e2a\u201cmain\uff08\u201d\u51fd\u6570\u4e2d\u3002\u60a8\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9a\u4e49\u4e00\u4e2a\u201cmain()\u201d\u51fd\u6570\u5e76\u8c03\u7528\u5b83\uff0c\u5c31\u50cf\u60a8\u5bf9\u4e0a\u9762\u6240\u6709\u5176\u4ed6\u51fd\u6570\u6240\u505a\u7684\u90a3\u6837\uff1a\u7136\u800c\uff0c\u76ee\u524d\u7684\u60c5\u51b5\u662f\uff0c\u5f53\u60a8\u5c06\u201cmain\uff08\uff09\u201d\u51fd\u6570\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u65f6\uff0c\u5b83\u7684\u4ee3\u7801\u5c06\u88ab\u8c03\u7528\u3002\u4e3a\u4e86\u786e\u4fdd\u8fd9\u79cd\u60c5\u51b5\u4e0d\u4f1a\u53d1\u751f\uff0c\u5f53:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if __name__ == '__main__':<\/pre>\n\n\n\n<p>\u8fd9\u610f\u5473\u7740\u4e0a\u8ff0\u4ee3\u7801\u5757\u7684\u4ee3\u7801\u53d8\u4e3a\uff1amain &#8212; \u4e3b\u51fd\u6570\u4ee3\u7801\u5757 \u8bf7\u6ce8\u610f\uff0c\u9664\u4e86\u201c<strong>main<\/strong>\u201d\u51fd\u6570\uff0c\u8fd8\u6709\u4e00\u4e2a\u201c<strong>init<\/strong>\u201d\u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u7c7b\u6216\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u7b80\u5355\u5730\u8bf4\uff0c\u5b83\u5145\u5f53\u6784\u9020\u51fd\u6570\u6216\u521d\u59cb\u503c\u8bbe\u5b9a\u9879\uff0c\u5e76\u5728\u521b\u5efa\u7c7b\u7684\u65b0\u5b9e\u4f8b\u65f6\u81ea\u52a8\u8c03\u7528\u3002\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5c06\u88ab\u6307\u5b9a\u7ed9\u53c2\u6570self\uff0c\u60a8\u5728\u672c\u6559\u7a0b\u524d\u9762\u770b\u5230\u4e86\u8fd9\u4e00\u70b9\u3002\u8bf7\u770b\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Dog:<br> &nbsp;\"\"\" &nbsp; <br>  Requires:<br>  legs - Legs so that the dog can walk.<br>  color - A color of the fur.<br>  \"\"\"<br>\u200b<br> &nbsp;def __init__(self, legs, color):<br> &nbsp; &nbsp; &nbsp;self.legs = legs<br> &nbsp; &nbsp; &nbsp;self.color = color<br>\u200b<br>\u200b<br> &nbsp;def bark(self):<br> &nbsp; &nbsp;bark = \"bark\" * 2<br> &nbsp; &nbsp;return bark<br>\u200b<br>if __name__ == \"__main__\":<br> &nbsp; &nbsp; &nbsp;dog = Dog(4, \"brown\")<br> &nbsp; &nbsp; &nbsp;bark = dog.bark()<br> &nbsp; &nbsp; &nbsp;print(bark)<br> &nbsp; &nbsp; &nbsp;print(\"Legs\uff1a{}\uff0cColor\uff1a{}\".format(dog.legs,dog.color))<br> &nbsp; &nbsp; &nbsp;dog = Dog(4, \"Red\")<br> &nbsp; &nbsp; &nbsp;print(\"Legs\uff1a{}\uff0cColor\uff1a{}\".format(dog.legs,dog.color))<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/www.datacamp.com\/tutorial\/python-list-function\">\u51fd\u6570\u4e0e\u65b9\u6cd5\u7684\u533a\u522b<\/a><\/h2>\n\n\n\n<p>\u65b9\u6cd5\u662f\u6307\u4f5c\u4e3a\u7c7b\u7684\u4e00\u90e8\u5206\u7684\u51fd\u6570\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u7684\u5b9e\u4f8b\u6216\u5bf9\u8c61\u6765\u8bbf\u95ee\u5b83\u3002\u51fd\u6570\u6ca1\u6709\u8fd9\u4e2a\u9650\u5236\uff1a\u5b83\u53ea\u662f\u6307\u4e00\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u662f\u51fd\u6570\uff0c\u4f46\u5e76\u4e0d\u662f\u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u51fd\u6570\u7528\u9014\u3001\u5b9a\u4e49\u65b9\u6cd5\u3001\u53c2\u6570\u7c7b\u578b\u3001\u8c03\u7528\u51fd\u6570\u3001\u9012\u5f52\u51fd\u6570\u3001lambda\u51fd\u6570P <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3470\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3470","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3470"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3470\/revisions"}],"predecessor-version":[{"id":3471,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3470\/revisions\/3471"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}