{"id":1085299,"date":"2025-01-08T13:14:53","date_gmt":"2025-01-08T05:14:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085299.html"},"modified":"2025-01-08T13:14:57","modified_gmt":"2025-01-08T05:14:57","slug":"python%e5%a6%82%e4%bd%95%e5%8f%aa%e8%bf%90%e8%a1%8c%e4%b8%80%e8%a1%8c%e4%bb%a3%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085299.html","title":{"rendered":"python\u5982\u4f55\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195244\/92cb8a9c-309c-4bdd-943e-aff2cac5cead.webp\" alt=\"python\u5982\u4f55\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c\u3001\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3001\u4f7f\u7528exec\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c<\/strong>\u662f\u6700\u65b9\u4fbf\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python -c &quot;\u4f60\u7684\u4ee3\u7801&quot;<\/code>\u6765\u6267\u884c\u5355\u884c\u4ee3\u7801\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u6267\u884c\u7b80\u5355\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884cPython\u4ee3\u7801\u662f\u6700\u5feb\u901f\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python -c &quot;\u4f60\u7684\u4ee3\u7801&quot;<\/code>\u5373\u53ef\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u6267\u884c\u7b80\u5355\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -c &quot;print(&#39;Hello, World!&#39;)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u76f4\u63a5\u8f93\u51fa<code>Hello, World!<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e8e\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u7b80\u5355\u7684\u811a\u672c\u6216\u8fdb\u884c\u5feb\u901f\u6d4b\u8bd5\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c\u4ee3\u7801\u8fd8\u6709\u4e00\u4e2a\u663e\u8457\u7684\u4f18\u70b9\uff1a\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f20\u9012\u591a\u4e2aPython\u8bed\u53e5\uff0c\u53ea\u9700\u4f7f\u7528\u5206\u53f7<code>;<\/code>\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -c &quot;import sys; print(sys.version)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u5f53\u524d\u4f7f\u7528\u7684Python\u7248\u672c\u4fe1\u606f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u6267\u884c\u591a\u4e2a\u76f8\u5173\u7684Python\u8bed\u53e5\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff0c\u5141\u8bb8\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8f93\u5165\u548c\u6267\u884cPython\u4ee3\u7801\u3002\u542f\u52a8\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python<\/p>\n<p>Python 3.8.10 (default, May  3 2021, 08:55:58)<\/p>\n<p>[GCC 8.4.0] on linux<\/p>\n<p>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<\/p>\n<p>&gt;&gt;&gt; print(&#39;Hello, World!&#39;)<\/p>\n<p>Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9010\u884c\u8f93\u5165Python\u4ee3\u7801\uff0c\u6bcf\u884c\u4ee3\u7801\u8f93\u5165\u540e\u90fd\u4f1a\u7acb\u5373\u6267\u884c\u5e76\u663e\u793a\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5b9e\u9a8c\u548c\u8c03\u8bd5\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u7acb\u5373\u770b\u5230\u6bcf\u884c\u4ee3\u7801\u7684\u6267\u884c\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u7684\u53e6\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u4f60\u53ef\u4ee5\u5728\u5176\u4e2d\u5b9a\u4e49\u548c\u6d4b\u8bd5\u51fd\u6570\u3001\u7c7b\u548c\u6a21\u5757\uff0c\u800c\u65e0\u9700\u521b\u5efa\u548c\u4fdd\u5b58\u5b8c\u6574\u7684\u811a\u672c\u6587\u4ef6\u3002\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u5f00\u53d1\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528exec\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u6765\u52a8\u6001\u6267\u884c\u4e00\u6bb5Python\u4ee3\u7801\u3002<code>exec<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u8be5\u5b57\u7b26\u4e32\u5305\u542b\u8981\u6267\u884c\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">code = &quot;print(&#39;Hello, World!&#39;)&quot;<\/p>\n<p>exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa<code>Hello, World!<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u7a0b\u5e8f\u4e2d\u52a8\u6001\u751f\u6210\u548c\u6267\u884c\u4ee3\u7801\u7247\u6bb5\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4f1a\u6267\u884c\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002\u5982\u679c\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u5305\u542b\u6076\u610f\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u5bf9\u7cfb\u7edf\u9020\u6210\u4e25\u91cd\u7684\u5b89\u5168\u5a01\u80c1\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u4ee3\u7801\u5b57\u7b26\u4e32\u6765\u81ea\u53ef\u4fe1\u4efb\u7684\u6765\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<code>exec<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u6765\u6267\u884c\u591a\u884c\u4ee3\u7801\uff0c\u53ea\u9700\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u591a\u884c\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528\u6362\u884c\u7b26\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">code = &quot;&quot;&quot;<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&#39;Hello, {name}!&#39;)<\/p>\n<p>greet(&#39;Alice&#39;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5b9a\u4e49\u4e00\u4e2a<code>greet<\/code>\u51fd\u6570\u5e76\u8c03\u7528\u5b83\uff0c\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u548c\u6267\u884c\u590d\u6742\u4ee3\u7801\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c\u3001\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3001\u4f7f\u7528exec\u51fd\u6570<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u6267\u884c\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5355\u884c\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u6216\u547d\u4ee4\u884c\u754c\u9762\u6765\u6267\u884c\u5355\u884c\u4ee3\u7801\u3002\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\u542f\u52a8\u89e3\u91ca\u5668\uff0c\u7136\u540e\u8f93\u5165\u60a8\u60f3\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>print(&quot;Hello, World!&quot;)<\/code>\u540e\u6309\u4e0bEnter\u952e\uff0c\u5373\u53ef\u770b\u5230\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff08\u4f8b\u5982<code>script.py<\/code>\uff09\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u53ea\u5199\u4e00\u884c\u4ee3\u7801\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello, World!&quot;)<\/code>\u3002\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u8be5\u811a\u672c\u65f6\uff0cPython\u5c06\u53ea\u6267\u884c\u8fd9\u4e00\u884c\u4ee3\u7801\u3002\u901a\u8fc7\u547d\u4ee4<code>python script.py<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u8fd0\u884c\u4e00\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u4efb\u610f\u4e00\u4e2a\u5355\u5143\u683c\u4e2d\u8f93\u5165\u4e00\u884c\u4ee3\u7801\uff0c\u7136\u540e\u6309Shift + Enter\u6267\u884c\u8be5\u5355\u5143\u683c\u3002\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u9a8c\u8bc1\u4ee3\u7801\u7684\u529f\u80fd\u6216\u6d4b\u8bd5\u67d0\u4e2a\u7279\u5b9a\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>x = 5<\/code>\u5e76\u6267\u884c\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u4e0b\u4e00\u4e2a\u5355\u5143\u683c\u4e2d\u4f7f\u7528\u53d8\u91cf<code>x<\/code>\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5229\u7528\u5355\u884c\u4ee3\u7801\u5b9e\u73b0\u590d\u6742\u529f\u80fd\uff1f<\/strong><br \/>\u5c3d\u7ba1Python\u7684\u8bbe\u8ba1\u7406\u5ff5\u63d0\u5021\u53ef\u8bfb\u6027\uff0c\u4f46\u60a8\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001lambda\u8868\u8fbe\u5f0f\u6216\u5185\u7f6e\u51fd\u6570\u6765\u5728\u4e00\u884c\u4e2d\u5b9e\u73b0\u590d\u6742\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>sum([x for x in range(10)])<\/code>\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u4ece0\u52309\u7684\u6240\u6709\u6570\u5b57\u4e4b\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5728\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\u7684\u540c\u65f6\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ea\u8fd0\u884c\u4e00\u884c\u4ee3\u7801\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6267\u884c\u3001\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3001\u4f7f\u7528exec\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1085312,"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\/1085299"}],"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=1085299"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085299\/revisions"}],"predecessor-version":[{"id":1085315,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085299\/revisions\/1085315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085312"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}