{"id":2778,"date":"2025-03-14T23:00:10","date_gmt":"2025-03-14T15:00:10","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2778"},"modified":"2025-03-14T23:00:10","modified_gmt":"2025-03-14T15:00:10","slug":"python3-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python3-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python3 \u8f93\u5165\u548c\u8f93\u51fa\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u8f93\u5165\u8f93\u51fa\u662f\u4e0e\u7528\u6237\u4ea4\u4e92\u548c\u5904\u7406\u6570\u636e\u7684\u6838\u5fc3\u90e8\u5206\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u8f93\u5165\u8f93\u51fa\uff08I\/O\uff09\u64cd\u4f5c\uff0c\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3 Python3 \u4e2d\u7684\u8f93\u5165\u548c\u8f93\u51fa\u64cd\u4f5c\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. \u8f93\u51fa\uff08Print\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u8f93\u51fa\u901a\u5e38\u662f\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u5b83\u5c06\u6307\u5b9a\u7684\u5185\u5bb9\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\uff08\u901a\u5e38\u662f\u5c4f\u5e55\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u7528\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello, World!\")<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4f1a\u5728\u5c4f\u5e55\u4e0a\u6253\u5370\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, World!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6253\u5370\u591a\u4e2a\u503c\uff1a<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 <code>print()<\/code> \u51fd\u6570\u4e2d\u4f20\u9012\u591a\u4e2a\u503c\uff0c\u5b83\u4eec\u4f1a\u81ea\u52a8\u7528\u7a7a\u683c\u9694\u5f00\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\nb = 20\nprint(a, b)  # \u8f93\u51fa: 10 20<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6253\u5370\u4e0d\u6362\u884c\uff1a<\/strong><\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>print()<\/code> \u51fd\u6570\u5728\u8f93\u51fa\u5185\u5bb9\u540e\u4f1a\u6362\u884c\u3002\u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u6362\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>end<\/code> \u53c2\u6570\u6765\u63a7\u5236\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello\", end=\" \")\nprint(\"World!\")  # \u8f93\u51fa: Hello World!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 f-string\uff08\u63a8\u8350\u65b9\u6cd5\uff0cPython 3.6 \u53ca\u4ee5\u4e0a\uff09<\/strong> f-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u4e2a\u975e\u5e38\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0 <code>f<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   name = \"Alice\"\n   age = 25\n   print(f\"My name is {name} and I am {age} years old.\")  # \u8f93\u51fa: My name is Alice and I am 25 years old.<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>format()<\/code> \u65b9\u6cd5<\/strong> \u5728 Python 3 \u4e2d\uff0c<code>str.format()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u4f60\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u7f6e\u82b1\u62ec\u53f7 <code>{}<\/code>\uff0c\u7136\u540e\u7528 <code>format()<\/code> \u65b9\u6cd5\u586b\u5145\u5b83\u4eec\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   name = \"Bob\"\n   age = 30\n   print(\"My name is {} and I am {} years old.\".format(name, age))  # \u8f93\u51fa: My name is Bob and I am 30 years old.<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u767e\u5206\u53f7 <code>%<\/code> \u683c\u5f0f\u5316<\/strong> \u4f20\u7edf\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u901a\u8fc7 <code>%<\/code> \u6765\u6307\u5b9a\u7c7b\u578b\u548c\u4f4d\u7f6e\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   name = \"Charlie\"\n   age = 35\n   print(\"My name is %s and I am %d years old.\" % (name, age))  # \u8f93\u51fa: My name is Charlie and I am 35 years old.<\/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. \u8f93\u5165\uff08Input\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u83b7\u53d6\u7528\u6237\u8f93\u5165\u901a\u5e38\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u5b9e\u73b0\u3002\u5b83\u5141\u8bb8\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u4e00\u884c\u8f93\u5165\uff0c\u8fd4\u56de\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u7528\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>user_input = input(\"Enter your name: \")\nprint(f\"Hello, {user_input}!\")<\/code><\/pre>\n\n\n\n<p>\u5f53\u4f60\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u540d\u5b57\uff0c\u8f93\u5165\u540e\u8fd4\u56de\u5e76\u6253\u5370\u95ee\u5019\u8bed\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8f93\u5165\u7684\u7c7b\u578b\uff1a<\/strong><\/h3>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>input()<\/code> \u51fd\u6570\u63a5\u6536\u7684\u8f93\u5165\u59cb\u7ec8\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\uff0c\u5219\u9700\u8981\u624b\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = input(\"Enter your age: \")\nage = int(age)  # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\nprint(f\"You are {age} years old.\")<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff0c\u53ef\u4ee5\u5728 <code>input()<\/code> \u8bed\u53e5\u4e2d\u76f4\u63a5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = int(input(\"Enter your age: \"))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5904\u7406\u5f02\u5e38\u8f93\u5165\uff1a<\/strong><\/h3>\n\n\n\n<p>\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u53ef\u80fd\u4e0d\u7b26\u5408\u9884\u671f\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u5904\u7406\u8f93\u5165\u65f6\u901a\u5e38\u9700\u8981\u8003\u8651\u5f02\u5e38\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try-except<\/code> \u5757\u6765\u6355\u6349\u8f6c\u6362\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    age = int(input(\"Enter your age: \"))\n    print(f\"You are {age} years old.\")\nexcept ValueError:\n    print(\"Invalid input! Please enter a valid number.\")<\/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. \u6587\u4ef6\u8f93\u5165\u8f93\u51fa\uff08File I\/O\uff09<\/strong><\/h2>\n\n\n\n<p>\u9664\u4e86\u63a7\u5236\u53f0\u7684\u8f93\u5165\u8f93\u51fa\uff0cPython \u8fd8\u63d0\u4f9b\u4e86\u5bf9\u6587\u4ef6\u7684\u8bfb\u5199\u652f\u6301\u3002\u6587\u4ef6 I\/O \u64cd\u4f5c\u4e3b\u8981\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6253\u5f00\u6587\u4ef6\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"w\")  # \u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u5199\u64cd\u4f5c\uff08\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u6587\u4ef6\uff09\nfile.write(\"Hello, file!\")  # \u5199\u5165\u5185\u5bb9\nfile.close()  # \u5173\u95ed\u6587\u4ef6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6587\u4ef6\u7684\u8bfb\u64cd\u4f5c\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"r\")  # \u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\ncontent = file.read()  # \u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u5185\u5bb9\nprint(content)\nfile.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u7528\u7684\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"r\"<\/code>\uff1a\u53ea\u8bfb\u6a21\u5f0f\uff0c\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\u3002<\/li>\n\n\n\n<li><code>\"w\"<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\uff0c\u82e5\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u6587\u4ef6\uff0c\u82e5\u6587\u4ef6\u5b58\u5728\u5219\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><code>\"a\"<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff0c\u82e5\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u5185\u5bb9\u4f1a\u88ab\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/li>\n\n\n\n<li><code>\"b\"<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff0c\u7528\u4e8e\u5904\u7406\u975e\u6587\u672c\u6587\u4ef6\uff08\u5982\u56fe\u7247\u3001\u89c6\u9891\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>\"rb\"<\/code>, <code>\"wb\"<\/code>\uff1a\u4e8c\u8fdb\u5236\u8bfb\u5199\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>file = open(\"example.txt\", \"r\")\nfor line in file:\n    print(line.strip())  # strip() \u7528\u4e8e\u53bb\u9664\u884c\u5c3e\u7684\u6362\u884c\u7b26\nfile.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6587\u4ef6\u64cd\u4f5c\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08\u63a8\u8350\u4f7f\u7528 <code>with<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\uff0c\u5b83\u4f1a\u81ea\u52a8\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u5173\u95ed\u6587\u4ef6\uff0c\u907f\u514d\u51fa\u73b0\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u7684\u95ee\u9898\u3002<\/p>\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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u51fa<\/strong>\uff1aPython \u4f7f\u7528 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5185\u5bb9\uff0c\u53ef\u4ee5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e76\u63a7\u5236\u8f93\u51fa\u7684\u65b9\u5f0f\uff08\u5982\u4e0d\u6362\u884c\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u5165<\/strong>\uff1a\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u9ed8\u8ba4\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6 I\/O<\/strong>\uff1a\u901a\u8fc7 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fdb\u884c\u8bfb\u53d6\u3001\u5199\u5165\u64cd\u4f5c\uff0c\u5efa\u8bae\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6b63\u786e\u5173\u95ed\u3002<\/li>\n<\/ul>\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>\u5728 Python \u4e2d\uff0c\u8f93\u5165\u8f93\u51fa\u662f\u4e0e\u7528\u6237\u4ea4\u4e92\u548c\u5904\u7406\u6570\u636e\u7684\u6838\u5fc3\u90e8\u5206\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u8f93\u5165\u8f93\u51fa\uff08I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2779,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2778","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\/2778","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=2778"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2778\/revisions"}],"predecessor-version":[{"id":2780,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2778\/revisions\/2780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2779"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}