{"id":951900,"date":"2024-12-27T01:14:24","date_gmt":"2024-12-26T17:14:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/951900.html"},"modified":"2024-12-27T01:14:27","modified_gmt":"2024-12-26T17:14:27","slug":"python%e9%87%8c%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/951900.html","title":{"rendered":"python\u91cc\u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085928\/604e9c20-e2eb-4f35-a8f4-de0a9fd4f205.webp\" alt=\"python\u91cc\u5982\u4f55\u8c03\u7528\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\u3001\u6a21\u5757\u3001\u7c7b\u548c\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u548c\u5b9e\u4f8b\u5316\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u6a21\u5757\u5185\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3001\u5b9a\u4e49\u7c7b\u5e76\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3001\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8c03\u7528\u65b9\u5f0f\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u8f7b\u677e\u8c03\u7528\u6240\u9700\u7684\u529f\u80fd\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Python\u4e2d\u5982\u4f55\u8c03\u7528\u51fd\u6570\u3001\u6a21\u5757\u3001\u7c7b\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51fd\u6570\u7684\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u5143\u3002\u51fd\u6570\u7684\u8c03\u7528\u662f\u6307\u5728\u7a0b\u5e8f\u4e2d\u901a\u8fc7\u51fd\u6570\u540d\u6765\u6267\u884c\u51fd\u6570\u4f53\u5185\u7684\u4ee3\u7801\u3002Python\u5141\u8bb8\u7528\u6237\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u4f9b\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>1.1 \u5185\u7f6e\u51fd\u6570\u7684\u8c03\u7528<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>print()<\/code>\u3001<code>len()<\/code>\u3001<code>range()<\/code>\u7b49\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>print()<\/code>\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u8f93\u51fa\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>1.2 \u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u5b9a\u4e49\u5b8c\u6210\u540e\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u8fdb\u884c\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>message = greet(&quot;Alice&quot;)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>greet(&quot;Alice&quot;)<\/code>\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6a21\u5757\u7684\u8c03\u7528<\/p>\n<\/p>\n<p><p>Python\u7684\u6a21\u5757\u662f\u4e00\u79cd\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u72ec\u7acb\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u590d\u7528\u4ee3\u7801\u3002\u6a21\u5757\u901a\u5e38\u4ee5<code>.py<\/code>\u4e3a\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><p>2.1 \u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u5e76\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u6a21\u5757\u5185\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>math<\/code>\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165\u5e76\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>math.sqrt<\/code>\u662f\u8c03\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>2.2 \u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u6cd5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u4ece\u800c\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<p>result = sqrt(25)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86<code>sqrt<\/code>\u51fd\u6570\uff0c\u5e76\u76f4\u63a5\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7c7b\u7684\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u84dd\u56fe\u3002\u7c7b\u7684\u8c03\u7528\u6d89\u53ca\u5b9a\u4e49\u7c7b\u3001\u5b9e\u4f8b\u5316\u5bf9\u8c61\u4ee5\u53ca\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>3.1 \u7c7b\u7684\u5b9a\u4e49\u548c\u5b9e\u4f8b\u5316<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>class<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u901a\u8fc7\u7c7b\u521b\u5efa\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def bark(self):<\/p>\n<p>        return f&quot;{self.name} says woof!&quot;<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>my_dog = Dog(&quot;Buddy&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Dog<\/code>\u7c7b\uff0c\u5e76\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_dog<\/code>\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>3.2 \u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u8c61\u53ef\u4ee5\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sound = my_dog.bark()<\/p>\n<p>print(sound)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u8c03\u7528<code>my_dog<\/code>\u5bf9\u8c61\u7684<code>bark<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e86\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>Python\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><p>4.1 \u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pip install<\/code>\u547d\u4ee4\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5<code>requests<\/code>\u5e93\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4.2 \u5bfc\u5165\u548c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5e76\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u6a21\u5757\u548c\u5bf9\u8c61\u7684\u5c5e\u6027<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u70b9\u64cd\u4f5c\u7b26\uff08<code>.<\/code>\uff09\u7528\u4e8e\u8bbf\u95ee\u6a21\u5757\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>5.1 \u8bbf\u95ee\u6a21\u5757\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>pi_value = math.pi<\/p>\n<p>print(pi_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u8bbf\u95ee\u4e86<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>pi<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>5.2 \u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<p>    def drive(self):<\/p>\n<p>        return f&quot;The {self.model} is driving.&quot;<\/p>\n<p>my_car = Car(&quot;Tesla&quot;)<\/p>\n<p>print(my_car.model)<\/p>\n<p>print(my_car.drive())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8bbf\u95ee\u4e86<code>my_car<\/code>\u5bf9\u8c61\u7684<code>model<\/code>\u5c5e\u6027\u548c<code>drive<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528<code>__name__<\/code>\u548c<code>__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__<\/code>\u63a7\u5236\u6a21\u5757\u6267\u884c<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86<code>__name__<\/code>\u548c<code>__main__<\/code>\u673a\u5236\uff0c\u7528\u4e8e\u63a7\u5236\u6a21\u5757\u7684\u6267\u884c\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>6.1 <code>__name__<\/code>\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e2a<code>__name__<\/code>\u5c5e\u6027\uff0c\u7528\u4e8e\u6807\u8bc6\u6a21\u5757\u7684\u540d\u79f0\u3002\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u6267\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a<code>&quot;__main__&quot;<\/code>\uff0c\u5426\u5219\u4e3a\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<p><p>6.2 \u4f7f\u7528<code>if __name__ == &quot;__main__&quot;:<\/code>\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>if __name__ == &quot;__main__&quot;:<\/code>\u7ed3\u6784\u6765\u63a7\u5236\u6a21\u5757\u7684\u6267\u884c\u884c\u4e3a\uff0c\u4f7f\u5176\u4ec5\u5728\u88ab\u76f4\u63a5\u6267\u884c\u65f6\u8fd0\u884c\u7279\u5b9a\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    print(&quot;This script is being run directly.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4ec5\u5f53\u811a\u672c\u88ab\u76f4\u63a5\u6267\u884c\u65f6\uff0c<code>main()<\/code>\u51fd\u6570\u624d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5f02\u6b65\u8c03\u7528<\/p>\n<\/p>\n<p><p>Python\u652f\u6301\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>async<\/code>\u548c<code>await<\/code>\u5173\u952e\u5b57\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>7.1 \u5b9a\u4e49\u5f02\u6b65\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>async def<\/code>\u5b9a\u4e49\u5f02\u6b65\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data():<\/p>\n<p>    print(&quot;Fetching data...&quot;)<\/p>\n<p>    await asyncio.sleep(2)  # \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c<\/p>\n<p>    print(&quot;Data fetched.&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u51fd\u6570<\/strong><\/h2>\n<p>asyncio.run(fetch_data())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>fetch_data<\/code>\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>asyncio.run()<\/code>\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>7.2 \u5f02\u6b65\u4efb\u52a1\u7684\u5e76\u53d1\u6267\u884c<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>asyncio.gather()<\/code>\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def task1():<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;Task 1 complete.&quot;)<\/p>\n<p>async def task2():<\/p>\n<p>    await asyncio.sleep(2)<\/p>\n<p>    print(&quot;Task 2 complete.&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(task1(), task2())<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u662f\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u901a\u8fc7<code>asyncio.gather()<\/code>\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u9519\u8bef\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u548c\u5f02\u5e38\u3002Python\u63d0\u4f9b\u4e86\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>8.1 \u6355\u83b7\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u7ed3\u6784\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;Cannot divide by zero.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6355\u83b7\u4e86<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>8.2 \u4f7f\u7528<code>finally<\/code>\u5757<\/p>\n<\/p>\n<p><p><code>finally<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    file = open(&quot;example.txt&quot;, &quot;r&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found.&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;Execution complete.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e0\u8bba\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c<code>finally<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u4e2a\u65b9\u9762\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u5bf9Python\u4e2d\u7684\u8c03\u7528\u65b9\u5f0f\u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\u3002Python\u4ee5\u5176\u7b80\u6d01\u548c\u6613\u4e8e\u4f7f\u7528\u7684\u7279\u6027\uff0c\u4f7f\u5f97\u51fd\u6570\u3001\u6a21\u5757\u3001\u7c7b\u548c\u65b9\u6cd5\u7684\u8c03\u7528\u53d8\u5f97\u975e\u5e38\u9ad8\u6548\u548c\u7075\u6d3b\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u8c03\u7528\u65b9\u5f0f\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u638c\u63e1\u8fd9\u4e9b\u8c03\u7528\u6280\u5de7\u90fd\u5c06\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>import math<\/code>\u53ef\u4ee5\u5bfc\u5165\u6570\u5b66\u6a21\u5757\u3002\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.sqrt(16)<\/code>\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>from module_name import function_name<\/code>\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u8fd9\u6837\u5728\u8c03\u7528\u65f6\u65e0\u9700\u518d\u52a0\u6a21\u5757\u540d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u7528\u4e8e\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u3002\u5f53\u6267\u884c\u7684\u4ee3\u7801\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u653e\u5728<code>try<\/code>\u5757\u4e2d\u3002\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u63a7\u5236\u6d41\u4f1a\u8f6c\u5230<code>except<\/code>\u5757\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f7f\u7528<code>try:<\/code>\u540e\u8ddf\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\uff0c<code>except ValueError:<\/code>\u540e\u8ddf\u5904\u7406\u8be5\u9519\u8bef\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6355\u6349\u5e76\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c<code>def greet(name):<\/code>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\u3002\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u6267\u884c\u4ee3\u7801\u5e76\u8fd4\u56de\u503c\uff0c\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u3002\u8c03\u7528\u51fd\u6570\u65f6\u53ea\u9700\u4f7f\u7528\u5176\u540d\u79f0\u5e76\u4f20\u5165\u6240\u9700\u53c2\u6570\uff0c\u4f8b\u5982<code>greet(&quot;Alice&quot;)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u6a21\u5757\u5316\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u51fd\u6570\u3001\u6a21\u5757\u3001\u7c7b\u548c\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u548c\u5b9e\u4f8b\u5316\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528\u70b9\u64cd [&hellip;]","protected":false},"author":3,"featured_media":951907,"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\/951900"}],"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=951900"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951900\/revisions"}],"predecessor-version":[{"id":951908,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951900\/revisions\/951908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/951907"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=951900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=951900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=951900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}