{"id":91,"date":"2026-06-13T19:04:36","date_gmt":"2026-06-13T11:04:36","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=91"},"modified":"2026-06-13T19:04:36","modified_gmt":"2026-06-13T11:04:36","slug":"python3-%e6%9d%a1%e4%bb%b6%e6%8e%a7%e5%88%b6-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/13\/python3-%e6%9d%a1%e4%bb%b6%e6%8e%a7%e5%88%b6-2\/","title":{"rendered":"Python3 \u6761\u4ef6\u63a7\u5236"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<strong>\u6761\u4ef6\u63a7\u5236<\/strong>\uff08Conditionals\uff09\u662f\u7a0b\u5e8f\u505a\u51fa\u51b3\u7b56\u7684\u6838\u5fc3\u673a\u5236\u3002\u4e0e C\u3001Java \u7b49\u8bed\u8a00\u4e0d\u540c\uff0cPython \u6ca1\u6709 <code>switch<\/code> \u8bed\u53e5\uff08\u76f4\u5230 Python 3.10 \u5f15\u5165\u4e86 <code>match...case<\/code>\uff09\uff0c\u4e5f\u6ca1\u6709\u62ec\u53f7\u5305\u88f9\u6761\u4ef6\uff0c\u800c\u662f\u4f9d\u9760<strong>\u6e05\u6670\u7684\u7f29\u8fdb<\/strong>\u548c<strong>\u4f18\u96c5\u7684\u771f\u503c\u6d4b\u8bd5<\/strong>\u6765\u6784\u5efa\u903b\u8f91\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u5c06\u4ece<strong>\u57fa\u7840\u8bed\u6cd5<\/strong>\u3001<strong>\u6838\u5fc3\u673a\u5236<\/strong>\u3001<strong>\u9ad8\u7ea7\u6280\u5de7<\/strong>\u5230<strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff0c\u4e3a\u4f60\u7cfb\u7edf\u68b3\u7406 Python 3 \u7684\u6761\u4ef6\u63a7\u5236\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001 \u57fa\u7840\u8bed\u6cd5\uff1a<code>if<\/code> \/ <code>elif<\/code> \/ <code>else<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4f7f\u7528\u5192\u53f7 <code>:<\/code> \u548c<strong>\u7f29\u8fdb<\/strong>\uff08\u901a\u5e38\u662f 4 \u4e2a\u7a7a\u683c\uff09\u6765\u5b9a\u4e49\u4ee3\u7801\u5757\u3002<code>elif<\/code> \u662f <code>else if<\/code> \u7684\u7f29\u5199\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">score = 85\n\nif score &gt;= 90:\n    print(&quot;\u4f18\u79c0&quot;)\nelif score &gt;= 80:\n    print(&quot;\u826f\u597d&quot;)      # \u8fd9\u91cc\u4f1a\u88ab\u6267\u884c\nelif score &gt;= 60:\n    print(&quot;\u53ca\u683c&quot;)\nelse:\n    print(&quot;\u4e0d\u53ca\u683c&quot;)<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\ud83d\udca1 \u6ce8\u610f\uff1a\u6761\u4ef6\u5224\u65ad\u662f\u4ece\u4e0a\u5230\u4e0b\u4f9d\u6b21\u8fdb\u884c\u7684\uff0c\u4e00\u65e6\u67d0\u4e2a\u6761\u4ef6\u4e3a <code>True<\/code>\uff0c\u6267\u884c\u5b8c\u5bf9\u5e94\u4ee3\u7801\u5757\u540e\uff0c<strong>\u6574\u4e2a <code>if<\/code> \u7ed3\u6784\u5c31\u4f1a\u7acb\u5373\u7ed3\u675f<\/strong>\uff0c\u4e0d\u4f1a\u7ee7\u7eed\u68c0\u67e5\u540e\u9762\u7684 <code>elif<\/code> \u6216 <code>else<\/code>\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001 Python \u7684\u7cbe\u9ad3\uff1a\u771f\u503c\u6d4b\u8bd5 (Truth Value Testing)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c<strong>\u4efb\u4f55\u5bf9\u8c61\u90fd\u53ef\u4ee5\u7528\u4e8e\u6761\u4ef6\u5224\u65ad<\/strong>\u3002Python \u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff08<code>True<\/code> \u6216 <code>False<\/code>\uff09\u3002\u638c\u63e1\u54ea\u4e9b\u503c\u662f <code>False<\/code>\uff0c\u662f\u5199\u51fa &#8220;Pythonic&#8221; \u4ee3\u7801\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u88ab\u89c6\u4e3a <code>False<\/code> (Falsy) \u7684\u503c\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e38\u91cf\uff1a<code>False<\/code>, <code>None<\/code><\/li>\n\n\n\n<li>\u6570\u5b57\u96f6\uff1a<code>0<\/code>, <code>0.0<\/code>, <code>0j<\/code> (\u590d\u6570\u96f6)<\/li>\n\n\n\n<li>\u7a7a\u5e8f\u5217\u6216\u96c6\u5408\uff1a<code>\"\"<\/code> (\u7a7a\u5b57\u7b26\u4e32), <code>[]<\/code> (\u7a7a\u5217\u8868), <code>()<\/code> (\u7a7a\u5143\u7ec4), <code>{}<\/code> (\u7a7a\u5b57\u5178), <code>set()<\/code> (\u7a7a\u96c6\u5408)<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5176\u4ed6\u6240\u6709\u503c\u5747\u88ab\u89c6\u4e3a <code>True<\/code> (Truthy)\u3002<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2705 \u6700\u4f73\u5b9e\u8df5\uff1a\u76f4\u63a5\u5224\u65ad\u5bf9\u8c61\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u5224\u65ad\u5b83\u7684\u957f\u5ea6\u6216\u662f\u5426\u7b49\u4e8e None\u3002<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">my_list = [1, 2, 3]\nname = &quot;&quot;\n\n# \u274c \u65b0\u624b\u5199\u6cd5 (\u5570\u55e6)\nif len(my_list) &gt; 0:\n    print(&quot;\u5217\u8868\u4e0d\u4e3a\u7a7a&quot;)\nif name == &quot;&quot;:\n    print(&quot;\u540d\u5b57\u4e3a\u7a7a&quot;)\nif my_var is not None:\n    print(&quot;\u53d8\u91cf\u6709\u503c&quot;)\n\n# \u2705 Pythonic \u5199\u6cd5 (\u7b80\u6d01\u3001\u9ad8\u6548)\nif my_list:\n    print(&quot;\u5217\u8868\u4e0d\u4e3a\u7a7a&quot;)\nif not name:\n    print(&quot;\u540d\u5b57\u4e3a\u7a7a&quot;)\nif my_var:\n    print(&quot;\u53d8\u91cf\u6709\u503c&quot;)<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001 \u903b\u8f91\u8fd0\u7b97\u7b26\u4e0e\u77ed\u8def\u6c42\u503c (Short-Circuit Evaluation)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u4f7f\u7528\u82f1\u6587\u5355\u8bcd\u4f5c\u4e3a\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a<code>and<\/code>, <code>or<\/code>, <code>not<\/code>\u3002\u5b83\u4eec\u5177\u6709<strong>\u77ed\u8def\u7279\u6027<\/strong>\uff0c\u8fd9\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u8fd0\u7b97\u7b26<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u542b\u4e49<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u77ed\u8def\u7279\u6027<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>A and B<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u903b\u8f91\u4e0e<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u5982\u679c <code>A<\/code> \u4e3a <code>False<\/code>\uff0c\u5219<strong>\u4e0d\u518d\u8ba1\u7b97 <code>B<\/code><\/strong>\uff0c\u76f4\u63a5\u8fd4\u56de <code>A<\/code> \u7684\u503c\u3002<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>A or B<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u903b\u8f91\u6216<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u5982\u679c <code>A<\/code> \u4e3a <code>True<\/code>\uff0c\u5219<strong>\u4e0d\u518d\u8ba1\u7b97 <code>B<\/code><\/strong>\uff0c\u76f4\u63a5\u8fd4\u56de <code>A<\/code> \u7684\u503c\u3002<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>not A<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u903b\u8f91\u975e<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8fd4\u56de <code>A<\/code> \u7684\u5e03\u5c14\u53cd\u503c\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b9e\u7528\u6280\u5de7\uff1a\u5229\u7528 <code>or<\/code> \u63d0\u4f9b\u9ed8\u8ba4\u503c<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u5982\u679c user_input \u4e3a\u7a7a\u5b57\u7b26\u4e32 (Falsy)\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c &quot;Guest&quot;\nuser_input = &quot;&quot;\nusername = user_input or &quot;Guest&quot;\nprint(username)  # \u8f93\u51fa: Guest\n\n# \u5982\u679c user_input \u6709\u503c (Truthy)\uff0c\u5219\u4fdd\u7559\u539f\u503c\nuser_input = &quot;Alice&quot;\nusername = user_input or &quot;Guest&quot;\nprint(username)  # \u8f93\u51fa: Alice<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001 \u9ad8\u7ea7\u6761\u4ef6\u63a7\u5236\u6280\u5de7<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u94fe\u5f0f\u6bd4\u8f83 (Chained Comparisons)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u5141\u8bb8\u4f60\u5c06\u591a\u4e2a\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fde\u5199\uff0c\u8fd9\u5728\u6570\u5b66\u4e0a\u975e\u5e38\u76f4\u89c2\uff0c\u4e14\u6bd4\u4f7f\u7528 <code>and<\/code> \u66f4\u9ad8\u6548\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">x = 5\n\n# \u2705 Pythonic \u5199\u6cd5\nif 1 &lt; x &lt; 10:\n    print(&quot;x \u5728 1 \u5230 10 \u4e4b\u95f4&quot;)\n\n# \u274c \u4f20\u7edf\u5199\u6cd5 (\u4e0d\u63a8\u8350)\nif x &gt; 1 and x &lt; 10:\n    print(&quot;x \u5728 1 \u5230 10 \u4e4b\u95f4&quot;)<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4e09\u5143\u8fd0\u7b97\u7b26 (\u6761\u4ef6\u8868\u8fbe\u5f0f)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u4e8e\u5728\u5355\u884c\u5185\u6839\u636e\u6761\u4ef6\u8d4b\u503c\uff0c\u8bed\u6cd5\u4e3a\uff1a<code>[\u771f\u503c] if [\u6761\u4ef6] else [\u5047\u503c]<\/code>\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">age = 20\n\n# \u4f20\u7edf\u5199\u6cd5\nif age &gt;= 18:\n    status = &quot;\u6210\u5e74&quot;\nelse:\n    status = &quot;\u672a\u6210\u5e74&quot;\n\n# \u2705 \u4e09\u5143\u8fd0\u7b97\u7b26\u5199\u6cd5 (\u7b80\u6d01)\nstatus = &quot;\u6210\u5e74&quot; if age &gt;= 18 else &quot;\u672a\u6210\u5e74&quot;<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3. <code>match...case<\/code> \u7ed3\u6784\u6a21\u5f0f\u5339\u914d (Python 3.10+ \u65b0\u589e)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f Python \u7248\u7684 <code>switch<\/code> \u8bed\u53e5\uff0c\u4f46\u529f\u80fd\u5f3a\u5927\u5f97\u591a\uff0c\u652f\u6301<strong>\u7ed3\u6784\u6a21\u5f0f\u5339\u914d<\/strong>\uff08\u4e0d\u4ec5\u80fd\u5339\u914d\u503c\uff0c\u8fd8\u80fd\u89e3\u6784\u6570\u636e\u7ed3\u6784\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">def handle_command(command):\n    match command.split():\n        case [&quot;quit&quot;]:\n            print(&quot;\u9000\u51fa\u7a0b\u5e8f&quot;)\n        case [&quot;load&quot;, filename]:\n            print(f&quot;\u6b63\u5728\u52a0\u8f7d\u6587\u4ef6: {filename}&quot;)\n        case [&quot;save&quot;, filename]:\n            print(f&quot;\u6b63\u5728\u4fdd\u5b58\u6587\u4ef6: {filename}&quot;)\n        case _:  # _ \u76f8\u5f53\u4e8e default\uff0c\u5339\u914d\u6240\u6709\u5176\u4ed6\u60c5\u51b5\n            print(&quot;\u672a\u77e5\u547d\u4ee4&quot;)\n\nhandle_command(&quot;load data.txt&quot;)  # \u8f93\u51fa: \u6b63\u5728\u52a0\u8f7d\u6587\u4ef6: data.txt<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001 \u5e38\u89c1\u9677\u9631\u4e0e\u907f\u5751\u6307\u5357<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u9677\u9631 1\uff1a\u8bef\u7528 <code>=<\/code> \u548c <code>==<\/code><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>=<\/code> \u662f<strong>\u8d4b\u503c<\/strong>\u8fd0\u7b97\u7b26\u3002<\/li>\n\n\n\n<li><code>==<\/code> \u662f<strong>\u76f8\u7b49\u6027\u6bd4\u8f83<\/strong>\u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u274c \u8bed\u6cd5\u9519\u8bef (\u5728 Python 3.8+ \u4e2d\uff0cif \u8bed\u53e5\u5185\u4e0d\u5141\u8bb8\u76f4\u63a5\u8d4b\u503c\uff0c\u9664\u975e\u4f7f\u7528\u6d77\u8c61\u8fd0\u7b97\u7b26 :=)\nif x = 5: \n    pass\n\n# \u2705 \u6b63\u786e\u5199\u6cd5\nif x == 5:\n    pass<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u9677\u9631 2\uff1a\u8fc7\u5ea6\u5d4c\u5957 (Arrow Anti-Pattern)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6df1\u5c42\u5d4c\u5957\u7684 <code>if<\/code> \u4f1a\u8ba9\u4ee3\u7801\u53d8\u6210\u201c\u7bad\u5934\u201d\u5f62\u72b6\uff0c\u6781\u96be\u9605\u8bfb\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><span>26<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u274c \u7cdf\u7cd5\u7684\u5d4c\u5957\ndef process_user(user):\n    if user:\n        if user.is_active:\n            if user.has_permission:\n                print(&quot;\u5141\u8bb8\u8bbf\u95ee&quot;)\n            else:\n                print(&quot;\u65e0\u6743\u9650&quot;)\n        else:\n            print(&quot;\u8d26\u6237\u672a\u6fc0\u6d3b&quot;)\n    else:\n        print(&quot;\u7528\u6237\u4e0d\u5b58\u5728&quot;)\n\n# \u2705 \u63a8\u8350\u505a\u6cd5\uff1a\u63d0\u524d\u8fd4\u56de (Early Return \/ Guard Clauses)\ndef process_user(user):\n    if not user:\n        print(&quot;\u7528\u6237\u4e0d\u5b58\u5728&quot;)\n        return\n    if not user.is_active:\n        print(&quot;\u8d26\u6237\u672a\u6fc0\u6d3b&quot;)\n        return\n    if not user.has_permission:\n        print(&quot;\u65e0\u6743\u9650&quot;)\n        return\n\n    print(&quot;\u5141\u8bb8\u8bbf\u95ee&quot;) # \u6838\u5fc3\u903b\u8f91\u653e\u5728\u6700\u5916\u5c42\uff0c\u65e0\u9700\u5d4c\u5957<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u9677\u9631 3\uff1a\u6df7\u6dc6 <code>is<\/code> \u548c <code>==<\/code><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>==<\/code> \u6bd4\u8f83\u7684\u662f<strong>\u503c<\/strong>\u662f\u5426\u76f8\u7b49\u3002<\/li>\n\n\n\n<li><code>is<\/code> \u6bd4\u8f83\u7684\u662f<strong>\u5185\u5b58\u5730\u5740<\/strong>\uff08\u5373\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">a = [1, 2, 3]\nb = [1, 2, 3]\n\nprint(a == b)  # True  (\u503c\u76f8\u7b49)\nprint(a is b)  # False (\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5217\u8868\u5bf9\u8c61)\n\n# \u2705 \u552f\u4e00\u4f8b\u5916\uff1a\u5224\u65ad\u662f\u5426\u4e3a None \u65f6\uff0c\u5fc5\u987b\u4f7f\u7528 is\nif my_var is None:\n    pass<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684\u6761\u4ef6\u63a7\u5236\u54f2\u5b66\u662f <strong>\u201c\u7b80\u6d01\u201d\u4e0e\u201c\u53ef\u8bfb\u6027\u201d<\/strong>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5584\u7528\u7f29\u8fdb\uff0c\u4fdd\u6301\u4ee3\u7801\u6574\u6d01\u3002<\/li>\n\n\n\n<li>\u5229\u7528 Python \u7684<strong>\u771f\u503c\u6d4b\u8bd5<\/strong>\u7279\u6027\uff0c\u5199\u51fa <code>if my_list:<\/code> \u8fd9\u6837\u4f18\u96c5\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u9047\u5230\u6df1\u5c42\u5d4c\u5957\u65f6\uff0c\u679c\u65ad\u4f7f\u7528<strong>\u63d0\u524d\u8fd4\u56de (Early Return)<\/strong> \u91cd\u6784\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u6761\u4ef6\u8d4b\u503c\uff0c\u5927\u80c6\u4f7f\u7528<strong>\u4e09\u5143\u8fd0\u7b97\u7b26<\/strong>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u73b0\u5728\u662f\u5426\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\uff08\u6bd4\u5982\u8868\u5355\u9a8c\u8bc1\u3001\u72b6\u6001\u673a\u6d41\u8f6c\uff09\u4e0d\u77e5\u9053\u5982\u4f55\u7528 <code>if<\/code> \u4f18\u96c5\u5730\u8868\u8fbe\uff1f\u53ef\u4ee5\u53d1\u7ed9\u6211\uff0c\u6211\u5e2e\u4f60\u7528 Pythonic \u7684\u65b9\u5f0f\u91cd\u5199\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u6761\u4ef6\u63a7\u5236\uff08Conditionals&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}