{"id":1094252,"date":"2025-01-08T14:39:17","date_gmt":"2025-01-08T06:39:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094252.html"},"modified":"2025-01-08T14:39:19","modified_gmt":"2025-01-08T06:39:19","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%bb%93%e6%9e%9c%e8%be%93%e5%87%ba%e4%b8%ba%e5%8f%98%e9%87%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094252.html","title":{"rendered":"python\u5982\u4f55\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210439\/9632f4e9-c449-42da-86f0-2961dd7fff39.webp\" alt=\"python\u5982\u4f55\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c<\/strong>\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u53d8\u91cf\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec<strong>\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u3001\u901a\u8fc7\u5217\u8868\u6216\u5b57\u5178\u5b58\u50a8\u591a\u4e2a\u7ed3\u679c<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u8d4b\u503c<\/strong>\uff1a\u5c06\u8ba1\u7b97\u7ed3\u679c\u76f4\u63a5\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 5 + 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0cresult\u53d8\u91cf\u4e2d\u5b58\u50a8\u7684\u503c\u662f8\u3002\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u5408\u7b80\u5355\u7684\u8ba1\u7b97\u6216\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u5c06\u8fd0\u7b97\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf\u7684\u6700\u76f4\u63a5\u65b9\u5f0f\u3002\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 20<\/p>\n<p>result = a + b<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u5f53\u4f60\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8fd0\u7b97\u7ed3\u679c\u9700\u8981\u5b58\u50a8\u65f6\uff0c\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u53d8\u91cf\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>Python\u8fd8\u652f\u6301\u5c06\u591a\u4e2a\u503c\u540c\u65f6\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x, y = 5, 10<\/p>\n<p>sum_result = x + y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\u4e00\u6837\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c<\/h2>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u4f4d\u4e4b\u4e00\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u67d0\u4e2a\u503c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u7136\u540e\uff0c\u5c06\u8fd9\u4e2a\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_numbers(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add_numbers(10, 20)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u590d\u7528\u4ee3\u7801\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u591a\u6b21\u6267\u884c\u76f8\u540c\u7684\u8ba1\u7b97\u65f6\uff0c\u4f7f\u7528\u51fd\u6570\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u5d4c\u5957\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u7528\u4e8e\u53e6\u4e00\u4e2a\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>def add_and_multiply(a, b, c):<\/p>\n<p>    return multiply(a + b, c)<\/p>\n<p>result = add_and_multiply(2, 3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u901a\u8fc7\u5217\u8868\u6216\u5b57\u5178\u5b58\u50a8\u591a\u4e2a\u7ed3\u679c<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5b58\u50a8\u591a\u4e2a\u7ed3\u679c\u65f6\uff0c\u5217\u8868\u548c\u5b57\u5178\u662f\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5217\u8868\u9002\u5408\u5b58\u50a8\u6709\u5e8f\u7684\u7ed3\u679c\u96c6\uff0c\u800c\u5b57\u5178\u9002\u5408\u5b58\u50a8\u5177\u6709\u952e\u503c\u5bf9\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u5217\u8868<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">results = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    results.append(i * 2)<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a[0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7for\u5faa\u73af\u8ba1\u7b97\u4e86\u591a\u4e2a\u7ed3\u679c\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u5b57\u5178<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">results = {}<\/p>\n<p>for i in range(5):<\/p>\n<p>    results[f&#39;key_{i}&#39;] = i * 2<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a{&#39;key_0&#39;: 0, &#39;key_1&#39;: 2, &#39;key_2&#39;: 4, &#39;key_3&#39;: 6, &#39;key_4&#39;: 8}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u7ed3\u679c\uff0c\u7279\u522b\u662f\u5728\u7ed3\u679c\u5177\u6709\u7279\u5b9a\u542b\u4e49\u6216\u6807\u7b7e\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u548c\u64cd\u4f5c\u65f6\uff0c\u7c7b\u548c\u5bf9\u8c61\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u5c01\u88c5\u6570\u636e\u548c\u65b9\u6cd5\uff0c\u7136\u540e\u901a\u8fc7\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u64cd\u4f5c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Calculator:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.result = 0<\/p>\n<p>    def add(self, a, b):<\/p>\n<p>        self.result = a + b<\/p>\n<p>    def get_result(self):<\/p>\n<p>        return self.result<\/p>\n<p>calc = Calculator()<\/p>\n<p>calc.add(10, 20)<\/p>\n<p>print(calc.get_result())  # \u8f93\u51fa\u7ed3\u679c\u4e3a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u6a21\u5757\u5316\u548c\u7ec4\u7ec7\u826f\u597d\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u6570\u636e\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><p>Python\u62e5\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u7b80\u5316\u5404\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>result = np.sum(array)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;a&#39;: [1, 2, 3], &#39;b&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>result = df[&#39;a&#39;].sum()<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u6570\u636e\u7ed3\u6784\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u4f60\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u4ea7\u751f\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8ba1\u7b97\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        yield i * 2<\/p>\n<p>gen = generate_numbers(5)<\/p>\n<p>results = list(gen)<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a[0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h2>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4ee3\u7801\u5757\u6267\u884c\u524d\u540e\u81ea\u52a8\u6267\u884c\u4e00\u4e9b\u8bbe\u7f6e\u548c\u6e05\u7406\u5de5\u4f5c\u3002\u6700\u5e38\u89c1\u7684\u4f8b\u5b50\u662f\u6587\u4ef6\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    result = 5 + 3<\/p>\n<p>    file.write(str(result))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516b\u3001\u4f7f\u7528\u5e76\u53d1\u548c\u591a\u7ebf\u7a0b<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5e76\u53d1\u6216\u591a\u7ebf\u7a0b\u6765\u52a0\u901f\u8ba1\u7b97\u6216\u5904\u7406I\/O\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86<code>concurrent.futures<\/code>\u6a21\u5757\u6765\u7b80\u5316\u8fd9\u7c7b\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def compute(x):<\/p>\n<p>    return x * 2<\/p>\n<p>with ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    futures = [executor.submit(compute, i) for i in range(5)]<\/p>\n<p>    results = [f.result() for f in futures]<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a[0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u7279\u522b\u662f\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e5d\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684Python\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u589e\u5f3a\u5176\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log_result(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&#39;Result: {result}&#39;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@log_result<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(10, 20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u81ea\u52a8\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4f8b\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u76d1\u63a7\u7b49\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u5341\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u548c\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u7b80\u5316\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = [i * 2 for i in range(5)]<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a[0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528\u5185\u7f6e\u7684<code>map<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = list(map(lambda x: x * 2, range(5)))<\/p>\n<p>print(results)  # \u8f93\u51fa\u7ed3\u679c\u4e3a[0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u548c\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u53d8\u91cf\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u51fd\u6570\u7684\u8f93\u51fa\u8d4b\u503c\u7ed9\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u6cd5\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n\nresult = add(5, 3)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>result<\/code>\u53d8\u91cf\u5c06\u5b58\u50a8\u51fd\u6570<code>add<\/code>\u7684\u8fd4\u56de\u503c\uff0c\u53738\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06\u6253\u5370\u7684\u7ed3\u679c\u5b58\u50a8\u4e3a\u53d8\u91cf\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u5c06\u6253\u5370\u7684\u7ed3\u679c\u5b58\u50a8\u4e3a\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>io<\/code>\u6a21\u5757\u4e2d\u7684<code>StringIO<\/code>\u5bf9\u8c61\u6765\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import io\nimport sys\n\n# \u521b\u5efa\u4e00\u4e2aStringIO\u5bf9\u8c61\noutput = io.StringIO()\n# \u4e34\u65f6\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\nsys.stdout = output\n\nprint(&quot;Hello, World!&quot;)  # \u8fd9\u6761\u4fe1\u606f\u5c06\u88ab\u6355\u83b7\n\n# \u6062\u590d\u6807\u51c6\u8f93\u51fa\nsys.stdout = sys.__stdout__\n\n# \u83b7\u53d6\u6355\u83b7\u7684\u8f93\u51fa\nresult = output.getvalue()\noutput.close()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>result<\/code>\u53d8\u91cf\u5c06\u5b58\u50a8\u88ab\u6253\u5370\u7684\u5b57\u7b26\u4e32\u201cHello, World!\u201d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u6216\u5b57\u5178\u7684\u7ed3\u679c\u4fdd\u5b58\u5230\u53d8\u91cf\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u6216\u5b57\u5178\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\u6216\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5217\u8868\u793a\u4f8b\nmy_list = [1, 2, 3, 4, 5]\n\n# \u5b57\u5178\u793a\u4f8b\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_list<\/code>\u548c<code>my_dict<\/code>\u53d8\u91cf\u5206\u522b\u5b58\u50a8\u4e00\u4e2a\u5217\u8868\u548c\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u53d8\u91cf\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u3001\u901a\u8fc7\u5217\u8868\u6216\u5b57\u5178 [&hellip;]","protected":false},"author":3,"featured_media":1094259,"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\/1094252"}],"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=1094252"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094252\/revisions"}],"predecessor-version":[{"id":1094260,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094252\/revisions\/1094260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094259"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}