{"id":2711,"date":"2025-03-14T21:47:27","date_gmt":"2025-03-14T13:47:27","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2711"},"modified":"2025-03-14T21:47:27","modified_gmt":"2025-03-14T13:47:27","slug":"python-3-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-3-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python 3 \u57fa\u7840\u8bed\u6cd5\u8be6\u89e3"},"content":{"rendered":"\n<p>Python 3 \u662f\u4e00\u79cd\u7b80\u5355\u6613\u5b66\u4f46\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u3001\u6570\u636e\u5206\u6790\u3001\u4eba\u5de5\u667a\u80fd\u7b49\u9886\u57df\u3002\u672c\u6307\u5357\u8be6\u7ec6\u4ecb\u7ecd Python 3 \u7684\u57fa\u7840\u8bed\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u638c\u63e1 Python \u7f16\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Python 3 \u4ee3\u7801\u7ed3\u6784<\/strong><\/h2>\n\n\n\n<p>Python 3 \u4f7f\u7528 <strong>\u7f29\u8fdb<\/strong> \u6765\u7ec4\u7ec7\u4ee3\u7801\u5757\uff0c\u800c\u4e0d\u662f <code>{}<\/code> \u6216 <code>begin\/end<\/code> \u8bed\u6cd5\u5757\u3002<\/p>\n\n\n\n<p>\u2705 <strong>\u6b63\u786e\u7684\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if True:\n    print(\"\u7f29\u8fdb\u6b63\u786e\")\nelse:\n    print(\"\u7f29\u8fdb\u6b63\u786e\")<\/code><\/pre>\n\n\n\n<p>\u274c <strong>\u9519\u8bef\u7684\u793a\u4f8b\uff08\u6ca1\u6709\u6b63\u786e\u7f29\u8fdb\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if True:\nprint(\"\u9519\u8bef\u7684\u7f29\u8fdb\")  # \u8fd9\u91cc\u6ca1\u6709\u7f29\u8fdb\uff0c\u4f1a\u62a5\u9519<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u53d8\u91cf\u4e0e\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p>Python \u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u76f4\u63a5\u8d4b\u503c\u5373\u53ef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u53d8\u91cf\u8d4b\u503c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10      # \u6574\u6570\ny = 3.14    # \u6d6e\u70b9\u6570\nname = \"Python\"  # \u5b57\u7b26\u4e32\nis_valid = True  # \u5e03\u5c14\u503c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u6570\u636e\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>Python \u63d0\u4f9b\u4ee5\u4e0b\u4e3b\u8981\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td>int<\/td><td>\u6574\u6570<\/td><td><code>x = 100<\/code><\/td><\/tr><tr><td>float<\/td><td>\u6d6e\u70b9\u6570<\/td><td><code>y = 3.14<\/code><\/td><\/tr><tr><td>str<\/td><td>\u5b57\u7b26\u4e32<\/td><td><code>name = \"Hello\"<\/code><\/td><\/tr><tr><td>bool<\/td><td>\u5e03\u5c14\u503c<\/td><td><code>flag = True<\/code><\/td><\/tr><tr><td>list<\/td><td>\u5217\u8868<\/td><td><code>numbers = [1, 2, 3]<\/code><\/td><\/tr><tr><td>tuple<\/td><td>\u5143\u7ec4<\/td><td><code>coordinates = (10, 20)<\/code><\/td><\/tr><tr><td>dict<\/td><td>\u5b57\u5178<\/td><td><code>person = {\"name\": \"Alice\", \"age\": 25}<\/code><\/td><\/tr><tr><td>set<\/td><td>\u96c6\u5408<\/td><td><code>unique_nums = {1, 2, 3}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\ny = str(x)  # \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\nz = float(x)  # \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u6ce8\u91ca<\/strong><\/h2>\n\n\n\n<p>Python \u652f\u6301\u5355\u884c\u548c\u591a\u884c\u6ce8\u91ca\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8fd9\u662f\u5355\u884c\u6ce8\u91ca\n\n\"\"\"\n\u8fd9\u662f\u591a\u884c\u6ce8\u91ca\uff0c\n\u9002\u7528\u4e8e\u957f\u6587\u672c\u63cf\u8ff0\n\"\"\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u8f93\u5165\u4e0e\u8f93\u51fa<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u8f93\u51fa<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello, Python!\")  <\/code><\/pre>\n\n\n\n<p>\u591a\u4e2a\u53d8\u91cf\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\nage = 25\nprint(\"\u59d3\u540d:\", name, \"\u5e74\u9f84:\", age)  # \u8f93\u51fa\uff1a\u59d3\u540d: Alice \u5e74\u9f84: 25<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u8f93\u5165<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"\u8bf7\u8f93\u5165\u4f60\u7684\u540d\u5b57: \")\nprint(\"\u4f60\u597d,\", name)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u8fd0\u7b97\u7b26<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u7b97\u672f\u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\nb = 3\nprint(a + b)  # \u52a0\u6cd5 13\nprint(a - b)  # \u51cf\u6cd5 7\nprint(a * b)  # \u4e58\u6cd5 30\nprint(a \/ b)  # \u9664\u6cd5 3.3333\nprint(a \/\/ b)  # \u6574\u9664 3\nprint(a % b)  # \u53d6\u6a21 1\nprint(a ** b)  # \u5e42\u8fd0\u7b97 1000<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2 \u903b\u8f91\u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>x = True\ny = False\nprint(x and y)  # False\nprint(x or y)   # True\nprint(not x)    # False<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.3 \u5173\u7cfb\u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>print(10 &gt; 5)   # True\nprint(10 == 5)  # False\nprint(10 != 5)  # True<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u6761\u4ef6\u8bed\u53e5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>age = 18\nif age &gt;= 18:\n    print(\"\u53ef\u4ee5\u6295\u7968\")\nelif age &gt;= 16:\n    print(\"\u53ef\u4ee5\u9a7e\u9a76\")\nelse:\n    print(\"\u672a\u6210\u5e74\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u5faa\u73af<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 for \u5faa\u73af<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)  # \u8f93\u51fa 0 1 2 3 4<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.2 while \u5faa\u73af<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 0\nwhile x &lt; 5:\n    print(x)\n    x += 1<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u5217\u8868\uff08List\uff09<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"\u82f9\u679c\", \"\u9999\u8549\", \"\u6a59\u5b50\"]\nprint(fruits&#91;0])  # \u8bbf\u95ee\u5143\u7d20\nfruits.append(\"\u8461\u8404\")  # \u6dfb\u52a0\u5143\u7d20\nfruits.remove(\"\u9999\u8549\")  # \u79fb\u9664\u5143\u7d20\nprint(len(fruits))  # \u83b7\u53d6\u957f\u5ea6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u5b57\u5178\uff08Dictionary\uff09<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25}\nprint(person&#91;\"name\"])  # \u8bbf\u95ee\u5143\u7d20\nperson&#91;\"city\"] = \"\u5317\u4eac\"  # \u6dfb\u52a0\u952e\u503c\u5bf9\nprint(person.keys())  # \u83b7\u53d6\u6240\u6709\u952e<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. \u51fd\u6570<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    return \"\u4f60\u597d, \" + name\n\nprint(greet(\"Alice\"))  # \u4f60\u597d, Alice<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10.1 \u5e26\u9ed8\u8ba4\u53c2\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name=\"\u4e16\u754c\"):\n    print(\"\u4f60\u597d,\", name)\n\ngreet()  # \u4f60\u597d, \u4e16\u754c<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11. \u7c7b\u4e0e\u5bf9\u8c61<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def introduce(self):\n        print(f\"\u6211\u662f {self.name}\uff0c\u4eca\u5e74 {self.age} \u5c81\")\n\np = Person(\"Alice\", 25)\np.introduce()  # \u6211\u662f Alice\uff0c\u4eca\u5e74 25 \u5c81<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12. \u6587\u4ef6\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.1 \u8bfb\u53d6\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    content = file.read()\n    print(content)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.2 \u5199\u5165\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"w\") as file:\n    file.write(\"Hello, Python!\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>13. \u5f02\u5e38\u5904\u7406<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(input(\"\u8bf7\u8f93\u5165\u6570\u5b57: \"))\n    print(10 \/ num)\nexcept ZeroDivisionError:\n    print(\"\u4e0d\u80fd\u9664\u4ee5 0\")\nexcept ValueError:\n    print(\"\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u5b57\")\nfinally:\n    print(\"\u7a0b\u5e8f\u7ed3\u675f\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u2705 Python \u4f7f\u7528 <strong>\u7f29\u8fdb<\/strong> \u7ec4\u7ec7\u4ee3\u7801<br>\u2705 <strong>\u52a8\u6001\u7c7b\u578b<\/strong>\uff0c\u53d8\u91cf\u65e0\u9700\u58f0\u660e\u7c7b\u578b<br>\u2705 \u652f\u6301 <strong>\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7ec4\u3001\u96c6\u5408<\/strong><br>\u2705 <strong>if-else<\/strong> \u63a7\u5236\u6d41\uff0c<strong>for\/while<\/strong> \u5faa\u73af<br>\u2705 <strong>\u51fd\u6570<\/strong> \u4f7f\u7528 <code>def<\/code> \u5b9a\u4e49<br>\u2705 <strong>\u9762\u5411\u5bf9\u8c61<\/strong> \u7f16\u7a0b\uff1a\u7c7b\u548c\u5bf9\u8c61<br>\u2705 <strong>\u5f02\u5e38\u5904\u7406<\/strong> \u4fdd\u8bc1\u7a0b\u5e8f\u5065\u58ee\u6027<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3 \u662f\u4e00\u79cd\u7b80\u5355\u6613\u5b66\u4f46\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u3001\u6570\u636e\u5206\u6790\u3001\u4eba\u5de5\u667a\u80fd\u7b49\u9886\u57df\u3002\u672c\u6307\u5357 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2712,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2711"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2711\/revisions"}],"predecessor-version":[{"id":2713,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2711\/revisions\/2713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2712"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}