{"id":1056961,"date":"2024-12-31T15:03:53","date_gmt":"2024-12-31T07:03:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056961.html"},"modified":"2024-12-31T15:03:55","modified_gmt":"2024-12-31T07:03:55","slug":"%e5%a6%82%e4%bd%95%e5%86%99%e4%b8%80%e4%b8%aapython%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056961.html","title":{"rendered":"\u5982\u4f55\u5199\u4e00\u4e2apython\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7fbd0b00-ade6-43bb-aec5-ba14b53714c7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5199\u4e00\u4e2apython\u811a\u672c\" \/><\/p>\n<p><p> <strong>\u8981\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u3001\u9009\u62e9\u4ee3\u7801\u7f16\u8f91\u5668\u3001\u7f16\u5199\u4ee3\u7801\u3001\u8fd0\u884c\u548c\u8c03\u8bd5\u4ee3\u7801\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u548c\u7406\u89e3\u57fa\u672c\u7684Python\u8bed\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\uff0c\u8fd8\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u4ee3\u7801\u3002Python\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\u53d8\u91cf\u3001\u6570\u636e\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u3001\u63a7\u5236\u6d41\u7b49\uff0c\u8fd9\u4e9b\u90fd\u662f\u7f16\u5199Python\u811a\u672c\u7684\u57fa\u7840\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4e00\u6b65\u4e00\u6b65\u5730\u5199\u4e00\u4e2aPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u3002Python\u662f\u4e00\u79cd\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u4e00\u4e2a\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u4f60\u7684\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84Python%E8%A7%A3%E9%87%8A%E5%99%A8%E3%80%82%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B%E9%9D%9E%E5%B8%B8%E7%AE%80%E5%8D%95%EF%BC%8C%E5%8F%AA%E9%9C%80%E6%8C%89%E7%85%A7%E5%AE%89%E8%A3%85%E5%90%91%E5%AF%BC%E7%9A%84%E6%8F%90%E7%A4%BA%E8%BF%9B%E8%A1%8C%E6%93%8D%E4%BD%9C%E5%8D%B3%E5%8F%AF%E3%80%82\">https:\/\/www.python.org\/\uff09\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\u89e3\u91ca\u5668\u3002\u5b89\u88c5\u8fc7\u7a0b\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u7684\u63d0\u793a\u8fdb\u884c\u64cd\u4f5c\u5373\u53ef\u3002<\/a><\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7dPython\u5b89\u88c5\u5305\u3002<\/li>\n<li>\u8fd0\u884c\u5b89\u88c5\u5305\uff0c\u6309\u7167\u63d0\u793a\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9009\u62e9\u4e86\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u4f7f\u4f60\u80fd\u591f\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8fd0\u884cPython\u547d\u4ee4\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u9009\u62e9\u4ee3\u7801\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u662f\u7f16\u5199Python\u811a\u672c\u7684\u91cd\u8981\u4e00\u6b65\u3002\u4e00\u4e2a\u597d\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u53ef\u4ee5\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u4ee3\u7801\u3002\u76ee\u524d\uff0c\u5e02\u9762\u4e0a\u6709\u8bb8\u591a\u6d41\u884c\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u53ef\u4f9b\u9009\u62e9\uff0c\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Visual Studio Code\uff08VS Code\uff09<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5f00\u6e90\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ecPython\u3002\u5b83\u6709\u8bb8\u591a\u6709\u7528\u7684\u6269\u5c55\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>PyCharm<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684Python IDE\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u4ee3\u7801\u8c03\u8bd5\u3001\u4ee3\u7801\u8865\u5168\u3001\u7248\u672c\u63a7\u5236\u7b49\uff0c\u975e\u5e38\u9002\u5408\u7f16\u5199\u590d\u6742\u7684Python\u9879\u76ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Sublime Text<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5177\u6709\u9ad8\u6548\u7684\u4ee3\u7801\u8865\u5168\u548c\u8bed\u6cd5\u9ad8\u4eae\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u7f16\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u597d\u4ee3\u7801\u7f16\u8f91\u5668\u5e76\u5b89\u88c5\u597dPython\u89e3\u91ca\u5668\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199Python\u811a\u672c\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u57fa\u672c\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># hello.py<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u7684\u529f\u80fd\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u5728\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u201cHello, World!\u201d\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u7684\u89e3\u91ca\uff1a<\/p>\n<\/p>\n<ol>\n<li><code>def main():<\/code>\uff1a\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u51fd\u6570\u3002<\/li>\n<li><code>print(&quot;Hello, World!&quot;)<\/code>\uff1a\u5728\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u201cHello, World!\u201d\u3002<\/li>\n<li><code>if __name__ == &quot;__main__&quot;:<\/code>\uff1a\u786e\u4fdd\u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884c\u8be5\u811a\u672c\u65f6\u624d\u4f1a\u8c03\u7528<code>main<\/code>\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u8fd0\u884c\u548c\u8c03\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u597d\u4ee3\u7801\u540e\uff0c\u4f60\u9700\u8981\u8fd0\u884c\u548c\u8c03\u8bd5\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cPython\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5e94\u8be5\u4f1a\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u201cHello, World!\u201d\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u8bd5\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u8c03\u8bd5\u662f\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\u3002\u901a\u8fc7\u8c03\u8bd5\uff0c\u4f60\u53ef\u4ee5\u53d1\u73b0\u5e76\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002\u8bb8\u591a\u4ee3\u7801\u7f16\u8f91\u5668\u548cIDE\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\u3002\u6bd4\u5982\uff0c\u5728Visual Studio Code\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7406\u89e3Python\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u65f6\uff0c\u7406\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e9bPython\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5305\u62ec\u53d8\u91cf\u3001\u6570\u636e\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u3001\u63a7\u5236\u6d41\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u5b58\u50a8\u6570\u636e\u3002Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u7684\u7c7b\u578b\u3002Python\u4f1a\u6839\u636e\u8d4b\u7ed9\u53d8\u91cf\u7684\u503c\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>is_student = True<\/p>\n<p>height = 1.68<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u56db\u4e2a\u53d8\u91cf\uff0c\u5206\u522b\u662f\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u5e03\u5c14\u503c\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8fd0\u7b97\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b97\u672f\u8fd0\u7b97\u7b26<\/p>\n<p>a = 10<\/p>\n<p>b = 3<\/p>\n<p>print(a + b)  # \u52a0\u6cd5<\/p>\n<p>print(a - b)  # \u51cf\u6cd5<\/p>\n<p>print(a * b)  # \u4e58\u6cd5<\/p>\n<p>print(a \/ b)  # \u9664\u6cd5<\/p>\n<p>print(a % b)  # \u53d6\u6a21<\/p>\n<p>print(a  b)  # \u5e42\u8fd0\u7b97<\/p>\n<h2><strong>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>print(a &gt; b)  # \u5927\u4e8e<\/p>\n<p>print(a &lt; b)  # \u5c0f\u4e8e<\/p>\n<p>print(a == b)  # \u7b49\u4e8e<\/p>\n<p>print(a != b)  # \u4e0d\u7b49\u4e8e<\/p>\n<h2><strong>\u903b\u8f91\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>x = True<\/p>\n<p>y = False<\/p>\n<p>print(x and y)  # \u903b\u8f91\u4e0e<\/p>\n<p>print(x or y)  # \u903b\u8f91\u6216<\/p>\n<p>print(not x)  # \u903b\u8f91\u975e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u63a7\u5236\u6d41<\/h4>\n<\/p>\n<p><p>\u63a7\u5236\u6d41\u8bed\u53e5\u7528\u4e8e\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002Python\u652f\u6301\u591a\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u5305\u62ec\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u8bed\u53e5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6761\u4ef6\u8bed\u53e5<\/p>\n<p>score = 85<\/p>\n<p>if score &gt;= 90:<\/p>\n<p>    print(&quot;\u4f18\u79c0&quot;)<\/p>\n<p>elif score &gt;= 75:<\/p>\n<p>    print(&quot;\u826f\u597d&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u53ca\u683c&quot;)<\/p>\n<h2><strong>\u5faa\u73af\u8bed\u53e5<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p>j = 0<\/p>\n<p>while j &lt; 5:<\/p>\n<p>    print(j)<\/p>\n<p>    j += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u548c\u5305<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u590d\u6742\u7684Python\u9879\u76ee\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9700\u8981\u5c06\u4ee3\u7801\u62c6\u5206\u6210\u591a\u4e2a\u6a21\u5757\u548c\u5305\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u4ee3\u7801\u3002\u6a21\u5757\u662f\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u51fd\u6570\u548c\u7c7b\u653e\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u4ee5\u4fbf\u5728\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import my_module<\/p>\n<p>my_module.greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6a21\u5757\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>greet<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6<code>main.py<\/code>\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u4e86\u8fd9\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5305<\/h4>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u6587\u4ef6\u5939\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5305\u6765\u7ec4\u7ec7\u76f8\u5173\u7684\u6a21\u5757\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\"># \u76ee\u5f55\u7ed3\u6784<\/p>\n<p>my_package\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<h2><strong>my_package\/module1.py<\/strong><\/h2>\n<p>def func1():<\/p>\n<p>    print(&quot;This is func1 from module1&quot;)<\/p>\n<h2><strong>my_package\/module2.py<\/strong><\/h2>\n<p>def func2():<\/p>\n<p>    print(&quot;This is func2 from module2&quot;)<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>from my_package import module1, module2<\/p>\n<p>module1.func1()<\/p>\n<p>module2.func2()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_package<\/code>\u7684\u5305\uff0c\u5e76\u5728\u5176\u4e2d\u5305\u542b\u4e86\u4e24\u4e2a\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6<code>main.py<\/code>\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u4e86\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>Python\u62e5\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u7b80\u5316\u7f16\u7a0b\u4efb\u52a1\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\uff08Python\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff09\u6765\u5b89\u88c5\u548c\u7ba1\u7406\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pip install<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002\u6bd4\u5982\uff0c\u5b89\u88c5<code>requests<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u597d\u7b2c\u4e09\u65b9\u5e93\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u5b83\u4eec\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&quot;https:\/\/api.github.com&quot;)<\/p>\n<p>print(response.status_code)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u6253\u5370\u4e86\u54cd\u5e94\u7684\u72b6\u6001\u7801\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7f16\u5199\u548c\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u662f\u4fdd\u8bc1\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u4fee\u6539\u540e\u4ecd\u7136\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002Python\u7684<code>unittest<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7f16\u5199\u548c\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>test_my_module.py<\/strong><\/h2>\n<p>import unittest<\/p>\n<p>from my_module import add<\/p>\n<p>class TestMyModule(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(2, 3), 5)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<code>add<\/code>\uff0c\u5e76\u4e3a\u5b83\u7f16\u5199\u4e86\u5355\u5143\u6d4b\u8bd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python test_my_module.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u6761\u6d88\u606f\u663e\u793a\u6240\u6709\u6d4b\u8bd5\u90fd\u901a\u8fc7\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7f16\u5199\u811a\u672c\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u7f16\u5199\u9ad8\u8d28\u91cf\u7684Python\u811a\u672c\uff0c\u4f60\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9075\u5faaPEP 8\u7f16\u7801\u89c4\u8303<\/strong>\uff1aPEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u89c4\u5b9a\u4e86\u4ee3\u7801\u7684\u683c\u5f0f\u548c\u98ce\u683c\u3002\u9075\u5faaPEP 8\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6574\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\uff1a\u4e3a\u51fd\u6570\u548c\u7c7b\u7f16\u5199\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u80fd\u591f\u7406\u89e3\u4f60\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u7248\u672c\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u6bd4\u5982Git\uff09\u6765\u7ba1\u7406\u4ee3\u7801\u7684\u4fee\u6539\u5386\u53f2\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/strong>\uff1a\u4e3a\u4ee3\u7801\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u5c3d\u91cf\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\uff0c\u907f\u514d\u8fc7\u5ea6\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u51fa\u9ad8\u8d28\u91cf\u3001\u6613\u7ef4\u62a4\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b66\u4e60\u672c\u6587\u7684\u5185\u5bb9\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u548c\u9009\u62e9\u5408\u9002\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u3002\u7136\u540e\uff0c\u7f16\u5199\u548c\u8fd0\u884c\u4ee3\u7801\u3002\u7406\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5e76\u5b66\u4f1a\u4f7f\u7528\u6a21\u5757\u3001\u5305\u548c\u7b2c\u4e09\u65b9\u5e93\u3002\u6700\u540e\uff0c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u548c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728\u7f16\u5199Python\u811a\u672c\u7684\u8fc7\u7a0b\u4e2d\u53d6\u5f97\u6210\u529f\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5f00\u53d1\u73af\u5883\uff1f<\/strong><br \/>\u5728\u7f16\u5199Python\u811a\u672c\u4e4b\u524d\uff0c\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684\u5f00\u53d1\u73af\u5883\u81f3\u5173\u91cd\u8981\u3002\u5e38\u89c1\u7684\u9009\u9879\u5305\u62ecIDLE\u3001PyCharm\u3001VS Code\u548cJupyter Notebook\u7b49\u3002IDLE\u662fPython\u81ea\u5e26\u7684\u5f00\u53d1\u73af\u5883\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1b\u800cPyCharm\u548cVS Code\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u9002\u5408\u8fdb\u884c\u5927\u578b\u9879\u76ee\u5f00\u53d1\u3002Jupyter Notebook\u5219\u975e\u5e38\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>Python\u811a\u672c\u7684\u57fa\u672c\u7ed3\u6784\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/strong><br \/>\u4e00\u4e2aPython\u811a\u672c\u901a\u5e38\u7531\u5bfc\u5165\u7684\u6a21\u5757\u3001\u51fd\u6570\u5b9a\u4e49\u3001\u7c7b\u5b9a\u4e49\u548c\u4e3b\u7a0b\u5e8f\u7ec4\u6210\u3002\u5bfc\u5165\u7684\u6a21\u5757\u53ef\u4ee5\u8ba9\u4f60\u4f7f\u7528\u5176\u4ed6\u5e93\u7684\u529f\u80fd\uff1b\u51fd\u6570\u5b9a\u4e49\u5219\u7528\u4e8e\u5c01\u88c5\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\uff1b\u7c7b\u5b9a\u4e49\u5e2e\u52a9\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u6700\u540e\uff0c\u4e3b\u7a0b\u5e8f\u4e00\u822c\u653e\u5728\u811a\u672c\u7684\u5e95\u90e8\uff0c\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u786e\u4fdd\u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884c\u811a\u672c\u65f6\u624d\u4f1a\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u548c\u6d4b\u8bd5Python\u811a\u672c\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u811a\u672c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u63d2\u5165\u6253\u5370\u8bed\u53e5\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684\u8c03\u8bd5\u5668pdb\uff0c\u6216\u5229\u7528IDE\u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u6d4b\u8bd5\u65b9\u9762\uff0c\u53ef\u4ee5\u4f7f\u7528unittest\u6216pytest\u7b49\u6846\u67b6\u6765\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u786e\u4fdd\u4f60\u7684\u811a\u672c\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u6d4b\u8bd5\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u53d1\u73b0\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u4ee3\u7801\u5728\u672a\u6765\u7684\u4fee\u6539\u4e2d\u4fdd\u6301\u7a33\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u3001\u9009\u62e9\u4ee3\u7801\u7f16\u8f91\u5668\u3001\u7f16\u5199\u4ee3\u7801\u3001\u8fd0\u884c\u548c\u8c03\u8bd5\u4ee3\u7801\u3002 \u5176\u4e2d\uff0c\u9009\u62e9 [&hellip;]","protected":false},"author":3,"featured_media":1056969,"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\/1056961"}],"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=1056961"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056961\/revisions"}],"predecessor-version":[{"id":1056972,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056961\/revisions\/1056972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056969"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}