{"id":205265,"date":"2025-05-29T16:17:50","date_gmt":"2025-05-29T08:17:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205265\/"},"modified":"2025-05-29T16:17:50","modified_gmt":"2025-05-29T08:17:50","slug":"python-%e4%b8%ad%e7%9a%84%e9%a2%84%e5%ae%9a%e4%b9%89%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205265\/","title":{"rendered":"python \u4e2d\u7684\u9884\u5b9a\u4e49\u6a21\u5757"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>python \u4e2d\u7684\u9884\u5b9a\u4e49\u6a21\u5757<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300apython \u4e2d\u7684\u9884\u5b9a\u4e49\u6a21\u5757\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17327046926746f9b442987.jpg\" class=\"aligncenter\"><\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a bank \u7684 python \u6a21\u5757\u3002 <\/li>\n<li>\u6dfb\u52a0\u529f\u80fd\uff1a \u5b58\u6b3e\uff08\u91d1\u989d\uff09 \u63d0\u6b3e(\u91d1\u989d)<\/li>\n<li>\u518d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a customer \u7684 python \u6a21\u5757<\/li>\n<li>\u4ece\u5ba2\u6237\u6a21\u5757\u8c03\u7528\u94f6\u884c\u6a21\u5757\u7684\u5b58\u53d6\u6b3e\u529f\u80fd\u3002 <\/li>\n<\/ol>\n<pre>#bank.py\ndef deposit(amount):\n    print(\"enter the deposit amount:\",amount)\ndef withdraw(amount):\n    print(\"enter the withdraw amount:\",amount)\n<\/pre>\n<p>\u521b\u5efapython\u6a21\u5757\u6587\u4ef6\u540d\u4e3abank.py<br \/> \u8fd9\u4e24\u4e2a\u51fd\u6570\u662f<br \/> deposit(amount)\uff1a\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 amount \u5e76\u6253\u5370\u6307\u793a\u5b58\u6b3e\u91d1\u989d\u7684\u6d88\u606f\u3002<br \/> withdraw(amount)\uff1a\u8be5\u51fd\u6570\u4e5f\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570amount\uff0c\u5e76\u6253\u5370\u6307\u793a\u63d0\u6b3e\u91d1\u989d\u7684\u6d88\u606f\u3002<\/p>\n<pre>#customer.py\nimport bank\nbank.deposit(1000)\nbank.withdraw(500)\n\n<\/pre>\n<p>\u521b\u5efa\u53e6\u4e00\u4e2apython\u6a21\u5757\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u4e3acustomer.py <\/p>\n<p>\u4f7f\u7528\u6b64\u5bfc\u5165\u5173\u952e\u5b57\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165\u94f6\u884c\u6a21\u5757\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4ece customer.py<br \/> \u8bbf\u95ee deposit() \u548c deposit() \u51fd\u6570 <\/p>\n<pre>enter the deposit amount: 1000\nenter the withdraw amount: 500\n\n\n<\/pre>\n<p><strong>python \u9884\u5b9a\u4e49\u6a21\u5757\uff1a<\/strong><\/p>\n<p><strong>1.\u968f\u673a\uff1a<\/strong><\/p>\n<p>\u968f\u673a\u6a21\u5757\u5141\u8bb8\u60a8\u751f\u6210\u968f\u673a\u6570\u3001\u6253\u4e71\u6570\u636e\u4ee5\u53ca\u4ece\u5e8f\u5217\u4e2d\u9009\u62e9\u968f\u673a\u5143\u7d20\u3002<\/p>\n<pre>import random\notp = random.randint(100000,999999)\nprint(otp)\n\n<\/pre>\n<p>random.randint(a, b) \u8fd4\u56de a \u548c b\uff08\u542b\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<pre>624367\n<\/pre>\n<p><strong>2.\u6570\u5b66\uff1a<\/strong><\/p>\n<p>\u6570\u5b66\u6a21\u5757\u63d0\u4f9b\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u548c\u5e38\u91cf\u7684\u51fd\u6570\u3002<\/p>\n<pre>import math\nprint(math.fabs(-5))\n\n<\/pre>\n<p>math.fabs(x)\uff1a\u8fd4\u56dex\u7684\u7edd\u5bf9\u503c\u3002<\/p>\n<pre>5.0\n<\/pre>\n<p><strong>3.os:<\/strong><\/p>\n<p>\u63d0\u4f9b\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff08\u4f8b\u5982\u6587\u4ef6\u5904\u7406\u3001\u76ee\u5f55\uff09\u3002<\/p>\n<pre>import os\nprint(os.getcwd())  \n<\/pre>\n<p>\u5b83\u5c06\u663e\u793a\u5f53\u524db\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<pre>\/home\/prigo\/desktop\n<\/pre>\n<p><strong>4.sys<\/strong><\/p>\n<p>\u63d0\u4f9b\u5bf9\u7cfb\u7edf\u7279\u5b9a\u53c2\u6570\u548c\u51fd\u6570\u7684\u8bbf\u95ee\uff0c\u4f8b\u5982\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002<\/p>\n<pre>import sys\nprint(sys.argv)  \n<\/pre>\n<p>\u5b83\u5c06\u663e\u793a\u6587\u4ef6\u540d\u3002<\/p>\n<pre>['one.py']\n<\/pre>\n<p><strong>5.\u65e5\u671f\u65f6\u95f4<\/strong><\/p>\n<p>\u7528\u4e8e\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<pre>import datetime\nnow = datetime.datetime.now()\nprint(now) \n\n<\/pre>\n<p>\u5b83\u5c06\u663e\u793a\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<pre>2024-11-22 00:59:19.436950\n\n<\/pre>\n<p><strong>6.time<\/strong><\/p>\n<p>\u63d0\u4f9b\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5305\u62ec\u65f6\u95f4\u6d4b\u91cf\u548c\u6267\u884c\u6682\u505c\u3002<\/p>\n<pre>import time\ntime.sleep(2)  # sleep for 2 seconds\n<\/pre>\n<p><strong>7.csv<\/strong><br \/> \u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165 csv \u6587\u4ef6\u3002<\/p>\n<pre>import csv\nwith open('data.csv', 'w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow(['name', 'age'])\n    writer.writerow(['alice', 25])\n\n<\/pre>\n<p><strong>8.numpy<\/strong><br \/> \u7528\u4e8e\u6570\u7ec4\u548c\u77e9\u9635\u6570\u503c\u8fd0\u7b97\u7684\u5f3a\u5927\u5e93\u3002<\/p>\n<pre>import numpy as np\narr = np.array([1, 2, 3, 4])\nprint(np.mean(arr))  # mean of the array\n\n<\/pre>\n<p><strong>9.pandas<\/strong><br \/> \u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\uff0c\u7279\u522b\u662f\u8868\u7b49\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<pre>import pandas as pd\ndf = pd.dataframe({'name': ['alice', 'bob'], 'age': [25, 30]})\nprint(df)\n<\/pre>\n<p><strong>10.matplotlib<\/strong><br \/> \u4e00\u4e2a\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u4ea4\u4e92\u5f0f\u548c\u52a8\u753b\u53ef\u89c6\u5316\u3002<\/p>\n<pre>import matplotlib.pyplot as plt\nplt.plot([1, 2, 3], [4, 5, 6])\nplt.show()\n\n<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300apython \u4e2d\u7684\u9884\u5b9a\u4e49\u6a21\u5757\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 \u4e2d\u7684\u9884\u5b9a\u4e49\u6a21\u5757 \u77e5&#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-205265","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205265","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=205265"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205265\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}