{"id":1045879,"date":"2024-12-31T13:28:12","date_gmt":"2024-12-31T05:28:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045879.html"},"modified":"2024-12-31T13:28:15","modified_gmt":"2024-12-31T05:28:15","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e5%87%bd%e6%95%b0%e8%be%93%e5%87%ba%e7%bb%93%e6%9e%9c%e6%98%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1045879.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u662f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/dc183eda-ad87-494a-903f-f55f58f6348a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4f7f\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u662f\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1areturn\u8bed\u53e5\u3001print\u8bed\u53e5\u3001yield\u8bed\u53e5\u3001\u4fee\u6539\u4f20\u5165\u53c2\u6570\u7684\u503c\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u6765\u8fd4\u56de\u51fd\u6570\u7684\u7ed3\u679c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>return<\/code>\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u5c06\u8ba1\u7b97\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\u901a\u8fc7<code>return<\/code>\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>add<\/code>\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u503c\u88ab\u5b58\u50a8\u5728\u53d8\u91cf<code>result<\/code>\u4e2d\uff0c\u5e76\u901a\u8fc7<code>print<\/code>\u8bed\u53e5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>print<\/code>\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>\u867d\u7136<code>print<\/code>\u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u8f93\u51fa\u7ed3\u679c\uff0c\u4f46\u5b83\u5e76\u4e0d\u8fd4\u56de\u503c\u7ed9\u8c03\u7528\u8005\u3002\u56e0\u6b64\uff0c<code>print<\/code>\u8bed\u53e5\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u6253\u5370\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u7528\u4e8e\u8fd4\u56de\u51fd\u6570\u7ed3\u679c\u3002<\/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>greet(&quot;Alice&quot;)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>greet<\/code>\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u8f93\u51fa\u95ee\u5019\u8bed\u3002\u4f46\u662f\uff0c\u51fd\u6570\u672c\u8eab\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5<\/h2>\n<\/p>\n<p><p><code>yield<\/code>\u8bed\u53e5\u7528\u4e8e\u751f\u6210\u5668\u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u7ec8\u6b62\u51fd\u6570\u6267\u884c\u3002\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u7cfb\u5217\u503c\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    a, b = 0, 1<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        yield a<\/p>\n<p>        a, b = b, a + b<\/p>\n<p>for num in fibonacci(5):<\/p>\n<p>    print(num)  # \u8f93\u51fa: 0 1 1 2 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>fibonacci<\/code>\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\u65f6\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6570\u5217\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4fee\u6539\u4f20\u5165\u53c2\u6570\u7684\u503c<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5bf9\u8c61\u7684\u503c\uff0c\u5e76\u5728\u51fd\u6570\u5916\u90e8\u8bbf\u95ee\u4fee\u6539\u540e\u7684\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u4f20\u5165\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update_list(lst):<\/p>\n<p>    lst.append(4)<\/p>\n<p>numbers = [1, 2, 3]<\/p>\n<p>update_list(numbers)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>update_list<\/code>\u63a5\u6536\u4e00\u4e2a\u5217\u8868\u53c2\u6570\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5217\u8868\u7684\u5185\u5bb9\u3002\u4fee\u6539\u540e\u7684\u5217\u8868\u5728\u51fd\u6570\u5916\u90e8\u4e5f\u80fd\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u7684\u8303\u56f4\u5185\u90fd\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u51fd\u6570\u7684\u7ed3\u679c\u5728\u5168\u5c40\u8303\u56f4\u5185\u53ef\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 0<\/p>\n<p>def calculate_sum(a, b):<\/p>\n<p>    global result<\/p>\n<p>    result = a + b<\/p>\n<p>calculate_sum(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>calculate_sum<\/code>\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u58f0\u660e\u5168\u5c40\u53d8\u91cf<code>result<\/code>\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u503c\u3002\u4fee\u6539\u540e\u7684\u7ed3\u679c\u5728\u51fd\u6570\u5916\u90e8\u4e5f\u80fd\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4f7f\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\uff0c\u6bcf\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u660e\u786e\u5730\u8fd4\u56de\u51fd\u6570\u7ed3\u679c\u5e76\u6613\u4e8e\u7406\u89e3\u3002\u5176\u4ed6\u65b9\u6cd5\u5982<code>print<\/code>\u8bed\u53e5\u3001<code>yield<\/code>\u8bed\u53e5\u3001\u4fee\u6539\u4f20\u5165\u53c2\u6570\u7684\u503c\u548c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5219\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7075\u6d3b\u8fd0\u7528\u5b83\u4eec\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u7f16\u5199Python\u51fd\u6570\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fd4\u56de\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u53ef\u4ee5\u4f7f\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u3002\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5e76\u5728\u9700\u8981\u8fd4\u56de\u7684\u503c\u524d\u6dfb\u52a0<code>return<\/code>\u5173\u952e\u5b57\uff0c\u5373\u53ef\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u8c03\u7528\u8005\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u6cd5\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n\nresult = add(5, 3)\nprint(result)  # \u8f93\u51fa 8\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u6355\u83b7\u51fd\u6570\u7684\u8f93\u51fa\u5e76\u8fdb\u884c\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u8fd4\u56de\u7684\u7ed3\u679c\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3001\u903b\u8f91\u5224\u65ad\u6216\u8005\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5904\u7406\u51fd\u6570\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-python\">def multiply(x, y):\n    return x * y\n\noutput = multiply(4, 5)\nif output &gt; 15:\n    print(&quot;\u7ed3\u679c\u5927\u4e8e15&quot;)\nelse:\n    print(&quot;\u7ed3\u679c\u5c0f\u4e8e\u6216\u7b49\u4e8e15&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u4f7f\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\uff1f<\/strong><br \/>Python\u652f\u6301\u4ece\u51fd\u6570\u4e2d\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u901a\u8fc7\u9017\u53f7\u5206\u9694\u8fd4\u56de\u503c\u3002\u8c03\u7528\u65f6\uff0c\u8fd9\u4e9b\u8fd4\u56de\u503c\u53ef\u4ee5\u901a\u8fc7\u5143\u7ec4\u63a5\u6536\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u540c\u65f6\u8fd4\u56de\u4e24\u4e2a\u8ba1\u7b97\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate(a, b):\n    sum_result = a + b\n    product_result = a * b\n    return sum_result, product_result\n\nsum_val, product_val = calculate(3, 4)\nprint(&quot;\u548c:&quot;, sum_val)        # \u8f93\u51fa \u548c: 7\nprint(&quot;\u79ef:&quot;, product_val)    # \u8f93\u51fa \u79ef: 12\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u5229\u7528\u51fd\u6570\u8fd4\u56de\u8f93\u51fa\u7ed3\u679c\uff0c\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u7ed3\u679c\uff0c\u4ee5\u53ca\u5982\u4f55\u8fd4\u56de\u591a\u4e2a\u7ed3\u679c\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u51fd\u6570\u7684\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1areturn\u8bed\u53e5\u3001print\u8bed\u53e5\u3001yield\u8bed\u53e5\u3001\u4fee\u6539\u4f20\u5165\u53c2\u6570\u7684\u503c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1045887,"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\/1045879"}],"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=1045879"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045879\/revisions"}],"predecessor-version":[{"id":1045893,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045879\/revisions\/1045893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045887"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}