{"id":204756,"date":"2025-05-29T16:18:13","date_gmt":"2025-05-29T08:18:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204756\/"},"modified":"2025-05-29T16:18:13","modified_gmt":"2025-05-29T08:18:13","slug":"python%ef%bc%9a%e7%ae%97%e6%9c%af%e3%80%81%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e5%92%8c%e6%9d%a1%e4%bb%b6%e9%80%bb%e8%be%91%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204756\/","title":{"rendered":"Python\uff1a\u7b97\u672f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u903b\u8f91\u7684\u57fa\u672c\u6982\u5ff5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\uff1a\u7b97\u672f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u903b\u8f91\u7684\u57fa\u672c\u6982\u5ff5<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython\uff1a\u7b97\u672f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u903b\u8f91\u7684\u57fa\u672c\u6982\u5ff5\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241115\/17316477476736d9037a663.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5982\u679c\u60a8\u662f python \u65b0\u624b\uff0c\u4e86\u89e3\u57fa\u672c\u64cd\u4f5c\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u903b\u8f91\u81f3\u5173\u91cd\u8981\u3002\u8ba9\u6211\u4eec\u56de\u987e\u4e00\u4e0b\u4e00\u4e9b\u57fa\u672c\u4e3b\u9898\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u793a\u4f8b\u63a2\u8ba8\u6bcf\u4e2a\u4e3b\u9898\u3002<\/p>\n<hr>\n<h4> \u7b2c 1 \u7ae0\uff1a\u7b97\u672f\u8fd0\u7b97\u7b26 <\/h4>\n<p>python\u63d0\u4f9b\u4e86\u591a\u79cd\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u8f7b\u677e\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u4ee5\u4e0b\u662f\u6700\u5e38\u89c1\u8fd0\u7b97\u7b26\u7684\u5feb\u901f\u6982\u8ff0\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>syntax<\/th>\n<th>action<\/th>\n<th>example<\/th>\n<th>output<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>*<\/td>\n<td>multiply<\/td>\n<td>4 * 10<\/td>\n<td>40<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>addition<\/td>\n<td>7 + 9<\/td>\n<td>16<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>subtract<\/td>\n<td>23 &#8211; 4<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>division<\/td>\n<td>27 \/ 3<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>**<\/td>\n<td>power<\/td>\n<td>3 ** 2<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>modulo<\/td>\n<td>7 % 4<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u5e2e\u52a9\u60a8\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre># multiplication\nresult = 4 * 10\nprint(result)  # output: 40\n\n# addition\ntotal = 7 + 9\nprint(total)  # output: 16\n\n# power\nsquared = 3 ** 2\nprint(squared)  # output: 9\n<\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u4e3a\u53d8\u91cf\u8d4b\u503c\uff1a<\/p>\n<pre># define total spend amount\ntotal_spend = 3150.96\nprint(total_spend)  # output: 3150.96\n<\/pre>\n<hr>\n<h4> \u7b2c 2 \u7ae0\uff1a\u6570\u636e\u7c7b\u578b\u548c\u96c6\u5408 <\/h4>\n<p>\u5728 python \u4e2d\uff0c\u60a8\u6709\u591a\u79cd\u5b58\u50a8\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u9002\u5408\u4e0d\u540c\u7c7b\u578b\u7684\u4efb\u52a1\u3002<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32<\/strong>\uff1a\u7528\u4e8e\u6587\u672c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002<\/p>\n<pre># defining a string\ncustomer_name = 'george boorman'\nprint(customer_name)\n\n# double quotes also work\ncustomer_name = \"george boorman\"\n<\/pre>\n<\/li>\n<li>\n<p><strong>\u5217\u8868<\/strong>\uff1a\u5217\u8868\u662f\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u503c\u7684\u6709\u5e8f\u96c6\u5408\u3002<\/p>\n<pre># creating a list\nprices = [10, 20, 30, 15, 25, 35]\n\n# accessing the first item\nprint(prices[0])  # output: 10\n<\/pre>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178<\/strong>\uff1a\u5b57\u5178\u5b58\u50a8\u952e\u503c\u5bf9\uff0c\u5141\u8bb8\u60a8\u6839\u636e\u952e\u67e5\u627e\u503c\u3002<\/p>\n<pre># creating a dictionary\nproducts_dict = {\n    \"ag32\": 10,\n    \"ht91\": 20,\n    \"pl65\": 30,\n    \"os31\": 15,\n    \"kb07\": 25,\n    \"tr48\": 35\n}\n\n# accessing a value by key\nprint(products_dict[\"ag32\"])  # output: 10\n<\/pre>\n<\/li>\n<li>\n<p><strong>\u96c6\u5408\u548c\u5143\u7ec4<\/strong>:<\/p>\n<ul>\n<li> <strong>\u96c6\u5408<\/strong>\uff1a\u72ec\u7279\u5143\u7d20\u7684\u96c6\u5408\u3002<\/li>\n<li> <strong>tuple<\/strong>\uff1a\u4e0d\u53ef\u53d8\u5217\u8868\uff0c\u610f\u5473\u7740\u521b\u5efa\u540e\u65e0\u6cd5\u66f4\u6539\u3002 <\/li>\n<\/ul>\n<pre># creating a set\nprices_set = {10, 20, 30, 15, 25, 35}\n\n# creating a tuple\nprices_tuple = (10, 20, 30, 15, 25, 35)\n<\/pre>\n<\/li>\n<\/ol>\n<hr>\n<h4> \u7b2c 3 \u7ae0\uff1a\u6761\u4ef6\u5173\u952e\u5b57 <\/h4>\n<p>python \u5305\u542b\u51e0\u4e2a\u7528\u4e8e\u8bc4\u4f30\u6761\u4ef6\u7684\u5173\u952e\u5b57\uff0c\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u4e2d\u7684\u51b3\u7b56\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>keyword<\/th>\n<th>function<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>and<\/td>\n<td>evaluate if multiple conditions are true<\/td>\n<\/tr>\n<tr>\n<td>or<\/td>\n<td>evaluate if one or more conditions are true<\/td>\n<\/tr>\n<tr>\n<td>in<\/td>\n<td>check if a value exists in a data structure<\/td>\n<\/tr>\n<tr>\n<td>not<\/td>\n<td>evaluate if a value is not in a data structure<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e9b\u793a\u4f8b\u6765\u4e86\u89e3\u8fd9\u4e9b\u5173\u952e\u5b57\u7684\u5b9e\u9645\u4f5c\u7528\uff1a<\/p>\n<ol>\n<li> <strong>\u4f7f\u7528\u548c<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   age = 25\n   income = 50000\n\n   # check if both conditions are true\n   if age &gt; 20 and income &gt; 30000:\n       print(\"eligible for loan\")\n<\/pre>\n<ol>\n<li> <strong>\u4f7f\u7528\u6216<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   day = \"sunday\"\n   weather = \"sunny\"\n\n   # check if either condition is true\n   if day == \"saturday\" or weather == \"sunny\":\n       print(\"let's go to the park!\")\n<\/pre>\n<ol>\n<li> <strong>\u7528\u4e8e<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   fruits = [\"apple\", \"banana\", \"cherry\"]\n\n   # check if a value is in the list\n   if \"apple\" in fruits:\n       print(\"apple is available.\")\n<\/pre>\n<ol>\n<li> <strong>\u4e0d\u4f7f\u7528<\/strong>\uff1a <\/li>\n<\/ol>\n<pre>   vegetables = [\"carrot\", \"potato\", \"spinach\"]\n\n   # Check if a value is not in the list\n   if \"broccoli\" not in vegetables:\n       print(\"Broccoli is not in the list.\")\n<\/pre>\n<hr>\n<p>\u672c\u6982\u8ff0\u6db5\u76d6\u4e86 python \u4e2d\u7b97\u672f\u8fd0\u7b97\u3001\u5404\u79cd\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u5173\u952e\u5b57\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\u5c06\u5e2e\u52a9\u60a8\u6784\u5efa\u66f4\u590d\u6742\u7684\u7a0b\u5e8f\u3002 <\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython\uff1a\u7b97\u672f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761\u4ef6\u903b\u8f91\u7684\u57fa\u672c\u6982\u5ff5\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\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\uff1a\u7b97\u672f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6761&#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-204756","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204756","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=204756"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204756\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}