{"id":1151048,"date":"2025-01-13T17:08:52","date_gmt":"2025-01-13T09:08:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151048.html"},"modified":"2025-01-13T17:08:54","modified_gmt":"2025-01-13T09:08:54","slug":"python3%e5%a6%82%e4%bd%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151048.html","title":{"rendered":"python3\u5982\u4f55\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181406\/ceadc6cf-10c7-4745-ae71-7da24877bd54.webp\" alt=\"python3\u5982\u4f55\u7528\" \/><\/p>\n<p><p> Python 3\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u6613\u4e8e\u5b66\u4e60\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u9886\u57df\u3002<strong>\u5b66\u4e60Python 3\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u3001\u4e86\u89e3\u57fa\u7840\u8bed\u6cd5\u3001\u638c\u63e1\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u548c\u63a7\u5236\u7ed3\u6784\u3001\u4e86\u89e3\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u5b66\u4e60\u4f7f\u7528Python\u6807\u51c6\u5e93\u3001\u5b9e\u8df5\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801<\/strong>\u3002\u5176\u4e2d\uff0c\u4e86\u89e3\u57fa\u7840\u8bed\u6cd5\u662f\u6700\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u662f\u638c\u63e1\u5176\u4ed6\u77e5\u8bc6\u7684\u524d\u63d0\u3002<\/p>\n<\/p>\n<p><p>Python 3\u7684\u57fa\u7840\u8bed\u6cd5\u5305\u62ec\u53d8\u91cf\u7684\u5b9a\u4e49\u3001\u6570\u636e\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u8bed\u53e5\u548c\u51fd\u6570\u5b9a\u4e49\u7b49\u3002\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u8bed\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u548c\u7f16\u5199\u66f4\u590d\u6742\u7684Python\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u662fPython\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u53d8\u91cf\u53ef\u4ee5\u5b58\u50a8\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\uff0c\u901a\u8fc7\u53d8\u91cf\u6211\u4eec\u53ef\u4ee5\u64cd\u4f5c\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u4ece\u5b89\u88c5Python 3\u3001\u57fa\u7840\u8bed\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u3001\u63a7\u5236\u7ed3\u6784\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u6807\u51c6\u5e93\u548c\u5b9e\u8df5\u7f16\u5199\u4ee3\u7801\u7b49\u65b9\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python 3\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5Python 3<\/strong><\/h2>\n<p><h2>1. \u4e0b\u8f7d\u548c\u5b89\u88c5Python 3<\/h2>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python 3\uff0c\u9996\u5148\u9700\u8981\u5728\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u5b83\u3002\u4f60\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/downloads\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E9%80%82%E7%94%A8%E4%BA%8E%E4%BD%A0%E7%9A%84%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%89%E8%A3%85%E5%8C%85%E3%80%82%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B%E9%9D%9E%E5%B8%B8%E7%AE%80%E5%8D%95%EF%BC%8C%E5%8F%AA%E9%9C%80%E6%8C%89%E7%85%A7%E6%8F%90%E7%A4%BA%E4%B8%80%E6%AD%A5%E6%AD%A5%E6%93%8D%E4%BD%9C%E5%8D%B3%E5%8F%AF%E3%80%82\">https:\/\/www.python.org\/downloads\/\uff09\u4e0b\u8f7d\u9002\u7528\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u8fc7\u7a0b\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6309\u7167\u63d0\u793a\u4e00\u6b65\u6b65\u64cd\u4f5c\u5373\u53ef\u3002<\/a><\/p>\n<\/p>\n<p><h2>2. \u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u9700\u8981\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u4ee5\u4fbf\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\u627e\u5230\u5e76\u7f16\u8f91\u201cPath\u201d\u53d8\u91cf\uff0c\u6dfb\u52a0Python\u5b89\u88c5\u76ee\u5f55\u7684\u8def\u5f84\uff08\u4f8b\u5982\uff1aC:\\Python39\uff09\u3002<\/li>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python --version<\/code>\uff0c\u5982\u679c\u663e\u793a\u51faPython\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u914d\u7f6e\u6210\u529f\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e8c\u3001\u57fa\u7840\u8bed\u6cd5<\/strong><\/h2>\n<p><h2>1. \u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/h2>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u7c7b\u578b\u3002\u4f60\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u5c06\u503c\u8d4b\u7ed9\u53d8\u91cf\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u6574\u6570<\/p>\n<p>y = 3.14  # \u6d6e\u70b9\u6570<\/p>\n<p>name = &quot;Alice&quot;  # \u5b57\u7b26\u4e32<\/p>\n<p>is_student = True  # \u5e03\u5c14\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u8fd0\u7b97\u7b26<\/h2>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b97\u672f\u8fd0\u7b97\u7b26<\/p>\n<p>a = 5 + 3  # \u52a0\u6cd5<\/p>\n<p>b = 5 - 3  # \u51cf\u6cd5<\/p>\n<p>c = 5 * 3  # \u4e58\u6cd5<\/p>\n<p>d = 5 \/ 3  # \u9664\u6cd5<\/p>\n<p>e = 5 % 3  # \u53d6\u4f59<\/p>\n<p>f = 5  3  # \u5e42\u8fd0\u7b97<\/p>\n<h2><strong>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>g = (5 == 3)  # \u7b49\u4e8e<\/p>\n<p>h = (5 != 3)  # \u4e0d\u7b49\u4e8e<\/p>\n<p>i = (5 &gt; 3)  # \u5927\u4e8e<\/p>\n<p>j = (5 &lt; 3)  # \u5c0f\u4e8e<\/p>\n<p>k = (5 &gt;= 3)  # \u5927\u4e8e\u7b49\u4e8e<\/p>\n<p>l = (5 &lt;= 3)  # \u5c0f\u4e8e\u7b49\u4e8e<\/p>\n<h2><strong>\u903b\u8f91\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>m = (5 &gt; 3 and 3 &lt; 4)  # \u4e0e<\/p>\n<p>n = (5 &gt; 3 or 3 &gt; 4)  # \u6216<\/p>\n<p>o = not (5 &gt; 3)  # \u975e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u6761\u4ef6\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>Python\u4f7f\u7528<code>if<\/code>\u3001<code>elif<\/code>\u548c<code>else<\/code>\u6765\u5b9e\u73b0\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff1a<\/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><h2>4. \u5faa\u73af\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>Python\u652f\u6301<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># for\u5faa\u73af<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<h2><strong>while\u5faa\u73af<\/strong><\/h2>\n<p>i = 0<\/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><h2>5. \u51fd\u6570\u5b9a\u4e49<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(&quot;Hello, &quot; + name + &quot;!&quot;)<\/p>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p><h2>1. \u6570\u5b57\u7c7b\u578b<\/h2>\n<\/p>\n<p><p>Python\u4e2d\u7684\u6570\u5b57\u7c7b\u578b\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u548c\u590d\u6570\uff08complex\uff09\u3002\u4f8b\u5982\uff1a<\/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 = 1 + 2j  # \u590d\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u7531\u4e00\u7cfb\u5217\u5b57\u7b26\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u5b9a\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s1 = &quot;Hello, World!&quot;<\/p>\n<p>s2 = &#39;Python is great!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u62fc\u63a5\u3001\u5207\u7247\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello, &quot; + &quot;World!&quot;  # \u62fc\u63a5<\/p>\n<p>print(s[0:5])  # \u5207\u7247\uff0c\u8f93\u51fa&quot;Hello&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u5217\u8868<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u662f\u6709\u5e8f\u7684\u53ef\u53d8\u5e8f\u5217\uff0c\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, &quot;Python&quot;, [4, 5]]<\/p>\n<p>print(lst[3])  # \u8f93\u51fa&quot;Python&quot;<\/p>\n<p>lst.append(6)  # \u6dfb\u52a0\u5143\u7d20<\/p>\n<p>print(lst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4. \u5143\u7ec4<\/h2>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u6709\u5e8f\u7684\u4e0d\u53ef\u53d8\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tup = (1, 2, 3, &quot;Python&quot;)<\/p>\n<p>print(tup[2])  # \u8f93\u51fa3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5. \u96c6\u5408<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u65e0\u5e8f\u7684\u3001\u4e0d\u53ef\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = {1, 2, 3, 4, 4}<\/p>\n<p>print(s)  # \u8f93\u51fa{1, 2, 3, 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6. \u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7684\u65e0\u5e8f\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>print(d[&quot;name&quot;])  # \u8f93\u51fa&quot;Alice&quot;<\/p>\n<p>d[&quot;age&quot;] = 26  # \u4fee\u6539\u503c<\/p>\n<p>print(d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u63a7\u5236\u7ed3\u6784<\/strong><\/h2>\n<p><h2>1. \u6761\u4ef6\u5224\u65ad<\/h2>\n<\/p>\n<p><p>\u6761\u4ef6\u5224\u65ad\u662f\u7f16\u7a0b\u4e2d\u5e38\u7528\u7684\u63a7\u5236\u7ed3\u6784\uff0c\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff1a<\/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><h2>2. \u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u5faa\u73af\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># for\u5faa\u73af<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<h2><strong>while\u5faa\u73af<\/strong><\/h2>\n<p>i = 0<\/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<h2><strong>\u4e94\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/strong><\/h2>\n<p><h2>1. \u7c7b\u548c\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7c7b\u548c\u5bf9\u8c61\u662f\u5176\u6838\u5fc3\u6982\u5ff5\u3002\u4f8b\u5982\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(&quot;Hello, my name is &quot; + self.name)<\/p>\n<p>p = Person(&quot;Alice&quot;, 25)<\/p>\n<p>p.greet()  # \u8f93\u51fa&quot;Hello, my name is Alice&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u7ee7\u627f<\/h2>\n<\/p>\n<p><p>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u91cd\u8981\u7279\u6027\uff0c\u7528\u4e8e\u5b9e\u73b0\u4ee3\u7801\u91cd\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Student(Person):<\/p>\n<p>    def __init__(self, name, age, student_id):<\/p>\n<p>        super().__init__(name, age)<\/p>\n<p>        self.student_id = student_id<\/p>\n<p>    def study(self):<\/p>\n<p>        print(self.name + &quot; is studying&quot;)<\/p>\n<p>s = Student(&quot;Bob&quot;, 20, &quot;S12345&quot;)<\/p>\n<p>s.greet()  # \u8f93\u51fa&quot;Hello, my name is Bob&quot;<\/p>\n<p>s.study()  # \u8f93\u51fa&quot;Bob is studying&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001Python\u6807\u51c6\u5e93<\/strong><\/h2>\n<p><h2>1. \u5e38\u7528\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u5305\u542b\u4e30\u5bcc\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa4.0<\/p>\n<p>import random<\/p>\n<p>print(random.randint(1, 10))  # \u8f93\u51fa1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p>import datetime<\/p>\n<p>print(datetime.datetime.now())  # \u8f93\u51fa\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u6587\u4ef6\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982\u8bfb\u5199\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6587\u4ef6<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)  # \u8f93\u51fa&quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5b9e\u8df5\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801<\/strong><\/h2>\n<p><h2>1. \u4f7f\u7528IDLE<\/h2>\n<\/p>\n<p><p>IDLE\u662fPython\u81ea\u5e26\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>idle<\/code>\u542f\u52a8IDLE\uff0c\u7136\u540e\u5728\u7f16\u8f91\u5668\u4e2d\u7f16\u5199\u4ee3\u7801\u5e76\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u5176\u4ed6IDE<\/h2>\n<\/p>\n<p><p>\u9664\u4e86IDLE\uff0c\u8fd8\u6709\u8bb8\u591a\u4f18\u79c0\u7684Python\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff0c\u4f8b\u5982PyCharm\u3001VS Code\u3001Jupyter Notebook\u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684IDE\u3002<\/p>\n<\/p>\n<p><h2>3. \u7f16\u5199\u548c\u8fd0\u884c\u811a\u672c<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u7f16\u5199Python\u811a\u672c\uff0c\u5e76\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>python<\/code>\u547d\u4ee4\u8fd0\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u540d\u4e3ahello.py\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u811a\u672c<\/strong><\/h2>\n<h2><strong>Windows<\/strong><\/h2>\n<p>python hello.py<\/p>\n<h2><strong>macOS\/Linux<\/strong><\/h2>\n<p>python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u9010\u6b65\u638c\u63e1Python 3\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u5e94\u7528\u4e8e\u5b9e\u9645\u5f00\u53d1\u4e2d\u3002\u5b66\u4e60\u7f16\u7a0b\u9700\u8981\u4e0d\u65ad\u5b9e\u8df5\u548c\u79ef\u7d2f\u7ecf\u9a8c\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python3\u8fdb\u884c\u7f16\u7a0b\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u4f7f\u7528Python3\uff0c\u9996\u5148\u9700\u8981\u5728\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5Python\u3002\u53ef\u4ee5\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u6216\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001Visual Studio Code\u7b49\u6765\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801\u3002\u5b66\u4e60\u57fa\u7840\u8bed\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u548c\u63a7\u5236\u7ed3\u6784\u662f\u5165\u95e8\u7684\u5173\u952e\uff0c\u8bb8\u591a\u5728\u7ebf\u6559\u7a0b\u548c\u5b66\u4e60\u5e73\u53f0\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b66\u4e60\u8d44\u6e90\u3002<\/p>\n<p><strong>Python3\u7684\u4e3b\u8981\u7279\u70b9\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python3\u5177\u6709\u591a\u79cd\u663e\u8457\u7279\u70b9\uff0c\u4f7f\u5176\u6210\u4e3a\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u9996\u5148\uff0cPython3\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5305\u62ec\u9762\u5411\u5bf9\u8c61\u3001\u547d\u4ee4\u5f0f\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002\u5176\u6b21\uff0cPython3\u7684\u8bed\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u53ef\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\u3002Python3\u8fd8\u5177\u6709\u826f\u597d\u7684\u793e\u533a\u652f\u6301\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u89e3\u51b3\u65b9\u6848\u548c\u8d44\u6e90\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\u5982\u4f55\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93\u901a\u5e38\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165<code>pip install \u5e93\u540d<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u4f7f\u7528<code>pip list<\/code>\u53ef\u4ee5\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\uff0c\u4f7f\u7528<code>pip uninstall \u5e93\u540d<\/code>\u53ef\u4ee5\u5378\u8f7d\u4e0d\u9700\u8981\u7684\u5e93\u3002\u6b64\u5916\uff0c\u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u5982\u4f7f\u7528venv\u6216virtualenv\uff09\u662f\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u7684\u597d\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u4e0d\u4f1a\u76f8\u4e92\u5e72\u6270\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python 3\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u6613\u4e8e\u5b66\u4e60\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u9886\u57df\u3002\u5b66\u4e60Python 3\u9700\u8981\u5b89\u88c5Python\u89e3 [&hellip;]","protected":false},"author":3,"featured_media":1151053,"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\/1151048"}],"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=1151048"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151048\/revisions"}],"predecessor-version":[{"id":1151056,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151048\/revisions\/1151056"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151053"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}