{"id":1099491,"date":"2025-01-08T15:31:11","date_gmt":"2025-01-08T07:31:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1099491.html"},"modified":"2025-01-08T15:31:14","modified_gmt":"2025-01-08T07:31:14","slug":"%e5%a6%82%e4%bd%95%e6%89%93%e4%b8%80%e4%ba%9b%e7%ae%80%e5%8d%95python%e4%bb%a3%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1099491.html","title":{"rendered":"\u5982\u4f55\u6253\u4e00\u4e9b\u7b80\u5355python\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063158\/363e7a79-689c-4e70-a810-e475409a8047.webp\" alt=\"\u5982\u4f55\u6253\u4e00\u4e9b\u7b80\u5355python\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u7f16\u5199\u7b80\u5355Python\u4ee3\u7801\u7684\u5173\u952e\u70b9\u5305\u62ec\uff1a\u7406\u89e3Python\u7684\u57fa\u7840\u8bed\u6cd5\u3001\u4f7f\u7528\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b\u3001\u4f7f\u7528\u63a7\u5236\u7ed3\u6784\uff08\u5982\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\uff09\u3001\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5e93\u3002<\/strong> \u8fd9\u4e9b\u662f\u7f16\u5199\u7b80\u5355Python\u4ee3\u7801\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\uff1a\u4f7f\u7528\u63a7\u5236\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u63a7\u5236\u7ed3\u6784\u662f\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002Python\u4e2d\u7684\u63a7\u5236\u7ed3\u6784\u5305\u62ec\u6761\u4ef6\u8bed\u53e5\uff08if-else\uff09\u548c\u5faa\u73af\u7ed3\u6784\uff08for, while\uff09\u3002\u901a\u8fc7\u4f7f\u7528\u63a7\u5236\u7ed3\u6784\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u548c\u6d41\u7a0b\u63a7\u5236\uff0c\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3Python\u7684\u57fa\u7840\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee5\u5176\u7b80\u6d01\u660e\u4e86\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u529f\u80fd\u8457\u79f0\u3002\u5728\u7f16\u5199Python\u4ee3\u7801\u4e4b\u524d\uff0c\u4e86\u89e3\u57fa\u7840\u8bed\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6ce8\u91ca\u662f\u7528\u6765\u89e3\u91ca\u4ee3\u7801\u7684\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5355\u884c\u6ce8\u91ca\u4ee5<code>#<\/code>\u5f00\u5934\uff0c\u591a\u884c\u6ce8\u91ca\u7528\u4e09\u4e2a\u5355\u5f15\u53f7\u6216\u4e09\u4e2a\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u662f\u5355\u884c\u6ce8\u91ca<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>\u8fd9\u662f\u591a\u884c\u6ce8\u91ca<\/p>\n<p>\u53ef\u4ee5\u5199\u591a\u884c\u89e3\u91ca<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u7f29\u8fdb<\/h4>\n<\/p>\n<p><p>Python\u4f7f\u7528\u7f29\u8fdb\u6765\u8868\u793a\u4ee3\u7801\u5757\u3002\u901a\u5e38\u4f7f\u7528\u56db\u4e2a\u7a7a\u683c\u6216\u4e00\u4e2aTab\u952e\u6765\u7f29\u8fdb\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if True:<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u53d8\u91cf\u662f\u7a0b\u5e8f\u4e2d\u7528\u6765\u5b58\u50a8\u6570\u636e\u7684\u5bb9\u5668\u3002\u5728Python\u4e2d\uff0c\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u8d4b\u503c\u65f6\u81ea\u52a8\u786e\u5b9a\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u53d8\u91cf\u7684\u8d4b\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>y = &quot;Hello&quot;<\/p>\n<p>z = 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u5b57\u7b26\u4e32\uff08str\uff09\u3001\u5217\u8868\uff08list\uff09\u3001\u5143\u7ec4\uff08tuple\uff09\u3001\u5b57\u5178\uff08dict\uff09\u3001\u96c6\u5408\uff08set\uff09\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 = &quot;Python&quot;<\/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 = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25}<\/p>\n<h2><strong>\u96c6\u5408<\/strong><\/h2>\n<p>g = {1, 2, 3, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u63a7\u5236\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u63a7\u5236\u7ed3\u6784\u662f\u7f16\u7a0b\u4e2d\u63a7\u5236\u7a0b\u5e8f\u6267\u884c\u6d41\u7a0b\u7684\u5173\u952e\u3002\u5728Python\u4e2d\uff0c\u5e38\u89c1\u7684\u63a7\u5236\u7ed3\u6784\u5305\u62ec\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u6761\u4ef6\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u7684\u771f\u5047\u6765\u51b3\u5b9a\u6267\u884c\u54ea\u4e2a\u4ee3\u7801\u5757\u3002Python\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u5305\u62ecif\u3001elif\u548celse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>if x &gt; 0:<\/p>\n<p>    print(&quot;x \u662f\u6b63\u6570&quot;)<\/p>\n<p>elif x == 0:<\/p>\n<p>    print(&quot;x \u662f\u96f6&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;x \u662f\u8d1f\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u5faa\u73af\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u6784\u7528\u4e8e\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u3002Python\u4e2d\u7684\u5faa\u73af\u7ed3\u6784\u5305\u62ecfor\u5faa\u73af\u548cwhile\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p><strong>3.2.1 for\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>for\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5217\u8868<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>for num in numbers:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3.2.2 while\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>while\u5faa\u73af\u5728\u7ed9\u5b9a\u6761\u4ef6\u4e3a\u771f\u65f6\u6267\u884c\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u53701\u52305<\/p>\n<p>i = 1<\/p>\n<p>while i &lt;= 5:<\/p>\n<p>    print(i)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u5143\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002Python\u4e2d\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return &quot;Hello, &quot; + name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">message = greet(&quot;Alice&quot;)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u7f16\u7a0b\u4efb\u52a1\u3002\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757\u548c\u5e93\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5bfc\u5165\u6807\u51c6\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u4f7f\u7528math\u5e93\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\u53ef\u4ee5\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982numpy\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u64cd\u4f5c\u662fPython\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u5199\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\uff0c\u4f7f\u7528<code>read<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002Python\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code>\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u6355\u83b7\u5f02\u5e38<\/h4>\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;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 finally\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>finally\u8bed\u53e5\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u7c7b\u548c\u5bf9\u8c61\u662f\u5176\u6838\u5fc3\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u5b9a\u4e49\u7c7b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>class<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\u3002<\/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>        return &quot;Hello, &quot; + self.name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u521b\u5efa\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u521b\u5efa\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">p = Person(&quot;Alice&quot;, 25)<\/p>\n<p>print(p.greet())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u62101\u523010\u7684\u5e73\u65b9\u6570\u5217\u8868<\/p>\n<p>squares = [x  2 for x in range(1, 11)]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u751f\u6210\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        yield i<\/p>\n<p>for num in generate_numbers(5):<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u884c\u4e3a\u7684\u9ad8\u7ea7\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello!&quot;)<\/p>\n<p>say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5e76\u53d1\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>12.1 \u4f7f\u7528\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=print_numbers)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>12.2 \u4f7f\u7528\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b<\/strong><\/h2>\n<p>process = multiprocessing.Process(target=print_numbers)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u7f51\u7edc\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\uff0c\u5982socket\u3001requests\u7b49\u3002<\/p>\n<\/p>\n<p><h4>13.1 \u4f7f\u7528socket\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efasocket\u5bf9\u8c61<\/strong><\/h2>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u670d\u52a1\u5668<\/strong><\/h2>\n<p>s.connect((&quot;www.example.com&quot;, 80))<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>s.sendall(b&quot;GET \/ HTTP\/1.1\\r\\nHost: www.example.com\\r\\n\\r\\n&quot;)<\/p>\n<h2><strong>\u63a5\u6536\u54cd\u5e94<\/strong><\/h2>\n<p>response = s.recv(1024)<\/p>\n<p>print(response)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>s.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>13.2 \u4f7f\u7528requests\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/api.example.com\/data&quot;)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/h3>\n<\/p>\n<p><p>Python\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5982pandas\u548cnumpy\u3002<\/p>\n<\/p>\n<p><h4>14.1 \u4f7f\u7528pandas<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&quot;name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;], &quot;age&quot;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u663e\u793a\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<h2><strong>\u6570\u636e\u5206\u6790<\/strong><\/h2>\n<p>print(df.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>14.2 \u4f7f\u7528numpy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(arr)<\/p>\n<h2><strong>\u6570\u7ec4\u8fd0\u7b97<\/strong><\/h2>\n<p>print(arr * 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e30\u5bcc\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u5982matplotlib\u548cseaborn\u3002<\/p>\n<\/p>\n<p><h4>15.1 \u4f7f\u7528matplotlib<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>15.2 \u4f7f\u7528seaborn<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>tips = sns.load_dataset(&quot;tips&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>sns.scatterplot(x=&quot;total_bill&quot;, y=&quot;tip&quot;, data=tips)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e0e<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a><\/h3>\n<\/p>\n<p><p>Python\u662f\u673a\u5668\u5b66\u4e60\u4e0e\u4eba\u5de5\u667a\u80fd\u9886\u57df\u7684\u91cd\u8981\u5de5\u5177\uff0c\u6709\u4e30\u5bcc\u7684\u5e93\u5982scikit-learn\u3001tensorflow\u548ckeras\u3002<\/p>\n<\/p>\n<p><h4>16.1 \u4f7f\u7528scikit-learn<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>data = load_iris()<\/p>\n<p>X, y = data.data, data.target<\/p>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>clf = RandomForestClassifier()<\/p>\n<p>clf.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = clf.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred)<\/p>\n<p>print(f&quot;Accuracy: {accuracy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>16.2 \u4f7f\u7528tensorflow<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.models import Sequential<\/p>\n<p>from tensorflow.keras.layers import Dense<\/p>\n<h2><strong>\u521b\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential([<\/p>\n<p>    Dense(128, activation=&#39;relu&#39;, input_shape=(4,)),<\/p>\n<p>    Dense(64, activation=&#39;relu&#39;),<\/p>\n<p>    Dense(3, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X_train, y_train, epochs=10, batch_size=32)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>loss, accuracy = model.evaluate(X_test, y_test)<\/p>\n<p>print(f&quot;Accuracy: {accuracy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u4e2a\u65b9\u9762\u7684\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u638c\u63e1\u7f16\u5199\u7b80\u5355Python\u4ee3\u7801\u7684\u6280\u5de7\u548c\u65b9\u6cd5\u3002\u8fd9\u4e9b\u77e5\u8bc6\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u4f60\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u8fd8\u80fd\u4e3a\u8fdb\u4e00\u6b65\u5b66\u4e60\u548c\u5e94\u7528Python\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\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\u6700\u4f73\u65b9\u6cd5\u662f\u4ece\u57fa\u7840\u77e5\u8bc6\u5165\u624b\u3002\u53ef\u4ee5\u9009\u62e9\u5728\u7ebf\u8bfe\u7a0b\u3001\u4e66\u7c4d\u6216\u89c6\u9891\u6559\u7a0b\uff0c\u6db5\u76d6\u53d8\u91cf\u3001\u6570\u636e\u7c7b\u578b\u3001\u63a7\u5236\u7ed3\u6784\uff08\u5982\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\uff09\u3001\u51fd\u6570\u7b49\u57fa\u672c\u6982\u5ff5\u3002\u5b9e\u8df5\u662f\u5173\u952e\uff0c\u5c1d\u8bd5\u7f16\u5199\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u5982\u8ba1\u7b97\u5668\u6216\u5c0f\u6e38\u620f\uff0c\u4ee5\u5de9\u56fa\u6240\u5b66\u77e5\u8bc6\u3002\u6b64\u5916\uff0c\u53c2\u4e0e\u7f16\u7a0b\u793e\u533a\u548c\u8bba\u575b\u53ef\u4ee5\u83b7\u5f97\u5b9d\u8d35\u7684\u5efa\u8bae\u548c\u652f\u6301\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684Python\u7f16\u7a0b\u9519\u8bef\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u521d\u5b66\u8005\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u7f29\u8fdb\u9519\u8bef\u548c\u7c7b\u578b\u9519\u8bef\u3002\u8bed\u6cd5\u9519\u8bef\u901a\u5e38\u51fa\u73b0\u5728\u62fc\u5199\u6216\u6807\u70b9\u7b26\u53f7\u4e0a\uff0c\u89e3\u51b3\u65b9\u6cd5\u662f\u4ed4\u7ec6\u68c0\u67e5\u4ee3\u7801\u3002\u7f29\u8fdb\u9519\u8bef\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u5757\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\uff0c\u786e\u4fdd\u6bcf\u4e2a\u4ee3\u7801\u5757\u7684\u7f29\u8fdb\u4e00\u81f4\u5373\u53ef\u3002\u7c7b\u578b\u9519\u8bef\u5219\u53d1\u751f\u5728\u5c1d\u8bd5\u5bf9\u4e0d\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u5e76\u8fdb\u884c\u9002\u5f53\u8f6c\u6362\u80fd\u591f\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u8bd5\u4ee3\u7801\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u4f7f\u7528\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177\u5982<code>pdb<\/code>\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u89c2\u5bdf\u53d8\u91cf\u72b6\u6001\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6253\u5370\u8bed\u53e5\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u8c03\u8bd5\u65b9\u6cd5\uff0c\u901a\u8fc7\u5728\u5173\u952e\u4f4d\u7f6e\u8f93\u51fa\u53d8\u91cf\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u3002IDE\u548c\u6587\u672c\u7f16\u8f91\u5668\u901a\u5e38\u63d0\u4f9b\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u548c\u76d1\u63a7\u53d8\u91cf\uff0c\u4f7f\u5f97\u8c03\u8bd5\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f16\u5199\u7b80\u5355Python\u4ee3\u7801\u7684\u5173\u952e\u70b9\u5305\u62ec\uff1a\u7406\u89e3Python\u7684\u57fa\u7840\u8bed\u6cd5\u3001\u4f7f\u7528\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b\u3001\u4f7f\u7528\u63a7\u5236\u7ed3\u6784\uff08\u5982\u6761\u4ef6\u8bed\u53e5 [&hellip;]","protected":false},"author":3,"featured_media":1099496,"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\/1099491"}],"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=1099491"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099491\/revisions"}],"predecessor-version":[{"id":1099497,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099491\/revisions\/1099497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1099496"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1099491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1099491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1099491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}