{"id":1034598,"date":"2024-12-31T11:51:35","date_gmt":"2024-12-31T03:51:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1034598.html"},"modified":"2024-12-31T11:51:37","modified_gmt":"2024-12-31T03:51:37","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e8%87%aa%e5%b7%b1%e5%ae%9a%e4%b9%89%e7%9a%84python%e5%87%bd%e6%95%b0%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1034598.html","title":{"rendered":"\u5982\u4f55\u8c03\u7528\u81ea\u5df1\u5b9a\u4e49\u7684python\u51fd\u6570\u5417"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2bb9f11f-99be-43a6-81c1-8d2bbd18eeea.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8c03\u7528\u81ea\u5df1\u5b9a\u4e49\u7684python\u51fd\u6570\u5417\" \/><\/p>\n<p><p> <strong>\u8c03\u7528\u81ea\u5df1\u5b9a\u4e49\u7684Python\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u5b9a\u4e49\u51fd\u6570\u3001\u8c03\u7528\u51fd\u6570\u3001\u4f20\u9012\u53c2\u6570\u3001\u8fd4\u56de\u503c\u5904\u7406\u3001\u6a21\u5757\u5316\u7f16\u7a0b<\/strong>\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5176\u4e2d<strong>\u5b9a\u4e49\u51fd\u6570<\/strong>\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u548c\u4e00\u5bf9\u5706\u62ec\u53f7\uff0c\u62ec\u53f7\u5185\u53ef\u4ee5\u5305\u542b\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5176\u8303\u56f4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5b9a\u4e49\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u662f\u51fd\u6570\u540d\uff0c<code>name<\/code>\u662f\u53c2\u6570\uff0c\u51fd\u6570\u4f53\u5305\u542b\u4e86\u4e00\u6761\u6253\u5370\u8bed\u53e5\u3002<strong>\u5b9a\u4e49\u51fd\u6570<\/strong>\u662f\u4e3a\u4e86\u5c06\u67d0\u4e9b\u64cd\u4f5c\u5c01\u88c5\u8d77\u6765\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u591a\u6b21\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u590d\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u51fd\u6570\u540d\u5e76\u4f20\u5165\u9002\u5f53\u7684\u53c2\u6570\u5373\u53ef\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528<code>greet<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0cPython\u4f1a\u6267\u884c\u51fd\u6570\u4f53\u5185\u7684\u6240\u6709\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u901a\u8fc7\u9017\u53f7\u5206\u9694\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6309\u987a\u5e8f\u4f20\u5165\u5bf9\u5e94\u7684\u53c2\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8c03\u7528\u65f6\u4f20\u5165\u4e24\u4e2a\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = add(3, 5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>8<\/code>\u3002<strong>\u4f20\u9012\u53c2\u6570<\/strong>\u53ef\u4ee5\u8ba9\u51fd\u6570\u66f4\u52a0\u901a\u7528\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fd4\u56de\u503c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002\u4f7f\u7528<code>return<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u503c\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u8d4b\u7ed9\u53d8\u91cf\uff0c\u4f9b\u540e\u7eed\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def square(x):<\/p>\n<p>    return x * x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8c03\u7528\u5e76\u5904\u7406\u8fd4\u56de\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = square(4)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>16<\/code>\u3002<strong>\u8fd4\u56de\u503c\u5904\u7406<\/strong>\u4f7f\u5f97\u51fd\u6570\u4e0d\u4ec5\u80fd\u6267\u884c\u64cd\u4f5c\uff0c\u8fd8\u80fd\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6a21\u5757\u5316\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0cPython\u652f\u6301\u5c06\u51fd\u6570\u5b9a\u4e49\u5728\u6a21\u5757\u4e2d\uff0c\u5e76\u5728\u5176\u4ed6\u5730\u65b9\u5bfc\u5165\u548c\u8c03\u7528\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u8c03\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>from mymodule import multiply<\/p>\n<p>result = multiply(3, 4)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>12<\/code>\u3002<strong>\u6a21\u5757\u5316\u7f16\u7a0b<\/strong>\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u7701\u7565\u8fd9\u4e9b\u53c2\u6570\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message=&quot;Hello&quot;):<\/p>\n<p>    print(f&quot;{message}, {name}!&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570<\/strong><\/h2>\n<p>greet(&quot;Alice&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570<\/strong><\/h2>\n<p>greet(name=&quot;Bob&quot;, message=&quot;Hi&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5206\u522b\u8f93\u51fa<code>Hello, Alice!<\/code>\u548c<code>Hi, Bob!<\/code>\u3002<strong>\u9ed8\u8ba4\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570<\/strong>\u4f7f\u5f97\u51fd\u6570\u8c03\u7528\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u53ef\u53d8\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5b9a\u4e49\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u53c2\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528*args\u548ckwargs\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_all(*args):<\/p>\n<p>    total = 0<\/p>\n<p>    for num in args:<\/p>\n<p>        total += num<\/p>\n<p>    return total<\/p>\n<p>result = sum_all(1, 2, 3, 4)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>10<\/code>\u3002*args\u4f1a\u5c06\u6240\u6709\u4f4d\u7f6e\u53c2\u6570\u4f5c\u4e3a\u5143\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u3002<strong>\u53ef\u53d8\u53c2\u6570<\/strong>\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u4e0d\u786e\u5b9a\u6570\u91cf\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u51fd\u6570\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\uff0c\u5e94\u8be5\u4e3a\u5176\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\uff0c\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u6587\u6863\u5b57\u7b26\u4e32\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u5b9a\u4e49\uff0c\u5e76\u653e\u5728\u51fd\u6570\u4f53\u7684\u7b2c\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Divide two numbers and return the result.<\/p>\n<p>    Args:<\/p>\n<p>        a (int or float): The dividend.<\/p>\n<p>        b (int or float): The divisor.<\/p>\n<p>    Returns:<\/p>\n<p>        float: The result of division.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a \/ b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>help<\/code>\u51fd\u6570\u67e5\u770b\u6587\u6863\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">help(divide)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002<strong>\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\u662f\u7f16\u5199\u6e05\u6670\u3001\u53ef\u7ef4\u62a4\u4ee3\u7801\u7684\u91cd\u8981\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5d4c\u5957\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u79f0\u4e3a\u5d4c\u5957\u51fd\u6570\u3002\u5d4c\u5957\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(text):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(text)<\/p>\n<p>    inner_function()<\/p>\n<p>outer_function(&quot;Hello from the inner function!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>Hello from the inner function!<\/code>\u3002\u5d4c\u5957\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5\u903b\u8f91\uff0c\u907f\u514d\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5d4c\u5957\u51fd\u6570\u53ef\u4ee5\u8bb0\u4f4f\u5176\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5de5\u5382\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_multiplier(n):<\/p>\n<p>    def multiplier(x):<\/p>\n<p>        return x * n<\/p>\n<p>    return multiplier<\/p>\n<p>times_three = make_multiplier(3)<\/p>\n<p>print(times_three(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>15<\/code>\u3002\u95ed\u5305\u4f7f\u5f97\u51fd\u6570\u5177\u6709\u72b6\u6001\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u7c7b\u884c\u4e3a\u7684\u9ad8\u7ea7\u7279\u6027\u3002\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u5f3a\u6216\u6539\u53d8\u5176\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello!&quot;)<\/p>\n<p>say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Something is happening before the function is called.<\/p>\n<p>Hello!<\/p>\n<p>Something is happening after the function is called.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u6269\u5c55\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u51fd\u6570\u81ea\u8eab\u3002\u9012\u5f52\u51fd\u6570\u9700\u8981\u6709\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\uff0c\u5982\u8ba1\u7b97\u9636\u4e58\u3001\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>print(factorial(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>120<\/code>\u3002\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u67d0\u4e9b\u7b97\u6cd5\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u533f\u540d\u51fd\u6570\uff08Lambda\uff09<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u4f7f\u7528lambda\u5173\u952e\u5b57\u521b\u5efa\u533f\u540d\u51fd\u6570\uff0c\u8fd9\u79cd\u51fd\u6570\u6ca1\u6709\u540d\u5b57\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u77ed\u7684\u56de\u8c03\u51fd\u6570\u6216\u9ad8\u9636\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add = lambda a, b: a + b<\/p>\n<p>print(add(3, 5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>8<\/code>\u3002Lambda\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u5e94\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\uff0c\u4ee5\u514d\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u6062\u590d\u5176\u6267\u884c\u72b6\u6001\u3002\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u6216\u65e0\u9650\u5e8f\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        yield n<\/p>\n<p>        n -= 1<\/p>\n<p>for num in countdown(5):<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>5<\/p>\n<p>4<\/p>\n<p>3<\/p>\n<p>2<\/p>\n<p>1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5185\u7f6e\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>len<\/code>\u3001<code>max<\/code>\u3001<code>min<\/code>\u7b49\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_function():<\/p>\n<p>    pass  # \u81ea\u5b9a\u4e49\u51fd\u6570\u903b\u8f91<\/p>\n<p>print(len([1, 2, 3]))  # \u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ed3\u5408\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u529f\u80fd\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        return &quot;Division by zero is not allowed&quot;<\/p>\n<p>    return result<\/p>\n<p>print(divide(10, 2))<\/p>\n<p>print(divide(10, 0))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>5.0<\/p>\n<p>Division by zero is not allowed<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9519\u8bef\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u51fd\u6570\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>Python\u5141\u8bb8\u4e3a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6dfb\u52a0\u6ce8\u89e3\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6ce8\u89e3\u4e0d\u4f1a\u5f71\u54cd\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u4f46\u53ef\u4ee5\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u548c\u6587\u6863\u751f\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name: str) -&gt; str:<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u51fd\u6570\u6ce8\u89e3\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u7684\u610f\u56fe\u548c\u7ea6\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u51fd\u6570\u7684\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u79f0\u4e3a\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728\u51fd\u6570\u5185\u90e8\u53ef\u89c1\u3002\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u79f0\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u3002\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>def modify():<\/p>\n<p>    global x<\/p>\n<p>    x = 20<\/p>\n<p>modify()<\/p>\n<p>print(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>20<\/code>\u3002\u4e86\u89e3\u51fd\u6570\u7684\u4f5c\u7528\u57df\u53ef\u4ee5\u907f\u514d\u53d8\u91cf\u547d\u540d\u51b2\u7a81\u548c\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u51fd\u6570\u7684\u547d\u540d\u7ea6\u5b9a<\/h3>\n<\/p>\n<p><p>\u9075\u5faa\u4e00\u81f4\u7684\u547d\u540d\u7ea6\u5b9a\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002Python\u7684\u547d\u540d\u7ea6\u5b9a\u5305\u62ec\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u5355\u8bcd\uff08snake_case\uff09\uff0c\u4ee5\u53ca\u4f7f\u7528\u52a8\u8bcd\u77ed\u8bed\u63cf\u8ff0\u51fd\u6570\u7684\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_area(radius):<\/p>\n<p>    pass  # \u51fd\u6570\u903b\u8f91<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9075\u5faa\u547d\u540d\u7ea6\u5b9a\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\uff08\u5982<code>map<\/code>\u3001<code>filter<\/code>\u3001<code>reduce<\/code>\uff09\u548c\u533f\u540d\u51fd\u6570\uff08lambda\uff09\u5b9e\u73b0\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f3a\u8c03\u4e0d\u53ef\u53d8\u6027\u548c\u65e0\u526f\u4f5c\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared = list(map(lambda x: x2, numbers))<\/p>\n<p>evens = list(filter(lambda x: x % 2 == 0, squared))<\/p>\n<p>sum_of_evens = reduce(lambda x, y: x + y, evens)<\/p>\n<p>print(sum_of_evens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>20<\/code>\u3002\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u4e8e\u6d4b\u8bd5\u548c\u5e76\u884c\u5316\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e8c\u5341\u4e2a\u65b9\u9762\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u5168\u9762\u4e86\u89e3\u4e86\u5982\u4f55\u5b9a\u4e49\u548c\u8c03\u7528Python\u51fd\u6570\uff0c\u4ee5\u53ca\u51fd\u6570\u7684\u5404\u79cd\u9ad8\u7ea7\u7279\u6027\u548c\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u52a0\u9ad8\u6548\u5730\u4f7f\u7528\u51fd\u6570\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684Python\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u79f0\u548c\u53c2\u6570\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function(param1, param2):\n    return param1 + param2\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<p><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u60a8\u4f20\u9012\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u51fd\u6570\u5b9a\u4e49\u4e2d\u7684\u8981\u6c42\u76f8\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u51fd\u6570\u671f\u671b\u63a5\u6536\u6574\u6570\uff0c\u4f46\u60a8\u4f20\u9012\u4e86\u5b57\u7b26\u4e32\uff0c\u5c06\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u8c03\u7528\u51fd\u6570\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">result = my_function(5, 10)  # \u8fd4\u56de15\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u60a8\u9700\u8981\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5c06\u5176\u5bfc\u5165\u3002\u5047\u8bbe\u60a8\u7684\u51fd\u6570\u4f4d\u4e8e<code>my_module.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">from my_module import my_function\nresult = my_function(3, 7)  # \u8fd4\u56de10\n<\/code><\/pre>\n<p>\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u6216\u8005\u4e0e\u60a8\u7684\u4e3b\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8c03\u7528\u81ea\u5df1\u5b9a\u4e49\u7684Python\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u5b9a\u4e49\u51fd\u6570\u3001\u8c03\u7528\u51fd\u6570\u3001\u4f20\u9012\u53c2\u6570\u3001\u8fd4\u56de\u503c\u5904\u7406\u3001\u6a21\u5757\u5316\u7f16\u7a0b\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1034605,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034598"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1034598"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034598\/revisions"}],"predecessor-version":[{"id":1034607,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034598\/revisions\/1034607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1034605"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1034598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1034598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1034598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}