{"id":1030625,"date":"2024-12-31T11:18:40","date_gmt":"2024-12-31T03:18:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030625.html"},"modified":"2024-12-31T11:18:42","modified_gmt":"2024-12-31T03:18:42","slug":"python%e5%a6%82%e4%bd%95%e9%9a%94%e5%bc%80%e5%8f%a6%e4%b8%80%e6%ae%b5%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1030625.html","title":{"rendered":"python\u5982\u4f55\u9694\u5f00\u53e6\u4e00\u6bb5\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/757d35dc-fafb-4719-bbaa-ea8d454e3c3f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u9694\u5f00\u53e6\u4e00\u6bb5\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u7f29\u8fdb\u3001\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u7b49<\/strong>\u662fPython\u4e2d\u9694\u5f00\u53e6\u4e00\u6bb5\u4ee3\u7801\u7684\u5e38\u89c1\u65b9\u5f0f\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u51fd\u6570\u3002\u51fd\u6570\u5728Python\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4eec\u6709\u52a9\u4e8e\u7ed3\u6784\u5316\u4ee3\u7801\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u51fd\u6570\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u662f\u4e00\u79cd\u5c01\u88c5\u4ee3\u7801\u5757\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u8c03\u7528\u3002\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528 <code>def<\/code> \u5173\u952e\u5b57\uff0c\u540e\u8ddf\u51fd\u6570\u540d\u79f0\u548c\u5706\u62ec\u53f7\u3002\u5728\u51fd\u6570\u5185\u90e8\uff0c\u4ee3\u7801\u9700\u8981\u7f29\u8fdb\u3002\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u540d\u79f0\uff0c\u53ef\u4ee5\u8fd0\u884c\u5176\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(&quot;This is a separated block of code.&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_function<\/code> \u5b9a\u4e49\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u901a\u8fc7\u8c03\u7528 <code>my_function()<\/code>\uff0c\u53ef\u4ee5\u6267\u884c\u8be5\u6bb5\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u9694\u5f00\u4ee3\u7801\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7f29\u8fdb<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4ee3\u7801\u7684\u7f29\u8fdb\u975e\u5e38\u91cd\u8981\u3002\u7f29\u8fdb\u4e0d\u4ec5\u4ec5\u662f\u4e3a\u4e86\u7f8e\u89c2\uff0c\u5b83\u5b9e\u9645\u4e0a\u5b9a\u4e49\u4e86\u4ee3\u7801\u7684\u5757\u3002Python\u4f7f\u7528\u7f29\u8fdb\u6765\u8868\u793a\u4ee3\u7801\u5757\u7684\u5c42\u6b21\u7ed3\u6784\uff0c\u4e0d\u540c\u7684\u7f29\u8fdb\u7ea7\u522b\u8868\u793a\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><h4>\u7f29\u8fdb\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6bcf\u884c\u4ee3\u7801\u7684\u5f00\u5934\u53ef\u4ee5\u6709\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff0c\u901a\u5e38\u4f7f\u7528\u56db\u4e2a\u7a7a\u683c\u6765\u8868\u793a\u4e00\u4e2a\u5c42\u6b21\u7684\u7f29\u8fdb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if True:<\/p>\n<p>    print(&quot;This is an indented block of code.&quot;)<\/p>\n<p>    print(&quot;This is part of the same block.&quot;)<\/p>\n<p>print(&quot;This is outside the block.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a <code>print<\/code> \u8bed\u53e5\u56e0\u4e3a\u7f29\u8fdb\u76f8\u540c\uff0c\u6240\u4ee5\u5c5e\u4e8e\u540c\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u800c\u6700\u540e\u4e00\u4e2a <code>print<\/code> \u8bed\u53e5\u56e0\u4e3a\u6ca1\u6709\u7f29\u8fdb\uff0c\u6240\u4ee5\u4e0d\u5c5e\u4e8e <code>if<\/code> \u8bed\u53e5\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><h4>\u7f29\u8fdb\u7684\u4f7f\u7528\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u5efa\u8bae\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u7f29\u8fdb\u98ce\u683c\u3002\u5927\u591a\u6570Python\u5f00\u53d1\u8005\u63a8\u8350\u4f7f\u7528\u56db\u4e2a\u7a7a\u683c\u6765\u8868\u793a\u4e00\u4e2a\u7f29\u8fdb\u7ea7\u522b\uff0c\u907f\u514d\u4f7f\u7528\u5236\u8868\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u5b9a\u4e49\u72ec\u7acb\u4ee3\u7801\u5757\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u9694\u79bb\u4ee3\u7801\u5757\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u7684\u5b9a\u4e49\u4f7f\u7528 <code>def<\/code> \u5173\u952e\u5b57\uff0c\u7d27\u8ddf\u51fd\u6570\u540d\u79f0\u548c\u5706\u62ec\u53f7\u3002\u5728\u51fd\u6570\u5185\u90e8\uff0c\u4ee3\u7801\u9700\u8981\u7f29\u8fdb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5b9a\u4e49\u548c\u8c03\u7528\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p>greet(&quot;Bob&quot;)<\/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\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <code>name<\/code>\u3002\u8c03\u7528 <code>greet<\/code> \u51fd\u6570\u65f6\uff0c\u4f20\u5165\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\uff0c\u901a\u8fc7 <code>return<\/code> \u5173\u952e\u5b57\u8fd4\u56de\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e26\u8fd4\u56de\u503c\u7684\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p>print(result)<\/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>add<\/code> \u7684\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e24\u4e2a\u53c2\u6570\u7684\u548c\u3002\u8c03\u7528 <code>add<\/code> \u51fd\u6570\u540e\uff0c\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u53d8\u91cf <code>result<\/code> \u4e2d\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7c7b<\/h3>\n<\/p>\n<p><p>\u7c7b\u662fPython\u4e2d\u5b9a\u4e49\u5bf9\u8c61\u548c\u5c01\u88c5\u4ee3\u7801\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u3002\u7c7b\u5141\u8bb8\u6211\u4eec\u5c06\u6570\u636e\u548c\u529f\u80fd\u7ec4\u7ec7\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u66f4\u590d\u6742\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>\u5b9a\u4e49\u548c\u5b9e\u4f8b\u5316\u7c7b<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u5b9a\u4e49\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\uff0c\u7d27\u8ddf\u7c7b\u540d\u79f0\u548c\u5192\u53f7\u3002\u5728\u7c7b\u5185\u90e8\uff0c\u5b9a\u4e49\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u5b9a\u4e49\u548c\u5b9e\u4f8b\u5316\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, my name is {self.name} and I am {self.age} years old.&quot;)<\/p>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person1.greet()<\/p>\n<p>person2 = Person(&quot;Bob&quot;, 25)<\/p>\n<p>person2.greet()<\/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>Person<\/code> \u7684\u7c7b\uff0c\u5e76\u5305\u542b\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5 <code>__init__<\/code> \u548c\u4e00\u4e2a\u65b9\u6cd5 <code>greet<\/code>\u3002\u901a\u8fc7\u5b9e\u4f8b\u5316 <code>Person<\/code> \u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u7c7b\u7684\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u7ee7\u627f\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\uff0c\u5e76\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ee7\u627f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Employee(Person):<\/p>\n<p>    def __init__(self, name, age, employee_id):<\/p>\n<p>        super().__init__(name, age)<\/p>\n<p>        self.employee_id = employee_id<\/p>\n<p>    def work(self):<\/p>\n<p>        print(f&quot;{self.name} is working with employee ID: {self.employee_id}.&quot;)<\/p>\n<p>employee = Employee(&quot;Charlie&quot;, 28, &quot;E12345&quot;)<\/p>\n<p>employee.greet()<\/p>\n<p>employee.work()<\/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>Employee<\/code> \u7684\u7c7b\uff0c\u5e76\u7ee7\u627f\u4e86 <code>Person<\/code> \u7c7b\u3002\u901a\u8fc7\u8c03\u7528 <code>super()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u662f\u63a7\u5236\u4ee3\u7801\u6267\u884c\u6d41\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><h4>if \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>if<\/code> \u8bed\u53e5\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 <code>if<\/code> \u8bed\u53e5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>if x &gt; 5:<\/p>\n<p>    print(&quot;x is greater than 5.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u53d8\u91cf <code>x<\/code> \u7684\u503c\u5927\u4e8e 5\uff0c\u5219\u6253\u5370\u76f8\u5e94\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>if-elif-else \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>if-elif-else<\/code> \u8bed\u53e5\u5141\u8bb8\u6211\u4eec\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\uff0c\u5e76\u6839\u636e\u6761\u4ef6\u7684\u4e0d\u540c\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>if x &gt; 15:<\/p>\n<p>    print(&quot;x is greater than 15.&quot;)<\/p>\n<p>elif x &gt; 5:<\/p>\n<p>    print(&quot;x is greater than 5 but less than or equal to 15.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;x is 5 or less.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u68c0\u67e5\u4e86\u591a\u4e2a\u6761\u4ef6\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u7684\u91cd\u8981\u5de5\u5177\u3002Python\u63d0\u4f9b\u4e86 <code>for<\/code> \u5faa\u73af\u548c <code>while<\/code> \u5faa\u73af\u4e24\u79cd\u4e3b\u8981\u7684\u5faa\u73af\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>for \u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>for<\/code> \u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 <code>for<\/code> \u5faa\u73af\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>for number in numbers:<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u904d\u5386\u5217\u8868 <code>numbers<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>while \u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>while<\/code> \u5faa\u73af\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 <code>while<\/code> \u5faa\u73af\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u8981\u53d8\u91cf <code>count<\/code> \u7684\u503c\u5c0f\u4e8e 5\uff0c\u5c31\u4f1a\u91cd\u590d\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5757\uff0c\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u540e\u5c06 <code>count<\/code> \u7684\u503c\u52a0 1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u7a0b\u5e8f\u9519\u8bef\u548c\u5f02\u5e38\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u8fc7\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>try-except \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>try-except<\/code> \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u5e38\u5904\u7406\u793a\u4f8b\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;Division by zero is not allowed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c1d\u8bd5\u6267\u884c\u9664\u4ee5\u96f6\u7684\u64cd\u4f5c\uff0c\u5e76\u6355\u83b7 <code>ZeroDivisionError<\/code> \u5f02\u5e38\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>try-except-finally \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>try-except-finally<\/code> \u8bed\u53e5\u5141\u8bb8\u6211\u4eec\u5728\u5f02\u5e38\u5904\u7406\u540e\u6267\u884c\u6e05\u7406\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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>    content = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found.&quot;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c1d\u8bd5\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u6355\u83b7 <code>FileNotFoundError<\/code> \u5f02\u5e38\uff0c\u5e76\u5728 <code>finally<\/code> \u4ee3\u7801\u5757\u4e2d\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5305<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u548c\u5305\u662f\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u8fc7\u5c06\u4ee3\u7801\u5206\u5272\u6210\u4e0d\u540c\u7684\u6a21\u5757\u548c\u5305\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>import<\/code> \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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\u5bfc\u5165\u4e86 <code>math<\/code> \u6a21\u5757\uff0c\u5e76\u4f7f\u7528 <code>sqrt<\/code> \u51fd\u6570\u8ba1\u7b97\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u548c\u5bfc\u5165\u5305<\/h4>\n<\/p>\n<p><p>\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u8fc7\u521b\u5efa <code>__init__.py<\/code> \u6587\u4ef6\u6765\u5b9a\u4e49\u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u548c\u5bfc\u5165\u5305\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Directory structure:<\/p>\n<h2><strong>my_package\/<\/strong><\/h2>\n<h2><strong>    __init__.py<\/strong><\/h2>\n<h2><strong>    module1.py<\/strong><\/h2>\n<h2><strong>    module2.py<\/strong><\/h2>\n<h2><strong>module1.py<\/strong><\/h2>\n<p>def function1():<\/p>\n<p>    print(&quot;This is function1 from module1.&quot;)<\/p>\n<h2><strong>module2.py<\/strong><\/h2>\n<p>def function2():<\/p>\n<p>    print(&quot;This is function2 from module2.&quot;)<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>from my_package import module1, module2<\/p>\n<p>module1.function1()<\/p>\n<p>module2.function2()<\/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>my_package<\/code> \u7684\u5305\uff0c\u5e76\u5305\u542b\u4e24\u4e2a\u6a21\u5757 <code>module1<\/code> \u548c <code>module2<\/code>\u3002\u901a\u8fc7 <code>from ... import ...<\/code> \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u6ce8\u91ca\u662f\u89e3\u91ca\u4ee3\u7801\u7684\u91cd\u8981\u5de5\u5177\uff0c\u901a\u8fc7\u6dfb\u52a0\u6ce8\u91ca\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u884c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u5355\u884c\u6ce8\u91ca\u4f7f\u7528 <code>#<\/code> \u7b26\u53f7\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># This is a single-line comment<\/p>\n<p>print(&quot;Hello, World!&quot;)  # This is an inline comment<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u884c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u591a\u884c\u6ce8\u91ca\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5355\u884c\u6ce8\u91ca\uff0c\u6216\u8005\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># This is a multi-line comment<\/p>\n<h2><strong>Line 2 of the comment<\/strong><\/h2>\n<h2><strong>Line 3 of the comment<\/strong><\/h2>\n<p>&quot;&quot;&quot;<\/p>\n<p>This is a multi-line comment<\/p>\n<p>using triple quotes.<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c55\u793a\u4e86\u4e24\u79cd\u591a\u884c\u6ce8\u91ca\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u9694\u5f00\u4e0d\u540c\u7684\u4ee3\u7801\u6bb5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u7f29\u8fdb\u3001\u51fd\u6570\u3001\u7c7b\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u3001\u5f02\u5e38\u5904\u7406\u3001\u6a21\u5757\u548c\u5305\uff0c\u8fd8\u662f\u6ce8\u91ca\uff0c\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u5de7\u90fd\u662fPython\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u7ec4\u7ec7\u4ee3\u7801\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u3001\u7c7b\u548c\u6a21\u5757\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002\u901a\u8fc7\u5c06\u76f8\u5173\u4ee3\u7801\u5c01\u88c5\u5728\u51fd\u6570\u6216\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002\u6a21\u5757\u5316\u7f16\u7a0b\u8fd8\u53ef\u4ee5\u5c06\u4e0d\u540c\u529f\u80fd\u7684\u4ee3\u7801\u5206\u5f00\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u6b64\u5916\uff0c\u6dfb\u52a0\u9002\u5f53\u7684\u6ce8\u91ca\u548c\u6587\u6863\u5b57\u7b26\u4e32\u4e5f\u80fd\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u7684\u7528\u9014\u548c\u903b\u8f91\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6ce8\u91ca\u6765\u9694\u5f00\u4e0d\u540c\u7684\u4ee3\u7801\u6bb5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5355\u884c\u6ce8\u91ca\uff08#\uff09\u6216\u591a\u884c\u6ce8\u91ca\uff08\u4f7f\u7528\u4e09\u4e2a\u5f15\u53f7\uff09\u6765\u9694\u5f00\u4ee3\u7801\u6bb5\u3002\u6ce8\u91ca\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u89e3\u91ca\u4ee3\u7801\u7684\u903b\u8f91\uff0c\u8fd8\u80fd\u5728\u89c6\u89c9\u4e0a\u5c06\u4e0d\u540c\u7684\u90e8\u5206\u5206\u5f00\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6574\u6d01\u3002\u5728\u7f16\u5199\u590d\u6742\u903b\u8f91\u65f6\uff0c\u9002\u5f53\u7684\u6ce8\u91ca\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7a7a\u884c\u6765\u5206\u9694\u4ee3\u7801\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7a7a\u884c\u53ef\u4ee5\u7528\u6765\u5206\u9694\u4e0d\u540c\u7684\u4ee3\u7801\u5757\uff0c\u4f7f\u5f97\u4ee3\u7801\u7684\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002\u901a\u5e38\uff0c\u5728\u51fd\u6570\u3001\u7c7b\u548c\u903b\u8f91\u6bb5\u843d\u4e4b\u95f4\u7559\u51fa\u7a7a\u884c\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5c3d\u7ba1\u7a7a\u884c\u4e0d\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u6267\u884c\uff0c\u4f46\u5b83\u4eec\u80fd\u663e\u8457\u6539\u5584\u4ee3\u7801\u7684\u6574\u4f53\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u7f29\u8fdb\u3001\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u7b49\u662fPython\u4e2d\u9694\u5f00\u53e6\u4e00\u6bb5\u4ee3\u7801\u7684\u5e38\u89c1\u65b9\u5f0f\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1030638,"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\/1030625"}],"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=1030625"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030625\/revisions"}],"predecessor-version":[{"id":1030641,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030625\/revisions\/1030641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030638"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}