{"id":648,"date":"2026-03-09T14:12:50","date_gmt":"2026-03-09T06:12:50","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=648"},"modified":"2026-07-13T17:32:02","modified_gmt":"2026-07-13T09:32:02","slug":"python%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/09\/python%e5%87%bd%e6%95%b0\/","title":{"rendered":"python\u51fd\u6570"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u9ed8\u8ba4\u53c2\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u53c2\u6570\u6307\u5b9a<strong>\u9ed8\u8ba4\u503c<\/strong>\uff0c\u8c03\u7528\u65f6\u53ef\u7701\u7565\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name, greeting=\"Hello\"):<br> &nbsp; &nbsp;print(f\"{greeting}, {name}!\")<br>\u200b<br>greet(\"Alice\") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Hello, Alice!<br>greet(\"Bob\", \"Hi\") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Hi, Bob!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\uff1a<\/strong> \u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u653e\u5728\u975e\u9ed8\u8ba4\u53c2\u6570\u540e\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u9519\u8bef\u793a\u4f8b<br># def greet(greeting=\"Hello\", name):  # SyntaxError!<br># &nbsp; &nbsp; pass<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7<strong>\u53c2\u6570\u540d<\/strong>\u4f20\u9012\uff0c\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person_info(name, age, city):<br> &nbsp; &nbsp;print(f\"{name}, {age}\u5c81, \u6765\u81ea{city}\")<br>\u200b<br># \u5173\u952e\u5b57\u53c2\u6570\uff0c\u987a\u5e8f\u65e0\u5173<br>person_info(age=25, city=\"\u5317\u4eac\", name=\"Alice\")<br># Alice, 25\u5c81, \u6765\u81ea\u5317\u4eac<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6df7\u5408\u4f7f\u7528\uff1a<\/strong> \u4f4d\u7f6e\u53c2\u6570\u5728\u524d\uff0c\u5173\u952e\u5b57\u53c2\u6570\u5728\u540e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person_info(\"Alice\", age=25, city=\"\u5317\u4eac\") &nbsp;# \u6b63\u786e<br># person_info(name=\"Alice\", 25, \"\u5317\u4eac\") &nbsp; # \u9519\u8bef\uff01\u4f4d\u7f6e\u53c2\u6570\u4e0d\u80fd\u5728\u5173\u952e\u5b57\u53c2\u6570\u540e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ef\u53d8\u53c2\u6570<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><code>*args<\/code> \u2014\u2014 \u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def sum_all(*args):<br> &nbsp; &nbsp;total = 0<br> &nbsp; &nbsp;for num in args:<br> &nbsp; &nbsp; &nbsp; &nbsp;total += num<br> &nbsp; &nbsp;return total<br>\u200b<br># \u8c03\u7528<br>print(sum_all(1, 2, 3)) &nbsp; &nbsp; # 6<br>print(sum_all(1, 2, 3, 4)) &nbsp;# 10<br>print(sum_all()) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>args<\/code> \u7684\u672c\u8d28\uff1a<\/strong> \u51fd\u6570\u5185\u90e8\u662f\u4e00\u4e2a<strong>\u5143\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def show_args(*args):<br> &nbsp; &nbsp;print(type(args)) &nbsp;# &lt;class 'tuple'&gt;<br> &nbsp; &nbsp;print(args) &nbsp; &nbsp; &nbsp; &nbsp;# (1, 2, 3)<br>\u200b<br>show_args(1, 2, 3)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><code>**kwargs<\/code> \u2014\u2014 \u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def print_info(**kwargs):<br> &nbsp; &nbsp;for key, value in kwargs.items():<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"{key}: {value}\")<br>\u200b<br># \u8c03\u7528<br>print_info(name=\"Alice\", age=25, city=\"\u5317\u4eac\")<br># name: Alice<br># age: 25<br># city: \u5317\u4eac<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>kwargs<\/code> \u7684\u672c\u8d28\uff1a<\/strong> \u51fd\u6570\u5185\u90e8\u662f\u4e00\u4e2a<strong>\u5b57\u5178<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def show_kwargs(**kwargs):<br> &nbsp; &nbsp;print(type(kwargs)) &nbsp;# &lt;class 'dict'&gt;<br> &nbsp; &nbsp;print(kwargs) &nbsp; &nbsp; &nbsp; &nbsp;# {'name': 'Alice', 'age': 25}<br>\u200b<br>show_kwargs(name=\"Alice\", age=25)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u6570\u987a\u5e8f\u89c4\u5219<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53c2\u6570\u5fc5\u987b\u6309\u4ee5\u4e0b\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def func(\u4f4d\u7f6e\u53c2\u6570, \u9ed8\u8ba4\u53c2\u6570, *args, **kwargs):<br> &nbsp; &nbsp;pass<br>\u200b<br># \u793a\u4f8b<br>def demo(a, b=2, *args, **kwargs):<br> &nbsp; &nbsp;print(f\"a={a}, b={b}\")<br> &nbsp; &nbsp;print(f\"args={args}\")<br> &nbsp; &nbsp;print(f\"kwargs={kwargs}\")<br>\u200b<br>demo(1, 3, 4, 5, x=10, y=20)<br># a=1, b=3<br># args=(4, 5)<br># kwargs={'x': 10, 'y': 20}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u8fd4\u56de\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u51fd\u6570\u53ef\u4ee5<strong>\u540c\u65f6\u8fd4\u56de\u591a\u4e2a\u503c<\/strong>\uff08\u5b9e\u9645\u662f\u8fd4\u56de\u5143\u7ec4\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def min_max(numbers):<br> &nbsp; &nbsp;return min(numbers), max(numbers)<br>\u200b<br>minimum, maximum = min_max(&#91;3, 1, 4, 1, 5])<br>print(minimum) &nbsp;# 1<br>print(maximum) &nbsp;# 5<br>\u200b<br># \u672c\u8d28\u4e0a\u8fd4\u56de\u7684\u662f\u5143\u7ec4<br>result = min_max(&#91;3, 1, 4])<br>print(result) &nbsp; &nbsp; &nbsp; # (1, 4)<br>print(type(result)) # &lt;class 'tuple'&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9ed8\u8ba4\u53c2\u6570 \u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8c03\u7528\u65f6\u53ef\u7701\u7565\uff1a \u6ce8\u610f\uff1a..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}