{"id":574,"date":"2025-10-23T15:17:50","date_gmt":"2025-10-23T07:17:50","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=574"},"modified":"2025-10-23T15:17:50","modified_gmt":"2025-10-23T07:17:50","slug":"python3-%e6%9d%a1%e4%bb%b6%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/23\/python3-%e6%9d%a1%e4%bb%b6%e6%8e%a7%e5%88%b6\/","title":{"rendered":"Python3 \u6761\u4ef6\u63a7\u5236"},"content":{"rendered":"\n<p>Python \u6761\u4ef6\u8bed\u53e5\u662f\u901a\u8fc7\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u7684\u6267\u884c\u7ed3\u679c\uff08True \u6216\u8005 False\uff09\u6765\u51b3\u5b9a\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e0b\u56fe\u6765\u7b80\u5355\u4e86\u89e3\u6761\u4ef6\u8bed\u53e5\u7684\u6267\u884c\u8fc7\u7a0b:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/if-condition.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.jyshare.com\/images\/mix\/python-if.webp\" alt=\"\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">if \u8bed\u53e5<\/h2>\n\n\n\n<p>Python\u4e2dif\u8bed\u53e5\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<p>if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c &#8220;condition_1&#8221; \u4e3a True \u5c06\u6267\u884c &#8220;statement_block_1&#8221; \u5757\u8bed\u53e5<\/li>\n\n\n\n<li>\u5982\u679c &#8220;condition_1&#8221; \u4e3aFalse\uff0c\u5c06\u5224\u65ad &#8220;condition_2&#8221;<\/li>\n\n\n\n<li>\u5982\u679c&#8221;condition_2&#8243; \u4e3a True \u5c06\u6267\u884c &#8220;statement_block_2&#8221; \u5757\u8bed\u53e5<\/li>\n\n\n\n<li>\u5982\u679c &#8220;condition_2&#8221; \u4e3aFalse\uff0c\u5c06\u6267\u884c&#8221;statement_block_3&#8243;\u5757\u8bed\u53e5<\/li>\n<\/ul>\n\n\n\n<p>Python \u4e2d\u7528&nbsp;<strong>elif<\/strong>&nbsp;\u4ee3\u66ff\u4e86&nbsp;<strong>else if<\/strong>\uff0c\u6240\u4ee5if\u8bed\u53e5\u7684\u5173\u952e\u5b57\u4e3a\uff1a<strong>if \u2013 elif \u2013 else<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u3001\u6bcf\u4e2a\u6761\u4ef6\u540e\u9762\u8981\u4f7f\u7528\u5192\u53f7\u00a0<strong>:<\/strong>\uff0c\u8868\u793a\u63a5\u4e0b\u6765\u662f\u6ee1\u8db3\u6761\u4ef6\u540e\u8981\u6267\u884c\u7684\u8bed\u53e5\u5757\u3002<\/li>\n\n\n\n<li>2\u3001\u4f7f\u7528\u7f29\u8fdb\u6765\u5212\u5206\u8bed\u53e5\u5757\uff0c\u76f8\u540c\u7f29\u8fdb\u6570\u7684\u8bed\u53e5\u5728\u4e00\u8d77\u7ec4\u6210\u4e00\u4e2a\u8bed\u53e5\u5757\u3002<\/li>\n\n\n\n<li>3\u3001\u5728 Python \u4e2d\u6ca1\u6709\u00a0<strong>switch&#8230;case<\/strong>\u00a0\u8bed\u53e5\uff0c\u4f46\u5728 Python3.10 \u7248\u672c\u6dfb\u52a0\u4e86\u00a0<strong>match&#8230;case<\/strong>\uff0c\u529f\u80fd\u4e5f\u7c7b\u4f3c\uff0c\u8be6\u89c1\u4e0b\u6587\u3002<\/li>\n<\/ul>\n\n\n\n<p>Gif \u6f14\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.runoob.com\/wp-content\/uploads\/2014\/05\/006faQNTgw1f5wnm0mcxrg30ci07o47l.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 if \u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 var1 = 100 if var1: print (&#8220;1 &#8211; if \u8868\u8fbe\u5f0f\u6761\u4ef6\u4e3a true&#8221;) print (var1) var2 = 0 if var2: print (&#8220;2 &#8211; if \u8868\u8fbe\u5f0f\u6761\u4ef6\u4e3a true&#8221;) print (var2) print (&#8220;Good bye!&#8221;)<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 - if \u8868\u8fbe\u5f0f\u6761\u4ef6\u4e3a true\n100\nGood bye!<\/pre>\n\n\n\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\u7531\u4e8e\u53d8\u91cf var2 \u4e3a 0\uff0c\u6240\u4ee5\u5bf9\u5e94\u7684 if \u5185\u7684\u8bed\u53e5\u6ca1\u6709\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u72d7\u7684\u5e74\u9f84\u8ba1\u7b97\u5224\u65ad\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 age = int(input(&#8220;\u8bf7\u8f93\u5165\u4f60\u5bb6\u72d7\u72d7\u7684\u5e74\u9f84: &#8220;)) print(&#8220;&#8221;) if age &lt;= 0: print(&#8220;\u4f60\u662f\u5728\u9017\u6211\u5427!&#8221;) elif age == 1: print(&#8220;\u76f8\u5f53\u4e8e 14 \u5c81\u7684\u4eba\u3002&#8221;) elif age == 2: print(&#8220;\u76f8\u5f53\u4e8e 22 \u5c81\u7684\u4eba\u3002&#8221;) elif age &gt; 2: human = 22 + (age -2)*5 print(&#8220;\u5bf9\u5e94\u4eba\u7c7b\u5e74\u9f84: &#8220;, human) ### \u9000\u51fa\u63d0\u793a input(&#8220;\u70b9\u51fb enter \u952e\u9000\u51fa&#8221;)<\/p>\n\n\n\n<p>\u5c06\u4ee5\u4e0a\u811a\u672c\u4fdd\u5b58\u5728dog.py\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u8be5\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python3 dog.py \n\u8bf7\u8f93\u5165\u4f60\u5bb6\u72d7\u72d7\u7684\u5e74\u9f84: 1\n\n\u76f8\u5f53\u4e8e 14 \u5c81\u7684\u4eba\u3002\n\u70b9\u51fb enter \u952e\u9000\u51fa<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u4e3aif\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\u8fd0\u7b97\u7b26:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u63cf\u8ff0<\/th><\/tr><tr><td><code>&lt;<\/code><\/td><td>\u5c0f\u4e8e<\/td><\/tr><tr><td><code>&lt;=<\/code><\/td><td>\u5c0f\u4e8e\u6216\u7b49\u4e8e<\/td><\/tr><tr><td><code>&gt;<\/code><\/td><td>\u5927\u4e8e<\/td><\/tr><tr><td><code>&gt;=<\/code><\/td><td>\u5927\u4e8e\u6216\u7b49\u4e8e<\/td><\/tr><tr><td><code>==<\/code><\/td><td>\u7b49\u4e8e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49<\/td><\/tr><tr><td><code>!=<\/code><\/td><td>\u4e0d\u7b49\u4e8e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 # \u7a0b\u5e8f\u6f14\u793a\u4e86 == \u64cd\u4f5c\u7b26 # \u4f7f\u7528\u6570\u5b57 print(5 == 6) # \u4f7f\u7528\u53d8\u91cf x = 5 y = 8 print(x == y)<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">False\nFalse<\/pre>\n\n\n\n<p>high_low.py\u6587\u4ef6\u6f14\u793a\u4e86\u6570\u5b57\u7684\u6bd4\u8f83\u8fd0\u7b97\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 # \u8be5\u5b9e\u4f8b\u6f14\u793a\u4e86\u6570\u5b57\u731c\u8c1c\u6e38\u620f number = 7 guess = -1 print(&#8220;\u6570\u5b57\u731c\u8c1c\u6e38\u620f!&#8221;) while guess != number: guess = int(input(&#8220;\u8bf7\u8f93\u5165\u4f60\u731c\u7684\u6570\u5b57\uff1a&#8221;)) if guess == number: print(&#8220;\u606d\u559c\uff0c\u4f60\u731c\u5bf9\u4e86\uff01&#8221;) elif guess &lt; number: print(&#8220;\u731c\u7684\u6570\u5b57\u5c0f\u4e86&#8230;&#8221;) elif guess &gt; number: print(&#8220;\u731c\u7684\u6570\u5b57\u5927\u4e86&#8230;&#8221;)<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u811a\u672c\uff0c\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python3 high_low.py \n\u6570\u5b57\u731c\u8c1c\u6e38\u620f!\n\u8bf7\u8f93\u5165\u4f60\u731c\u7684\u6570\u5b57\uff1a1\n\u731c\u7684\u6570\u5b57\u5c0f\u4e86...\n\u8bf7\u8f93\u5165\u4f60\u731c\u7684\u6570\u5b57\uff1a9\n\u731c\u7684\u6570\u5b57\u5927\u4e86...\n\u8bf7\u8f93\u5165\u4f60\u731c\u7684\u6570\u5b57\uff1a7\n\u606d\u559c\uff0c\u4f60\u731c\u5bf9\u4e86\uff01<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">if \u5d4c\u5957<\/h2>\n\n\n\n<p>\u5728\u5d4c\u5957 if \u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u628a if&#8230;elif&#8230;else \u7ed3\u6784\u653e\u5728\u53e6\u5916\u4e00\u4e2a if&#8230;elif&#8230;else \u7ed3\u6784\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if \u8868\u8fbe\u5f0f1:\n    \u8bed\u53e5\n    if \u8868\u8fbe\u5f0f2:\n        \u8bed\u53e5\n    elif \u8868\u8fbe\u5f0f3:\n        \u8bed\u53e5\n    else:\n        \u8bed\u53e5\nelif \u8868\u8fbe\u5f0f4:\n    \u8bed\u53e5\nelse:\n    \u8bed\u53e5<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p># !\/usr\/bin\/python3 num=int(input(&#8220;\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a&#8221;)) if num%2==0: if num%3==0: print (&#8220;\u4f60\u8f93\u5165\u7684\u6570\u5b57\u53ef\u4ee5\u6574\u9664 2 \u548c 3&#8221;) else: print (&#8220;\u4f60\u8f93\u5165\u7684\u6570\u5b57\u53ef\u4ee5\u6574\u9664 2\uff0c\u4f46\u4e0d\u80fd\u6574\u9664 3&#8221;) else: if num%3==0: print (&#8220;\u4f60\u8f93\u5165\u7684\u6570\u5b57\u53ef\u4ee5\u6574\u9664 3\uff0c\u4f46\u4e0d\u80fd\u6574\u9664 2&#8221;) else: print (&#8220;\u4f60\u8f93\u5165\u7684\u6570\u5b57\u4e0d\u80fd\u6574\u9664 2 \u548c 3&#8221;)<\/p>\n\n\n\n<p>\u5c06\u4ee5\u4e0a\u7a0b\u5e8f\u4fdd\u5b58\u5230 test_if.py \u6587\u4ef6\u4e2d\uff0c\u6267\u884c\u540e\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python3 test.py \n\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a6\n\u4f60\u8f93\u5165\u7684\u6570\u5b57\u53ef\u4ee5\u6574\u9664 2 \u548c 3<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">match&#8230;case<\/h2>\n\n\n\n<p>Python 3.10 \u589e\u52a0\u4e86<strong>&nbsp;match&#8230;case<\/strong>&nbsp;\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u4e0d\u9700\u8981\u518d\u4f7f\u7528\u4e00\u8fde\u4e32\u7684&nbsp;<strong>if-else<\/strong>&nbsp;\u6765\u5224\u65ad\u4e86\u3002<\/p>\n\n\n\n<p>match \u540e\u7684\u5bf9\u8c61\u4f1a\u4f9d\u6b21\u4e0e case \u540e\u7684\u5185\u5bb9\u8fdb\u884c\u5339\u914d\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u6267\u884c\u5339\u914d\u5230\u7684\u8868\u8fbe\u5f0f\uff0c\u5426\u5219\u76f4\u63a5\u8df3\u8fc7\uff0c<strong>_<\/strong>&nbsp;\u53ef\u4ee5\u5339\u914d\u4e00\u5207\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">match subject:\n    case &lt;pattern_1&gt;:\n        &lt;action_1&gt;\n    case &lt;pattern_2&gt;:\n        &lt;action_2&gt;\n    case &lt;pattern_3&gt;:\n        &lt;action_3&gt;\n    case _:\n        &lt;action_wildcard&gt;<\/pre>\n\n\n\n<p><strong>case _:<\/strong>&nbsp;\u7c7b\u4f3c\u4e8e C \u548c Java \u4e2d\u7684&nbsp;<strong>default:<\/strong>\uff0c\u5f53\u5176\u4ed6 case \u90fd\u65e0\u6cd5\u5339\u914d\u65f6\uff0c\u5339\u914d\u8fd9\u6761\uff0c\u4fdd\u8bc1\u6c38\u8fdc\u4f1a\u5339\u914d\u6210\u529f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p><strong>def<\/strong>&nbsp;http_error(status):<br>&nbsp; &nbsp; match status:<br>&nbsp; &nbsp; &nbsp; &nbsp; case&nbsp;400:<br>&nbsp;<strong>return<\/strong>&nbsp;&#8220;Bad request&#8221;<br>&nbsp; &nbsp; &nbsp; &nbsp; case&nbsp;404:<br>&nbsp;<strong>return<\/strong>&nbsp;&#8220;Not found&#8221;<br>&nbsp; &nbsp; &nbsp; &nbsp; case&nbsp;418:<br>&nbsp;<strong>return<\/strong>&nbsp;&#8220;I&#8217;m a teapot&#8221;<br>&nbsp; &nbsp; &nbsp; &nbsp; case _:<br>&nbsp;<strong>return<\/strong>&nbsp;&#8220;Something&#8217;s wrong with the internet&#8221;<\/p>\n\n\n\n<p>mystatus=400<br><strong>print<\/strong>(http_error(400))<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u8f93\u51fa HTTP \u72b6\u6001\u7801\u7684\u5b9e\u4f8b\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Bad request<\/pre>\n\n\n\n<p>\u4e00\u4e2a case \u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u5339\u914d\u6761\u4ef6\uff0c\u6761\u4ef6\u4f7f\u7528&nbsp;<strong>\uff5c<\/strong>&nbsp;\u9694\u5f00\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">...\n    case 401|403|404:\n        return \"Not allowed\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python \u6761\u4ef6\u8bed\u53e5\u662f\u901a\u8fc7\u4e00\u6761\u6216\u591a\u6761\u8bed\u53e5\u7684\u6267\u884c\u7ed3\u679c\uff08True \u6216\u8005&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":485,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":469,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=574"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":575,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/574\/revisions\/575"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/485"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}