{"id":204015,"date":"2025-05-29T12:26:03","date_gmt":"2025-05-29T04:26:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204015\/"},"modified":"2025-05-29T12:26:03","modified_gmt":"2025-05-29T04:26:03","slug":"python-%e4%b8%ad%e7%9a%84%e7%bb%93%e6%9e%84%e6%a8%a1%e5%bc%8f%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204015\/","title":{"rendered":"Python \u4e2d\u7684\u7ed3\u6784\u6a21\u5f0f\u5339\u914d"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d\u7684\u7ed3\u6784\u6a21\u5f0f\u5339\u914d<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPython \u4e2d\u7684\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241018\/172925122467124798092e5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u662fpython\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u60a8\u6839\u636e\u590d\u6742\u6570\u636e\u7684\u7ed3\u6784\u505a\u51fa\u51b3\u7b56\u5e76\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u503c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u3001\u58f0\u660e\u5f0f\u7684\u65b9\u5f0f\u6765\u8868\u8fbe\u6761\u4ef6\u903b\u8f91\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4e00\u4e9b\u5728 python \u4e2d\u4f7f\u7528\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u7684\u771f\u5b9e\u6848\u4f8b\u7814\u7a76\u793a\u4f8b\u3002<\/p>\n<p><strong>1\u3002\u89e3\u6790 api \u54cd\u5e94<\/strong><br \/> \u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u7684\u4e00\u79cd\u5e38\u89c1\u7528\u4f8b\u662f\u89e3\u6790 api \u54cd\u5e94\u3002\u5047\u8bbe\u60a8\u6b63\u5728\u4f7f\u7528\u4e00\u4e2a\u5929\u6c14 api\uff0c\u8be5 api \u8fd4\u56de\u4ee5\u4e0b\u683c\u5f0f\u7684\u6570\u636e\uff1a<\/p>\n<pre>{\n  \"current_weather\": {\n    \"location\": \"new york\",\n    \"temperature\": 25,\n    \"conditions\": \"sunny\"\n  }\n}\n<\/pre>\n<p>\u8981\u4ece\u6b64\u54cd\u5e94\u4e2d\u63d0\u53d6\u6e29\u5ea6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>response = {\n  \"current_weather\": {\n    \"location\": \"new york\",\n    \"temperature\": 25,\n    \"conditions\": \"sunny\"\n  }\n}\n\nmatch response:\n    case {\"current_weather\": {\"temperature\": temp}}:\n        print(f\"the current temperature in {response['current_weather']['location']} is {temp} degrees celsius.\")\n    case _:\n        print(\"invalid response.\")\n\n<\/pre>\n<p>\u6b64\u6a21\u5f0f\u5339\u914d\u4efb\u4f55\u5177\u6709\u201ccurrent_weather\u201d\u952e\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u5728\u8be5\u952e\u4e2d\uff0c\u5b83\u5339\u914d\u201c\u6e29\u5ea6\u201d\u503c\u5e76\u5c06\u5176\u63d0\u53d6\u4e3a\u53d8\u91cf temp\u3002\u8fd9\u4f7f\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6240\u9700\u7684\u6570\u636e\uff0c\u800c\u65e0\u9700\u7f16\u5199\u591a\u4e2a if \u8bed\u53e5\u6765\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p><strong>2\u3002\u6570\u636e\u5904\u7406<\/strong><br \/> \u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u4e5f\u5f88\u6709\u7528\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6709\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u4e0d\u540c\u4ea7\u54c1\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5b83\u4eec\u7684\u540d\u79f0\u3001\u7c7b\u522b\u548c\u4ef7\u683c\u3002\u60a8\u5e0c\u671b\u8fc7\u6ee4\u6570\u636e\u96c6\u4ee5\u4ec5\u5305\u542b\u4f4e\u4e8e\u7279\u5b9a\u4ef7\u683c\u9608\u503c\u7684\u4ea7\u54c1\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u5e76\u5bf9\u5176\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>products = [\n  {\"name\": \"smartphone\", \"category\": \"electronics\", \"price\": 500},\n  {\"name\": \"t-shirt\", \"category\": \"clothing\", \"price\": 20},\n  {\"name\": \"headphones\", \"category\": \"electronics\", \"price\": 100},\n  {\"name\": \"jeans\", \"category\": \"clothing\", \"price\": 50},\n]\n\nmatch products:\n    case [{\"category\": \"electronics\", \"price\": price} for price in range(200)] as electronics:\n        print([product[\"name\"] for product in electronics])\n    case [{\"category\": \"clothing\", \"price\": price} for price in range(40)] as clothing:\n        print([product[\"name\"] for product in clothing])\n    case _:\n        print(\"no products found.\")\n\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6a21\u5f0f\u6839\u636e\u7c7b\u522b\u548c\u4ef7\u683c\u7ea6\u675f\u5339\u914d\u5e76\u63d0\u53d6\u503c\u3002\u8fd9\u5141\u8bb8\u4f7f\u7528\u66f4\u7b80\u6d01\u548c\u53ef\u8bfb\u7684\u65b9\u6cd5\u6765\u8fc7\u6ee4\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>3\u3002\u9a8c\u8bc1\u7528\u6237\u8f93\u5165<\/strong><br \/> \u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u5bf9\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u4e5f\u5f88\u6709\u7528\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6b63\u5728\u4e3a\u4e00\u4e2a\u7f51\u7ad9\u521b\u5efa\u6ce8\u518c\u8868\u5355\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u786e\u4fdd\u7528\u6237\u7684\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\u6b63\u786e\u5e76\u4e14\u5176\u5bc6\u7801\u6ee1\u8db3\u67d0\u4e9b\u8981\u6c42\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u6267\u884c\u8fd9\u4e9b\u9a8c\u8bc1\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>import re\n\nemail = \"test@test.com\"\npassword = \"12345\"\n\nmatch email:\n    case _ if not re.match(r\"^\\w+@[a-za-z_]+?\\.[a-za-z]{2,3}$\", email):\n        print(\"invalid email format.\")\n    case _ if len(password) &lt; 6:\n        print(\"password must be at least 6 characters.\")\n    case _:\n        print(\"sign-up successful.\")\n\n<\/pre>\n<p>\u6b64\u6a21\u5f0f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\uff0c\u5e76\u4f7f\u7528\u957f\u5ea6\u68c0\u67e5\u6765\u5339\u914d\u548c\u9a8c\u8bc1\u5bc6\u7801\u957f\u5ea6\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f7b\u677e\u6269\u5c55\u4ee5\u5305\u542b\u989d\u5916\u7684\u9a8c\u8bc1\u3002<\/p>\n<p><strong>4\u3002\u52a8\u6001\u8c03\u5ea6\u51fd\u6570<\/strong><br \/> \u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u7684\u53e6\u4e00\u4e2a\u6709\u8da3\u7684\u7528\u4f8b\u662f\u6839\u636e\u8f93\u5165\u53c2\u6570\u52a8\u6001\u8c03\u5ea6\u51fd\u6570\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u60a8\u6b63\u5728\u4f7f\u7528\u4e00\u4e2a\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5176\u4e2d\u8f93\u5165\u4e00\u4e2a\u8fd0\u7b97\u548c\u4e24\u4e2a\u6570\u5b57\uff0c\u8be5\u7a0b\u5e8f\u5c06\u4e3a\u5b83\u4eec\u6267\u884c\u8ba1\u7b97\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6839\u636e\u6307\u5b9a\u7684\u64cd\u4f5c\u6267\u884c\u6b63\u786e\u7684\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>from operator import add, sub, mul, truediv as div\n\ndef calculate(operator, num1, num2):\n    match operator:\n        case \"+\":\n            return add(num1, num2)\n        case \"-\":\n            return sub(num1, num2)\n        case \"*\":\n            return mul(num1, num2)\n        case \"\/\":\n            return div(num1, num2)\n        case _:\n            print(\"Invalid operation.\")\n\nresult = calculate(\"*\", 5, 3)\nprint(f\"The result is: {result}\")\n# Output: The result is: 15\n\n<\/pre>\n<p>\u6b64\u6a21\u5f0f\u5339\u914d\u6307\u5b9a\u7684\u8fd0\u7b97\u7b26\u5e76\u6267\u884c\u8fd0\u7b97\u7b26\u6a21\u5757\u4e2d\u7684\u76f8\u5e94\u51fd\u6570\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u7d27\u51d1\u4e14\u53ef\u6269\u5c55\u7684\u65b9\u6cd5\u6765\u5904\u7406\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u7f16\u5199\u591a\u4e2a if \u8bed\u53e5\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><br \/> \u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u662f python \u4e2d\u7684\u4e00\u9879\u5f3a\u5927\u529f\u80fd\uff0c\u53ef\u5b9e\u73b0\u7b80\u6d01\u3001\u58f0\u660e\u6027\u548c\u9009\u62e9\u6027\u4ee3\u7801\u3002\u5b83\u53ef\u7528\u4e8e\u591a\u79cd\u573a\u666f\uff0c\u4ece\u89e3\u6790 api \u54cd\u5e94\u5230\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u548c\u52a8\u6001\u8c03\u5ea6\u51fd\u6570\u3002\u901a\u8fc7\u5229\u7528\u7ed3\u6784\u6a21\u5f0f\uff0c\u60a8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u4f7f\u590d\u6742\u7684\u903b\u8f91\u66f4\u6613\u4e8e\u7ba1\u7406\u3002 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPython \u4e2d\u7684\u7ed3\u6784\u6a21\u5f0f\u5339\u914d\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d\u7684\u7ed3\u6784\u6a21\u5f0f\u5339\u914d &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204015","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204015"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204015\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}