{"id":1150542,"date":"2025-01-13T17:02:41","date_gmt":"2025-01-13T09:02:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150542.html"},"modified":"2025-01-13T17:02:43","modified_gmt":"2025-01-13T09:02:43","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150542.html","title":{"rendered":"\u7528python\u5982\u4f55\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181007\/3ac09a93-93a8-403a-aefc-ad744442bc3b.webp\" alt=\"\u7528python\u5982\u4f55\u5199\" \/><\/p>\n<p><p> \u7528Python\u7f16\u5199\u7a0b\u5e8f\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<strong>\u4f7f\u7528Python\u7f16\u5199\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5Python\u73af\u5883\u3001\u7f16\u5199\u4ee3\u7801\u3001\u8c03\u8bd5\u4ee3\u7801\u3001\u8fd0\u884c\u4ee3\u7801<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u4e2a\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u548c\u6280\u5de7\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7528Python\u7f16\u5199\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Python\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5Python\u73af\u5883\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0b\u8f7dPython\u5b89\u88c5\u5305<\/strong>\uff1a\u8bbf\u95eePython\u5b98\u7f51\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%EF%BC%8C%E4%B8%8B%E8%BD%BD%E9%80%82%E5%90%88%E4%BD%A0%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84Python%E5%AE%89%E8%A3%85%E5%8C%85%E3%80%82\">https:\/\/www.python.org\/\uff09\uff0c\u4e0b\u8f7d\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684Python\u5b89\u88c5\u5305\u3002<\/a><\/li>\n<li><strong>\u5b89\u88c5Python<\/strong>\uff1a\u8fd0\u884c\u4e0b\u8f7d\u7684\u5b89\u88c5\u5305\uff0c\u6839\u636e\u63d0\u793a\u5b8c\u6210Python\u7684\u5b89\u88c5\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u9009\u62e9\u6dfb\u52a0Python\u5230\u7cfb\u7edf\u8def\u5f84\uff08PATH\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528<code>python<\/code>\u547d\u4ee4\u3002<\/li>\n<li><strong>\u9a8c\u8bc1\u5b89\u88c5<\/strong>\uff1a\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u5982\u679c\u663e\u793aPython\u7684\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u597dPython\u73af\u5883\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e86\u3002\u7f16\u5199\u4ee3\u7801\u7684\u5de5\u5177\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u9009\u62e9\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Notepad++\u3001Sublime Text\uff09\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff0c\u5982PyCharm\u3001Visual Studio Code\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u7a0b\u5e8f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># hello.py<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u4e0a\u8ff0\u4ee3\u7801\u4e3a<code>hello.py<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u4ee3\u7801\u540e\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\u6216\u9700\u8981\u4f18\u5316\u7684\u5730\u65b9\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u8fdb\u884c\u8c03\u8bd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u8c03\u8bd5\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5370\u8c03\u8bd5<\/strong>\uff1a\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6253\u5370\u53d8\u91cf\u7684\u503c\u6216\u72b6\u6001\uff0c\u67e5\u770b\u7a0b\u5e8f\u7684\u6267\u884c\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u65ad\u70b9<\/strong>\uff1a\u5728IDE\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/li>\n<li><strong>\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/strong>\uff1aPython\u81ea\u5e26\u7684<code>pdb<\/code>\u8c03\u8bd5\u5668\u6216\u5176\u4ed6\u8c03\u8bd5\u5de5\u5177\uff0c\u5982<code>ipdb<\/code>\u3001<code>pyringe<\/code>\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u8c03\u8bd5\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u8fd0\u884c\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u8fd0\u884c\u4ee3\u7801\u4e86\u3002\u4ee5\u4e0b\u662f\u8fd0\u884cPython\u4ee3\u7801\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u547d\u4ee4\u884c\u8fd0\u884c<\/strong>\uff1a\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>python hello.py<\/code>\u6216<code>python3 hello.py<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u8f93\u51fa<code>Hello, World!<\/code>\u3002<\/li>\n<li><strong>\u5728IDE\u4e2d\u8fd0\u884c<\/strong>\uff1a\u5728IDE\u4e2d\u6253\u5f00<code>hello.py<\/code>\u6587\u4ef6\uff0c\u70b9\u51fb\u8fd0\u884c\u6309\u94ae\u6216\u4f7f\u7528\u5feb\u6377\u952e\u8fd0\u884c\u4ee3\u7801\u3002<\/li>\n<li><strong>\u811a\u672c\u8fd0\u884c<\/strong>\uff1a\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u591a\u4e2aPython\u6587\u4ef6\u7684\u811a\u672c\uff0c\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5176\u4ed6\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c\u4e3b\u811a\u672c\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001Python\u7f16\u7a0b\u7684\u6838\u5fc3\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u638c\u63e1\u4e00\u4e9b\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u6838\u5fc3\u6982\u5ff5\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\u4f7f\u7528\u3002\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u5b57\u7b26\u4e32\uff08str\uff09\u3001\u5e03\u5c14\u503c\uff08bool\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10          # \u6574\u6570<\/p>\n<p>b = 3.14        # \u6d6e\u70b9\u6570<\/p>\n<p>c = &quot;Hello&quot;     # \u5b57\u7b26\u4e32<\/p>\n<p>d = True        # \u5e03\u5c14\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u63a7\u5236\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u63a7\u5236\u7ed3\u6784\uff0c\u5305\u62ec\u6761\u4ef6\u8bed\u53e5\uff08if-elif-else\uff09\u3001\u5faa\u73af\u8bed\u53e5\uff08for\u3001while\uff09\u7b49\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(&quot;a is greater than b&quot;)<\/p>\n<p>elif a == b:<\/p>\n<p>    print(&quot;a is equal to b&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;a is less than b&quot;)<\/p>\n<h2><strong>\u5faa\u73af\u8bed\u53e5<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p>while a &gt; 0:<\/p>\n<p>    print(a)<\/p>\n<p>    a -= 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u51fd\u6570\u548c\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u5143\uff0c\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u3002\u6a21\u5757\u662f\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u51fd\u6570<\/p>\n<p>def add(x, y):<\/p>\n<p>    return x + y<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = add(5, 3)<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p>import math<\/p>\n<p>print(math.sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>Python\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u3002\u7c7b\u662f\u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u5bf9\u8c61\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7c7b<\/p>\n<p>class Dog:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def bark(self):<\/p>\n<p>        print(f&quot;{self.name} is barking.&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>my_dog = Dog(&quot;Buddy&quot;, 3)<\/p>\n<p>my_dog.bark()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001Python\u9ad8\u7ea7\u7f16\u7a0b\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u638c\u63e1\u4e86\u57fa\u672c\u6982\u5ff5\u540e\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b66\u4e60\u4e00\u4e9b\u9ad8\u7ea7\u7f16\u7a0b\u6280\u5de7\uff0c\u4ee5\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e00\u884c\u4ee3\u7801\u4ee3\u66ff\u5faa\u73af\u521b\u5efa\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u666e\u901a\u65b9\u6cd5\u521b\u5efa\u5217\u8868<\/p>\n<p>squares = []<\/p>\n<p>for x in range(10):<\/p>\n<p>    squares.append(x2)<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u5217\u8868<\/strong><\/h2>\n<p>squares = [x2 for x in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u521b\u5efa\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u503c\u3002\u8fed\u4ee3\u5668\u662f\u5b9e\u73b0\u4e86<code>__iter__()<\/code>\u548c<code>__next__()<\/code>\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u751f\u6210\u5668<\/p>\n<p>def my_generator():<\/p>\n<p>    yield 1<\/p>\n<p>    yield 2<\/p>\n<p>    yield 3<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/h2>\n<p>for value in my_generator():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u521b\u5efa\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>class MyIterator:<\/p>\n<p>    def __init__(self, start, end):<\/p>\n<p>        self.current = start<\/p>\n<p>        self.end = end<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.current &gt;= self.end:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p>        self.current += 1<\/p>\n<p>        return self.current - 1<\/p>\n<h2><strong>\u4f7f\u7528\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>my_iterator = MyIterator(0, 5)<\/p>\n<p>for value in my_iterator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u529f\u80fd\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():<\/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<h2><strong>\u4f7f\u7528\u88c5\u9970\u5668<\/strong><\/h2>\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>\u4e03\u3001\u5e38\u7528Python\u5e93\u548c\u5de5\u5177<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u7684\u5e93\u548c\u5de5\u5177\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001NumPy<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u5404\u79cd\u6570\u5b66\u51fd\u6570\u3002<\/p>\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>a = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(a)<\/p>\n<h2><strong>\u6570\u7ec4\u8fd0\u7b97<\/strong><\/h2>\n<p>b = a * 2<\/p>\n<p>print(b)<\/p>\n<h2><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/h2>\n<p>c = np.array([[1, 2], [3, 4]])<\/p>\n<p>d = np.array([[5, 6], [7, 8]])<\/p>\n<p>print(np.dot(c, d))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Pandas<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u6570\u636e\u64cd\u4f5c<\/strong><\/h2>\n<p>print(df[&#39;Age&#39;].mean())<\/p>\n<p>df[&#39;Age&#39;] = df[&#39;Age&#39;] + 1<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u56fe\u8868\u3002<\/p>\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 = [2, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Line Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001Scikit-learn<\/h4>\n<\/p>\n<p><p>Scikit-learn\u662f\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5404\u79cd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn import datasets, model_selection, svm<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>iris = datasets.load_iris()<\/p>\n<p>X = iris.data<\/p>\n<p>y = iris.target<\/p>\n<h2><strong>\u5212\u5206\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3)<\/p>\n<h2><strong>\u521b\u5efa\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = svm.SVC()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6a21\u578b<\/strong><\/h2>\n<p>accuracy = model.score(X_test, y_test)<\/p>\n<p>print(f&quot;Accuracy: {accuracy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001Python\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9075\u5faaPEP 8\u7f16\u7801\u89c4\u8303<\/h4>\n<\/p>\n<p><p>PEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u89c4\u5b9a\u4e86\u4ee3\u7801\u683c\u5f0f\u3001\u547d\u540d\u7ea6\u5b9a\u7b49\u3002\u9075\u5faaPEP 8\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6574\u6d01\u548c\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># PEP 8\u793a\u4f8b<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199\u6587\u6863\u548c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863\u548c\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u4ed6\u4eba\u7406\u89e3\u4f60\u7684\u4ee3\u7801\u3002\u4f7f\u7528<code>docstring<\/code>\u4e3a\u51fd\u6570\u3001\u7c7b\u548c\u6a21\u5757\u7f16\u5199\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>    Return the sum of a and b.<\/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 a and b<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\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<p>        self.assertEqual(add(0, 0), 0)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001Python<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/23316.html\" target=\"_blank\">\u9879\u76ee\u7ba1\u7406<\/a><\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ba1\u7406\u591a\u4e2aPython\u6587\u4ef6\u548c\u4f9d\u8d56\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9879\u76ee\u7ba1\u7406\u5de5\u5177\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5e93\uff0c\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u51b2\u7a81\u3002\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u521b\u5efa\u865a\u62df\u73af\u5883<\/p>\n<p>python -m venv myenv<\/p>\n<h2><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<h2><strong>Windows<\/strong><\/h2>\n<p>myenv\\Scripts\\activate<\/p>\n<h2><strong>macOS\/Linux<\/strong><\/h2>\n<p>source myenv\/bin\/activate<\/p>\n<h2><strong>\u5b89\u88c5\u4f9d\u8d56\u5e93<\/strong><\/h2>\n<p>pip install numpy<\/p>\n<h2><strong>\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p>deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5305\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5e93\uff0c\u5e76\u4f7f\u7528<code>requirements.txt<\/code>\u8bb0\u5f55\u4f9d\u8d56\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5\u4f9d\u8d56\u5e93<\/p>\n<p>pip install numpy pandas<\/p>\n<h2><strong>\u751f\u6210requirements.txt<\/strong><\/h2>\n<p>pip freeze &gt; requirements.txt<\/p>\n<h2><strong>\u4ecerequirements.txt\u5b89\u88c5\u4f9d\u8d56\u5e93<\/strong><\/h2>\n<p>pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ee3\u7801\u7248\u672c\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Git\u8fdb\u884c\u4ee3\u7801\u7248\u672c\u63a7\u5236\uff0c\u53ef\u4ee5\u8ddf\u8e2a\u4ee3\u7801\u7684\u53d8\u5316\uff0c\u5e76\u4e0e\u56e2\u961f\u6210\u5458\u534f\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u521d\u59cb\u5316Git\u4ed3\u5e93<\/p>\n<p>git init<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u4ef6<\/strong><\/h2>\n<p>git add .<\/p>\n<h2><strong>\u63d0\u4ea4\u66f4\u6539<\/strong><\/h2>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<h2><strong>\u8fdc\u7a0b\u4ed3\u5e93<\/strong><\/h2>\n<p>git remote add origin https:\/\/github.com\/username\/repository.git<\/p>\n<p>git push -u origin master<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001Python\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Python\u5728\u5404\u4e2a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001Web\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Django\u6216Flask\u6846\u67b6\u8fdb\u884cWeb\u5f00\u53d1\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Flask\u793a\u4f8b<\/p>\n<p>from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u7ed3\u5408Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u6570\u636e\u5206\u6790<\/strong><\/h2>\n<p>print(data.describe())<\/p>\n<h2><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>data[&#39;column&#39;].plot(kind=&#39;hist&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u673a\u5668\u5b66\u4e60<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Scikit-learn\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn import datasets, model_selection, svm<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>iris = datasets.load_iris()<\/p>\n<p>X = iris.data<\/p>\n<p>y = iris.target<\/p>\n<h2><strong>\u5212\u5206\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3)<\/p>\n<h2><strong>\u521b\u5efa\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = svm.SVC()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6a21\u578b<\/strong><\/h2>\n<p>accuracy = model.score(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\u5185\u5bb9\uff0c\u4f60\u5e94\u8be5\u5bf9\u7528Python\u7f16\u5199\u7a0b\u5e8f\u6709\u4e86\u4e00\u4e2a\u5168\u9762\u7684\u4e86\u89e3\u3002\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u7f16\u7a0b\uff0c\u5e76\u5e94\u7528\u5230\u5b9e\u9645\u9879\u76ee\u4e2d\u3002Python\u662f\u4e00\u95e8\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u5b66\u4e60\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u638c\u63e1\u5b83\u5c06\u4e3a\u4f60\u7684\u7f16\u7a0b\u4e4b\u8def\u5e26\u6765\u8bb8\u591a\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u5b66\u4e60\u7528Python\u7f16\u7a0b\uff1f<\/strong><br \/>\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5b66\u4e60Python\u53ef\u4ee5\u4ece\u5b89\u88c5Python\u89e3\u91ca\u5668\u5f00\u59cb\u3002\u63a5\u4e0b\u6765\uff0c\u5efa\u8bae\u9009\u62e9\u4e00\u672c\u597d\u7684\u6559\u6750\u6216\u8005\u5728\u7ebf\u8bfe\u7a0b\uff0c\u7cfb\u7edf\u5730\u5b66\u4e60Python\u7684\u57fa\u672c\u8bed\u6cd5\u3001\u6570\u636e\u7ed3\u6784\u548c\u5e38\u7528\u5e93\u3002\u6b64\u5916\uff0c\u5b9e\u8df5\u662f\u5b66\u4e60\u7f16\u7a0b\u7684\u5173\u952e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u5c0f\u9879\u76ee\u6216\u53c2\u4e0e\u5f00\u6e90\u9879\u76ee\u6765\u589e\u5f3a\u81ea\u5df1\u7684\u6280\u80fd\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u548c\u6846\u67b6\uff1f<\/strong><br \/>Python\u62e5\u6709\u4e30\u5bcc\u7684\u5e93\u548c\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\u3002\u5bf9\u4e8e\u6570\u636e\u5206\u6790\uff0cPandas\u548cNumPy\u662f\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\uff1b\u5728\u7f51\u9875\u5f00\u53d1\u65b9\u9762\uff0cFlask\u548cDjango\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff1b\u673a\u5668\u5b66\u4e60\u9886\u57df\uff0cTensorFlow\u548cScikit-learn\u662f\u5e38\u7528\u7684\u5e93\u3002\u8fd9\u4e9b\u5de5\u5177\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u662f\u7f16\u7a0b\u4e2d\u7684\u91cd\u8981\u90e8\u5206\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u7ee7\u7eed\u8fd0\u884c\u800c\u4e0d\u662f\u76f4\u63a5\u5d29\u6e83\u3002\u6b64\u5916\uff0c\u4f7f\u7528finally\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u67d0\u4e9b\u4ee3\u7801\u5728\u5f02\u5e38\u53d1\u751f\u540e\u4ecd\u7136\u88ab\u6267\u884c\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7f16\u5199\u7a0b\u5e8f\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u4f7f\u7528Python\u7f16\u5199\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4 [&hellip;]","protected":false},"author":3,"featured_media":1150547,"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\/1150542"}],"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=1150542"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150542\/revisions"}],"predecessor-version":[{"id":1150551,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150542\/revisions\/1150551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}