{"id":1169783,"date":"2025-01-15T16:14:45","date_gmt":"2025-01-15T08:14:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169783.html"},"modified":"2025-01-15T16:14:48","modified_gmt":"2025-01-15T08:14:48","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99python%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169783.html","title":{"rendered":"\u5982\u4f55\u7f16\u5199python\u7a0b\u5e8f\u8bbe\u8ba1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070645\/98bcc516-e845-476a-ad43-9702ad8ea006.webp\" alt=\"\u5982\u4f55\u7f16\u5199python\u7a0b\u5e8f\u8bbe\u8ba1\" \/><\/p>\n<p><p> <strong>\u7f16\u5199Python\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u4e86\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\u3001\u638c\u63e1Python\u7684\u6570\u636e\u7ed3\u6784\u3001\u719f\u6089\u51fd\u6570\u548c\u6a21\u5757\u7684\u4f7f\u7528\u3001\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u638c\u63e1\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u6280\u672f\u3002<\/strong> \u5176\u4e2d\uff0c\u4e86\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u7f16\u5199Python\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u57fa\u7840\uff0c\u5b83\u5305\u62ec\u53d8\u91cf\u3001\u6570\u636e\u7c7b\u578b\u3001\u63a7\u5236\u7ed3\u6784\u7b49\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e86\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u638c\u63e1\u57fa\u672c\u8bed\u6cd5\u662f\u7f16\u5199Python\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002Python\u662f\u4e00\u79cd\u89e3\u91ca\u578b\u3001\u9762\u5411\u5bf9\u8c61\u7684\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u5177\u6709\u6e05\u6670\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u8bed\u6cd5\u7684\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u7528\u6765\u5b58\u50a8\u6570\u636e\u7684\u5bb9\u5668\u3002Python\u7684\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\u4f7f\u7528\u3002Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6574\u6570<\/p>\n<p>a = 10<\/p>\n<h2><strong>\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>b = 20.5<\/p>\n<h2><strong>\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>c = &#39;Hello, Python&#39;<\/p>\n<h2><strong>\u5217\u8868<\/strong><\/h2>\n<p>d = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5143\u7ec4<\/strong><\/h2>\n<p>e = (1, 2, 3, 4, 5)<\/p>\n<h2><strong>\u5b57\u5178<\/strong><\/h2>\n<p>f = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u63a7\u5236\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u63a7\u5236\u7ed3\u6784\u662f\u7f16\u7a0b\u7684\u6838\u5fc3\uff0c\u901a\u8fc7\u63a7\u5236\u7ed3\u6784\u53ef\u4ee5\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002Python\u652f\u6301\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6761\u4ef6\u8bed\u53e5<\/p>\n<p>if a &gt; b:<\/p>\n<p>    print(&#39;a is greater than b&#39;)<\/p>\n<p>elif a == b:<\/p>\n<p>    print(&#39;a is equal to b&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;a is less than b&#39;)<\/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>while a &gt; 0:<\/p>\n<p>    print(a)<\/p>\n<p>    a -= 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u638c\u63e1Python\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u4e86\u591a\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u662f\u7f16\u5199\u9ad8\u6548\u7a0b\u5e8f\u7684\u91cd\u8981\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u5b83\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u5373\u53ef\u4ee5\u52a8\u6001\u5730\u589e\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/strong><\/h2>\n<p>print(my_list[0])  # \u8f93\u51fa\uff1a1<\/p>\n<h2><strong>\u4fee\u6539\u5217\u8868\u5143\u7d20<\/strong><\/h2>\n<p>my_list[0] = 10<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[10, 2, 3, 4, 5]<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_list.append(6)<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[10, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>my_list.remove(10)<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u5b83\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\u3002\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>print(my_dict[&#39;name&#39;])  # \u8f93\u51fa\uff1aAlice<\/p>\n<h2><strong>\u4fee\u6539\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26}<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;gender&#39;] = &#39;female&#39;<\/p>\n<p>print(my_dict)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;gender&#39;: &#39;female&#39;}<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>del my_dict[&#39;age&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;gender&#39;: &#39;female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u719f\u6089\u51fd\u6570\u548c\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u548c\u6a21\u5757\u662fPython\u4e2d\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u4f4d\u3002\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u7ec4\u8bed\u53e5\u6253\u5305\u6210\u4e00\u4e2a\u5355\u5143\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u591a\u6b21\u8c03\u7528\u3002\u6a21\u5757\u662f\u5305\u542b\u4e00\u7ec4\u51fd\u6570\u548c\u53d8\u91cf\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7a0b\u5e8f\u4e2d\u590d\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u5b9e\u73b0\u4ee3\u7801\u91cd\u7528\u548c\u6a21\u5757\u5316\u7684\u57fa\u672c\u5355\u4f4d\u3002\u53ef\u4ee5\u4f7f\u7528def\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u51fd\u6570<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&#39;Hello, {name}!&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>greet(&#39;Alice&#39;)  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u662f\u5305\u542b\u4e00\u7ec4\u51fd\u6570\u548c\u53d8\u91cf\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528import\u5173\u952e\u5b57\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6a21\u5757 my_module.py<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&#39;Hello, {name}!&#39;)<\/p>\n<h2><strong>\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p>import my_module<\/p>\n<h2><strong>\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>my_module.greet(&#39;Alice&#39;)  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u5c01\u88c5\u5728\u5bf9\u8c61\u4e2d\u3002Python\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u5b83\u652f\u6301\u7c7b\u548c\u5bf9\u8c61\u7684\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u7c7b\u662f\u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u5b83\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5bf9\u8c61\u662f\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5b83\u5305\u542b\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7c7b<\/p>\n<p>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&#39;Hello, my name is {self.name} and I am {self.age} years old.&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&#39;Alice&#39;, 25)<\/p>\n<h2><strong>\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet()  # \u8f93\u51fa\uff1aHello, my name is Alice and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7c7b\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002Python\u652f\u6301\u5355\u7ee7\u627f\u548c\u591a\u7ee7\u627f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7236\u7c7b<\/p>\n<p>class Animal:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(f&#39;{self.name} is making a sound.&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u5b50\u7c7b<\/strong><\/h2>\n<p>class Dog(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(f&#39;{self.name} is barking.&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>dog = Dog(&#39;Buddy&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>dog.speak()  # \u8f93\u51fa\uff1aBuddy is barking.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u638c\u63e1\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u7a0b\u5e8f\u65f6\u96be\u514d\u4f1a\u9047\u5230\u9519\u8bef\u548c\u5f02\u5e38\uff0c\u638c\u63e1\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u6280\u672f\u662f\u63d0\u9ad8\u7a0b\u5e8f\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86try-except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u53ef\u4ee5\u5728try\u5757\u4e2d\u653e\u7f6e\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5728except\u5757\u4e2d\u5904\u7406\u5f02\u5e38\u3002<\/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(&#39;Error: Division by zero.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8c03\u8bd5\u6280\u672f<\/h4>\n<\/p>\n<p><p>\u8c03\u8bd5\u662f\u5b9a\u4f4d\u548c\u4fee\u590d\u7a0b\u5e8f\u9519\u8bef\u7684\u91cd\u8981\u6b65\u9aa4\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\u548c\u6280\u672f\uff0c\u5982print\u8c03\u8bd5\u3001\u65ad\u70b9\u8c03\u8bd5\u548c\u4f7f\u7528\u8c03\u8bd5\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528print\u8c03\u8bd5<\/p>\n<p>def divide(a, b):<\/p>\n<p>    print(f&#39;divide({a}, {b})&#39;)<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u638c\u63e1\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u64cd\u4f5c\u662f\u7f16\u5199Python\u7a0b\u5e8f\u7684\u91cd\u8981\u90e8\u5206\uff0c\u5305\u62ec\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u548c\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528read\u3001readline\u6216readlines\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u6309\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write\u6216writelines\u65b9\u6cd5\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, Python!&#39;)<\/p>\n<h2><strong>\u5199\u5165\u591a\u884c\u5185\u5bb9<\/strong><\/h2>\n<p>lines = [&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;, &#39;Line 3\\n&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u638c\u63e1Python\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u6a21\u5757\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u6ee1\u8db3\u65e5\u5e38\u7f16\u7a0b\u9700\u6c42\u3002\u9664\u6b64\u4e4b\u5916\uff0cPython\u8fd8\u6709\u5927\u91cf\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u6269\u5c55Python\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Python\u6807\u51c6\u5e93<\/h4>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u5305\u542b\u4e86\u8bb8\u591a\u5b9e\u7528\u7684\u6a21\u5757\uff0c\u5982math\u3001datetime\u3001os\u3001sys\u7b49\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>import datetime<\/p>\n<h2><strong>\u4f7f\u7528math\u6a21\u5757<\/strong><\/h2>\n<p>print(math.sqrt(16))  # \u8f93\u51fa\uff1a4.0<\/p>\n<h2><strong>\u4f7f\u7528datetime\u6a21\u5757<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<p>print(now)  # \u8f93\u51fa\uff1a\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93\u3002\u5e38\u7528\u7684\u7b2c\u4e09\u65b9\u5e93\u5305\u62ecNumPy\u3001Pandas\u3001Requests\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/p>\n<h2><strong>pip install numpy<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u5e93<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(array)  # \u8f93\u51fa\uff1a[1 2 3 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b66\u4e60\u6d4b\u8bd5\u548c\u6587\u6863\u64b0\u5199<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u6d4b\u8bd5\u548c\u6587\u6863\u662f\u7f16\u5199\u9ad8\u8d28\u91cfPython\u7a0b\u5e8f\u7684\u5173\u952e\u6b65\u9aa4\u3002\u6d4b\u8bd5\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\uff0c\u6587\u6863\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7f16\u5199\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86unittest\u6a21\u5757\u6765\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u3002\u53ef\u4ee5\u5b9a\u4e49\u6d4b\u8bd5\u7528\u4f8b\u548c\u6d4b\u8bd5\u65b9\u6cd5\u6765\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<h2><strong>\u88ab\u6d4b\u8bd5\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>\u5b9a\u4e49\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n<p>class TestAdd(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<h2><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/h2>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u64b0\u5199\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u64b0\u5199\u6587\u6863\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u53ef\u4ee5\u4f7f\u7528\u6ce8\u91ca\u548cdocstring\u6765\u64b0\u5199\u6587\u6863\u3002Python\u8fd8\u63d0\u4f9b\u4e86sphinx\u5de5\u5177\u6765\u751f\u6210\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Adds two numbers.<\/p>\n<p>    Parameters:<\/p>\n<p>    a (int): The first number.<\/p>\n<p>    b (int): The second number.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of the two numbers.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u638c\u63e1\u7f51\u7edc\u7f16\u7a0b\u548c\u5e76\u53d1\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u7f16\u7a0b\u548c\u5e76\u53d1\u7f16\u7a0b\u662f\u7f16\u5199\u9ad8\u7ea7Python\u7a0b\u5e8f\u7684\u91cd\u8981\u6280\u80fd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u548c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7f51\u7edc\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7f51\u7edc\u7f16\u7a0b\u6a21\u5757\uff0c\u5982socket\u3001requests\u7b49\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u548c\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u670d\u52a1\u5668<\/strong><\/h2>\n<p>server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>server.bind((&#39;localhost&#39;, 8080))<\/p>\n<p>server.listen(5)<\/p>\n<p>while True:<\/p>\n<p>    client, address = server.accept()<\/p>\n<p>    data = client.recv(1024)<\/p>\n<p>    if data:<\/p>\n<p>        print(f&#39;Received: {data.decode(&quot;utf-8&quot;)}&#39;)<\/p>\n<p>        client.send(data)<\/p>\n<p>    client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e76\u53d1\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e76\u53d1\u7f16\u7a0b\u6a21\u5757\uff0c\u5982threading\u3001multiprocessing\u7b49\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<h2><strong>\u5b9a\u4e49\u7ebf\u7a0b\u4efb\u52a1<\/strong><\/h2>\n<p>def task(name):<\/p>\n<p>    print(f&#39;Task {name} is running.&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    thread = threading.Thread(target=task, args=(i,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5b66\u4e60\u9ad8\u7ea7Python\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u638c\u63e1\u4e00\u4e9b\u9ad8\u7ea7Python\u6280\u5de7\uff0c\u53ef\u4ee5\u7f16\u5199\u66f4\u9ad8\u6548\u548c\u4f18\u96c5\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u662fPython\u4e2d\u5b9e\u73b0\u5ef6\u8fdf\u8ba1\u7b97\u548c\u8282\u7701\u5185\u5b58\u7684\u91cd\u8981\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528yield\u5173\u952e\u5b57\u5b9a\u4e49\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u751f\u6210\u5668<\/p>\n<p>def my_generator():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        yield i<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/h2>\n<p>gen = my_generator()<\/p>\n<p>for value in gen:<\/p>\n<p>    print(value)  # \u8f93\u51fa\uff1a0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u5b9e\u73b0\u4ee3\u7801\u91cd\u7528\u548c\u529f\u80fd\u6269\u5c55\u7684\u91cd\u8981\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528@\u8bed\u6cd5\u5b9a\u4e49\u88c5\u9970\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u88c5\u9970\u5668<\/p>\n<p>def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&#39;Before function call&#39;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&#39;After function call&#39;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<h2><strong>\u4f7f\u7528\u88c5\u9970\u5668<\/strong><\/h2>\n<p>@my_decorator<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&#39;Hello, {name}!&#39;)<\/p>\n<p>greet(&#39;Alice&#39;)  # \u8f93\u51fa\uff1aBefore function call, Hello, Alice!, After function call<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7f16\u5199Python\u7a0b\u5e8f\u8bbe\u8ba1\u6d89\u53ca\u591a\u4e2a\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u5305\u62ec\u4e86\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\u3001\u638c\u63e1Python\u7684\u6570\u636e\u7ed3\u6784\u3001\u719f\u6089\u51fd\u6570\u548c\u6a21\u5757\u7684\u4f7f\u7528\u3001\u5b66\u4e60\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u638c\u63e1\u9519\u8bef\u5904\u7406\u548c\u8c03\u8bd5\u6280\u672f\u3001\u638c\u63e1\u6587\u4ef6\u64cd\u4f5c\u3001\u638c\u63e1Python\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u3001\u5b66\u4e60\u6d4b\u8bd5\u548c\u6587\u6863\u64b0\u5199\u3001\u638c\u63e1\u7f51\u7edc\u7f16\u7a0b\u548c\u5e76\u53d1\u7f16\u7a0b\u3001\u5b66\u4e60\u9ad8\u7ea7Python\u6280\u5de7\u7b49\u3002\u901a\u8fc7\u7cfb\u7edf\u5730\u5b66\u4e60\u548c\u5b9e\u8df5\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u53ef\u9760\u548c\u4f18\u96c5\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u5b66\u4e60Python\u7f16\u7a0b\uff1f<\/strong><br \/>\u5b66\u4e60Python\u7f16\u7a0b\u7684\u7b2c\u4e00\u6b65\u662f\u9009\u62e9\u5408\u9002\u7684\u5b66\u4e60\u8d44\u6e90\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u8bfe\u7a0b\u3001\u4e66\u7c4d\u548c\u89c6\u9891\u6559\u7a0b\u6765\u5165\u95e8\u3002\u5efa\u8bae\u4ece\u57fa\u7840\u77e5\u8bc6\u5f00\u59cb\uff0c\u5305\u62ec\u6570\u636e\u7c7b\u578b\u3001\u63a7\u5236\u7ed3\u6784\u548c\u51fd\u6570\u3002\u5b9e\u8df5\u7f16\u7a0b\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c1d\u8bd5\u7f16\u5199\u5c0f\u9879\u76ee\u6216\u89e3\u51b3\u7f16\u7a0b\u6311\u6218\u6765\u5de9\u56fa\u6240\u5b66\u77e5\u8bc6\u3002<\/p>\n<p><strong>Python\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>Python\u4ee5\u5176\u7b80\u6d01\u6613\u8bfb\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u5e93\u652f\u6301\u800c\u95fb\u540d\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u4e13\u4e1a\u5f00\u53d1\u8005\u3002\u5b83\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u3001<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u3001\u7f51\u7edc\u5f00\u53d1\u7b49\u9886\u57df\u3002\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\uff0cPython\u7684\u793e\u533a\u6d3b\u8dc3\u7a0b\u5ea6\u9ad8\uff0c\u80fd\u591f\u83b7\u5f97\u5927\u91cf\u7684\u652f\u6301\u548c\u8d44\u6e90\u3002<\/p>\n<p><strong>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1f<\/strong><br \/>\u521d\u5b66\u8005\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\u5e38\u5e38\u4f1a\u9047\u5230\u8bed\u6cd5\u9519\u8bef\u3001\u7f29\u8fdb\u9519\u8bef\u6216\u53d8\u91cf\u672a\u5b9a\u4e49\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u548cIDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u6765\u5e2e\u52a9\u8bc6\u522b\u548c\u89e3\u51b3\u8fd9\u4e9b\u9519\u8bef\u3002\u6b64\u5916\uff0c\u67e5\u9605\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u8bba\u575b\u4e5f\u662f\u83b7\u53d6\u5e2e\u52a9\u7684\u597d\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f16\u5199Python\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u4e86\u89e3Python\u7684\u57fa\u672c\u8bed\u6cd5\u3001\u638c\u63e1Python\u7684\u6570\u636e\u7ed3\u6784\u3001\u719f\u6089\u51fd\u6570\u548c\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1169787,"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\/1169783"}],"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=1169783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169783\/revisions"}],"predecessor-version":[{"id":1169789,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169783\/revisions\/1169789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169787"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}