{"id":1154990,"date":"2025-01-13T17:57:54","date_gmt":"2025-01-13T09:57:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154990.html"},"modified":"2025-01-13T17:57:57","modified_gmt":"2025-01-13T09:57:57","slug":"python%e5%a6%82%e4%bd%95%e5%85%a8%e9%83%a8%e6%98%be%e7%a4%ba%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154990.html","title":{"rendered":"python\u5982\u4f55\u5168\u90e8\u663e\u793a\u7ed3\u679c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184449\/d72bd037-de10-4762-bc44-08835aceec78.webp\" alt=\"python\u5982\u4f55\u5168\u90e8\u663e\u793a\u7ed3\u679c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u7f16\u7a0b\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u6216\u8005\u5728\u811a\u672c\u4e2d\u663e\u793a\u6240\u6709\u7ed3\u679c\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5728Jupyter Notebook\u4e2d\u4f7f\u7528display\u51fd\u6570\u3001\u4fee\u6539sys.stdout\u8bbe\u7f6e\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>print\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u663e\u793a\u8f93\u51fa\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8c03\u7528print\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u7684\u503c\u6216\u8005\u5176\u4ed6\u4fe1\u606f\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4f60\u770b\u5230\u6240\u6709\u7684\u4e2d\u95f4\u7ed3\u679c\u3002\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 20<\/p>\n<p>print(a)<\/p>\n<p>print(b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>print\u51fd\u6570<\/strong> \u662f\u6700\u57fa\u7840\u3001\u6700\u5e38\u7528\u7684\u8f93\u51fa\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5c06\u6307\u5b9a\u5185\u5bb9\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u591a\u4e2aprint\u8bed\u53e5\uff0c\u53ef\u4ee5\u9010\u6b65\u8f93\u51fa\u6bcf\u4e00\u6b65\u7684\u7ed3\u679c\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u7406\u89e3\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>y = 10<\/p>\n<p>result = x + y<\/p>\n<p>print(&quot;x:&quot;, x)<\/p>\n<p>print(&quot;y:&quot;, y)<\/p>\n<p>print(&quot;result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55Python\u73af\u5883\uff0c\u5305\u62ec\u811a\u672c\u3001\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u548cJupyter Notebook\u3002\u7136\u800c\uff0c\u5f53\u9700\u8981\u6253\u5370\u5927\u91cf\u53d8\u91cf\u6216\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0cprint\u51fd\u6570\u53ef\u80fd\u663e\u5f97\u7b28\u62d9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u901a\u8fc7\u5c06\u8868\u8fbe\u5f0f\u653e\u5728\u65b9\u62ec\u53f7\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8ba1\u7b97\u7ed3\u679c\u7684\u5217\u8868\uff0c\u5e76\u5c06\u5176\u76f4\u63a5\u663e\u793a\u51fa\u6765\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u591a\u6b21print\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u591a\u4e2a\u7ed3\u679c\u4ee5\u5217\u8868\u5f62\u5f0f\u663e\u793a\u7684\u60c5\u51b5\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5728Jupyter Notebook\u4e2d\u4f7f\u7528display\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u3002Jupyter Notebook\u4e2d\u7684display\u51fd\u6570\u53ef\u4ee5\u663e\u793a\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ecDataFrame\u3001\u56fe\u50cf\u3001HTML\u7b49\u3002\u4f7f\u7528display\u51fd\u6570\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from IPython.display import display<\/p>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6]<\/p>\n<p>})<\/p>\n<p>display(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5c55\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4fee\u6539sys.stdout\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7Python\u4ee3\u7801\u7684\u8f93\u51fa\u5e76\u5c06\u5176\u663e\u793a\u5728\u7279\u5b9a\u4f4d\u7f6e\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539sys.stdout\u7684\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002sys.stdout\u662f\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u6307\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u901a\u8fc7\u5c06sys.stdout\u91cd\u5b9a\u5411\u5230\u5176\u4ed6\u6587\u4ef6\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import io<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aStringIO\u5bf9\u8c61<\/strong><\/h2>\n<p>output = io.StringIO()<\/p>\n<h2><strong>\u5c06sys.stdout\u91cd\u5b9a\u5411\u5230StringIO\u5bf9\u8c61<\/strong><\/h2>\n<p>sys.stdout = output<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>content = output.getvalue()<\/p>\n<h2><strong>\u6062\u590dsys.stdout<\/strong><\/h2>\n<p>sys.stdout = sys.__stdout__<\/p>\n<p>print(&quot;Captured Output:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6355\u83b7\u8f93\u51fa\u5e76\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u7684\u573a\u666f\uff0c\u4f8b\u5982\u65e5\u5fd7\u8bb0\u5f55\u548c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u662f\u7ba1\u7406\u548c\u5b58\u50a8\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4fe1\u606f\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528logging\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u8f93\u51fa\u8bb0\u5f55\u5230\u6587\u4ef6\u6216\u5176\u4ed6\u5b58\u50a8\u4ecb\u8d28\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u548c\u8c03\u8bd5\u3002logging\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\uff08DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f<\/strong><\/h2>\n<p>logging.debug(&quot;This is a debug message&quot;)<\/p>\n<p>logging.info(&quot;This is an info message&quot;)<\/p>\n<p>logging.warning(&quot;This is a warning message&quot;)<\/p>\n<p>logging.error(&quot;This is an error message&quot;)<\/p>\n<p>logging.critical(&quot;This is a critical message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u957f\u671f\u8ddf\u8e2a\u7a0b\u5e8f\u8fd0\u884c\u72b6\u6001\u548c\u8c03\u8bd5\u590d\u6742\u95ee\u9898\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Jupyter Notebook\u4e2d\u7684\u9b54\u6cd5\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u6709\u4e00\u4e9b\u7279\u6b8a\u7684\u9b54\u6cd5\u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u663e\u793a\u7ed3\u679c\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>%matplotlib inline<\/code>\u53ef\u4ee5\u5728Notebook\u4e2d\u76f4\u63a5\u663e\u793aMatplotlib\u751f\u6210\u7684\u56fe\u50cf\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u9b54\u6cd5\u547d\u4ee4\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u5c55\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728Jupyter Notebook\u4e2d\u4f7f\u7528\u9b54\u6cd5\u547d\u4ee4\u663e\u793a\u56fe\u50cf<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>%matplotlib inline<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u548c\u4ea4\u4e92\u5f0f\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u88c5\u9970\u5668\u663e\u793a\u51fd\u6570\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7Python\u7279\u6027\uff0c\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u65f6\u81ea\u52a8\u663e\u793a\u5176\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def show_result(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@show_result<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>add(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u663e\u793a\u51fd\u6570\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u5de5\u5177\u662f\u5206\u6790\u548c\u89e3\u51b3\u4ee3\u7801\u95ee\u9898\u7684\u5229\u5668\u3002\u901a\u8fc7\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u503c\u548c\u7a0b\u5e8f\u72b6\u6001\uff0c\u4ece\u800c\u627e\u51fa\u95ee\u9898\u6240\u5728\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5305\u62ec\u5185\u7f6e\u7684pdb\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u7684IDE\uff08\u5982PyCharm\u3001VS Code\uff09\u8c03\u8bd5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def divide(a, b):<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6df1\u5165\u5206\u6790\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u548c\u89e3\u51b3\u590d\u6742\u95ee\u9898\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u8f93\u51fa\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u8f93\u51fa\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u901a\u7528\u7684display\u51fd\u6570\uff0c\u6839\u636e\u9700\u8981\u8f93\u51fa\u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display(value, label=None):<\/p>\n<p>    if label:<\/p>\n<p>        print(f&quot;{label}: {value}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(value)<\/p>\n<p>display(42, &quot;The answer&quot;)<\/p>\n<p>display([1, 2, 3], &quot;List&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u8f93\u51fa\u4e0d\u540c\u7c7b\u578b\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u590d\u6742\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u548c\u961f\u5217\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\uff0c\u901a\u8fc7\u5408\u9002\u7684\u7b97\u6cd5\u5b9e\u73b0\u9ad8\u6548\u8ba1\u7b97\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>queue = deque()<\/p>\n<p>queue.append(1)<\/p>\n<p>queue.append(2)<\/p>\n<p>queue.append(3)<\/p>\n<p>print(&quot;Queue:&quot;, queue)<\/p>\n<p>while queue:<\/p>\n<p>    print(&quot;Popped:&quot;, queue.popleft())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u6570\u636e\u548c\u5c55\u793a\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u901a\u8fc7with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class OutputContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Entering context&quot;)<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>with OutputContextManager():<\/p>\n<p>    print(&quot;Inside context&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u548c\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u901a\u8fc7yield\u8bed\u53e5\u751f\u6210\u4e00\u4e2a\u503c\u5e8f\u5217\u3002\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u9010\u6b65\u751f\u6210\u548c\u663e\u793a\u7ed3\u679c\uff0c\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u3002<\/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 i in countdown(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u751f\u6210\u548c\u663e\u793a\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u88c5\u9970\u5668\u663e\u793a\u51fd\u6570\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7Python\u7279\u6027\uff0c\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u65f6\u81ea\u52a8\u663e\u793a\u5176\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def show_result(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@show_result<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>add(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u663e\u793a\u51fd\u6570\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u5de5\u5177\u662f\u5206\u6790\u548c\u89e3\u51b3\u4ee3\u7801\u95ee\u9898\u7684\u5229\u5668\u3002\u901a\u8fc7\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u503c\u548c\u7a0b\u5e8f\u72b6\u6001\uff0c\u4ece\u800c\u627e\u51fa\u95ee\u9898\u6240\u5728\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5305\u62ec\u5185\u7f6e\u7684pdb\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u7684IDE\uff08\u5982PyCharm\u3001VS Code\uff09\u8c03\u8bd5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def divide(a, b):<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6df1\u5165\u5206\u6790\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u548c\u89e3\u51b3\u590d\u6742\u95ee\u9898\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u8f93\u51fa\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u8f93\u51fa\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u901a\u7528\u7684display\u51fd\u6570\uff0c\u6839\u636e\u9700\u8981\u8f93\u51fa\u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display(value, label=None):<\/p>\n<p>    if label:<\/p>\n<p>        print(f&quot;{label}: {value}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(value)<\/p>\n<p>display(42, &quot;The answer&quot;)<\/p>\n<p>display([1, 2, 3], &quot;List&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u8f93\u51fa\u4e0d\u540c\u7c7b\u578b\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u590d\u6742\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u548c\u961f\u5217\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\uff0c\u901a\u8fc7\u5408\u9002\u7684\u7b97\u6cd5\u5b9e\u73b0\u9ad8\u6548\u8ba1\u7b97\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>queue = deque()<\/p>\n<p>queue.append(1)<\/p>\n<p>queue.append(2)<\/p>\n<p>queue.append(3)<\/p>\n<p>print(&quot;Queue:&quot;, queue)<\/p>\n<p>while queue:<\/p>\n<p>    print(&quot;Popped:&quot;, queue.popleft())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u6570\u636e\u548c\u5c55\u793a\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u901a\u8fc7with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class OutputContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Entering context&quot;)<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>with OutputContextManager():<\/p>\n<p>    print(&quot;Inside context&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u548c\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u901a\u8fc7yield\u8bed\u53e5\u751f\u6210\u4e00\u4e2a\u503c\u5e8f\u5217\u3002\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u9010\u6b65\u751f\u6210\u548c\u663e\u793a\u7ed3\u679c\uff0c\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u3002<\/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 i in countdown(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u751f\u6210\u548c\u663e\u793a\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u6253\u5370\u957f\u5217\u8868\u6216\u5927\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5f53\u6253\u5370\u957f\u5217\u8868\u6216\u5927\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u9ed8\u8ba4\u53ea\u4f1a\u663e\u793a\u90e8\u5206\u5185\u5bb9\u3002\u4e3a\u4e86\u6253\u5370\u5168\u90e8\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pprint<\/code>\u6a21\u5757\u4e2d\u7684<code>pprint()<\/code>\u51fd\u6570\uff0c\u6216\u8005\u5728\u5217\u8868\u548c\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u6ca1\u6709\u88ab\u622a\u65ad\u3002\u5bf9\u4e8eNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u8bbe\u7f6e<code>numpy.set_printoptions(threshold=n)<\/code>\uff0c\u5c06<code>n<\/code>\u8bbe\u7f6e\u4e3a\u66f4\u5927\u7684\u503c\uff0c\u4ee5\u4fbf\u663e\u793a\u5b8c\u6574\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u663e\u793a\u5b8c\u6574\u8f93\u51fa\u800c\u4e0d\u88ab\u622a\u65ad\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u8f93\u51fa\u4f1a\u81ea\u52a8\u622a\u65ad\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eJupyter\u7684\u8f93\u51fa\u9009\u9879\u6765\u663e\u793a\u5b8c\u6574\u7ed3\u679c\u3002\u5728Notebook\u7684\u4ee3\u7801\u5355\u5143\u4e2d\uff0c\u4f7f\u7528<code>pd.set_option(&#39;display.max_rows&#39;, None)<\/code>\u548c<code>pd.set_option(&#39;display.max_columns&#39;, None)<\/code>\u6765\u663e\u793a\u5b8c\u6574\u7684DataFrame\u5185\u5bb9\uff0c\u6216\u8005\u4f7f\u7528<code>display()<\/code>\u51fd\u6570\u6765\u66f4\u597d\u5730\u5c55\u793a\u5927\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u957f\u5b57\u7b26\u4e32\u7684\u5168\u90e8\u5185\u5bb9\uff1f<\/strong><br \/>\u5bf9\u4e8e\u957f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u76f4\u63a5\u6253\u5370\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u8fc7\u957f\uff0c\u53ef\u80fd\u4f1a\u5728\u7ec8\u7aef\u4e2d\u88ab\u622a\u65ad\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5206\u6bb5\u6216\u4f7f\u7528<code>textwrap<\/code>\u6a21\u5757\u4e2d\u7684<code>fill()<\/code>\u51fd\u6570\uff0c\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u5bbd\u5ea6\u7684\u884c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>repr()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u67e5\u770b\u5b57\u7b26\u4e32\u7684\u539f\u59cb\u8868\u793a\uff0c\u5e2e\u52a9\u4e86\u89e3\u5176\u5b8c\u6574\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8fdb\u884c\u7f16\u7a0b\u65f6\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u6216\u8005\u5728\u811a\u672c\u4e2d\u663e\u793a\u6240\u6709\u7ed3\u679c\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528pri [&hellip;]","protected":false},"author":3,"featured_media":1154996,"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\/1154990"}],"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=1154990"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154990\/revisions"}],"predecessor-version":[{"id":1154998,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154990\/revisions\/1154998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154996"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}