{"id":509,"date":"2025-01-16T08:39:12","date_gmt":"2025-01-16T00:39:12","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=509"},"modified":"2025-01-16T08:39:12","modified_gmt":"2025-01-16T00:39:12","slug":"python-%e5%87%bd%e6%95%b0%e5%ae%9e%e4%be%8b%ef%bc%9afor-%e5%be%aa%e7%8e%af%e5%ae%9a%e4%b9%89%e5%8f%96%e5%80%bc%e8%8c%83%e5%9b%b4%e6%89%93%e5%8d%b0%e6%af%8f%e5%a4%a9%ef%bc%8c%e4%b8%8e%e7%9b%b4%e6%8e%a5","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=509","title":{"rendered":"Python \u51fd\u6570\u5b9e\u4f8b\uff1afor \u5faa\u73af\u5b9a\u4e49\u53d6\u503c\u8303\u56f4\u6253\u5370\u6bcf\u5929\uff0c\u4e0e\u76f4\u63a5\u5b9a\u4e49\u6240\u9700\u5929\u6570\u6253\u5370\u6700\u540e\u4e00\u5929\u7ed3\u679c"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>\u6253\u5370\u6bcf\u4e00\u5929 \uff08for \u5b9a\u4e49\u53d6\u503c\u8303\u56f4\uff09 \u76f4\u63a5\u5199 \uff081\uff09\uff1a<\/strong>  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>initial_amount = 100000\ngrowth_rate = 1.1\n\nfor day in range(7) :\n    initial_amount *= growth_rate\n\n    print(f\"\u7b2c{day+1}\u5929\u7684\u91d1\u989d\u662f\uff1a{initial_amount}\")\n\n# \u4e3a\u4ec0\u4e48\u7528{day+1}\n\n  \u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\uff0c`range(7)` \u751f\u6210\u7684\u5e8f\u5217\u662f `0, 1, 2, 3, 4, 5, 6` \u3002\n\n\u5f53\u60a8\u4f7f\u7528 `f-string` \u6253\u5370 `day` \u65f6\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u663e\u793a\u7684\u662f\u4ece\u7b2c 1 \u5929\u5f00\u59cb\u5230\u7b2c 7 \u5929\uff0c\u90a3\u4e48\u4f7f\u7528 `{day + 1}` \u4f1a\u66f4\u7b26\u5408\u76f4\u89c2\u7684\u7406\u89e3\uff0c\u5373\u663e\u793a\u4e3a\u201c\u7b2c 1 \u5929\u201d\u201c\u7b2c 2 \u5929\u201d\u7b49\u3002\n\n\u4f46\u5982\u679c\u60a8\u76f4\u63a5\u4f7f\u7528 `{day}` \uff0c\u90a3\u4e48\u663e\u793a\u7684\u5c31\u662f\u201c\u7b2c 0 \u5929\u201d\u201c\u7b2c 1 \u5929\u201d\u7b49\u3002\n\n\u4e24\u79cd\u65b9\u5f0f\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\u4ee3\u7801\uff0c\u53ea\u662f\u663e\u793a\u7684\u7ed3\u679c\u5728\u5929\u6570\u7684\u8868\u8ff0\u4e0a\u6709\u6240\u4e0d\u540c\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u53d6\u51b3\u4e8e\u60a8\u5e0c\u671b\u5448\u73b0\u7684\u6548\u679c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6ca1\u6709\u4f7f\u7528 `{day + 1}` \u53ea\u662f\u9009\u62e9\u4e86\u76f4\u63a5\u4ece 0 \u5f00\u59cb\u8ba1\u6570\u5e76\u5c55\u793a\u5929\u6570\u3002\n\n<strong>\u6253\u5370\u6bcf\u4e00\u5929 for\uff08for \u5b9a\u4e49\u53d6\u503c\u8303\u56f4\uff09 \u533f\u540d\u51fd\u6570\u5199 \uff082\uff09\uff1a\n<\/strong>\n\ninitial_amount = 100000\ngrowth_rate = 1.1\n\ngrowth_func = lambda x , rate , days : x * (rate ** days)  #\u5b9a\u4e49\u51fd\u6570\u516c\u5f0f\uff0c x rate days \u7b49\u5f85\u8d4b\u503c\n                     #\u5207\u8bb0lambda \u540e\u4e0d\u80fd\u7528 ()\nfor day in range(7) :   #\u5b9a\u4e49day\u53d6\u503c\u8303\u56f4\n\n    amount = growth_func(initial_amount,growth_rate,day + 1)  #day + 1 \u8868\u793a\u4ece\u7b2c\u4e00\u5929\u5f00\u59cb\u8ba1\u7b97\n    print(f'\u7b2c{day +1}\u5929\u7684\u91d1\u989d\u662f\uff1a{amount}')<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6253\u5370\u5b9a\u4e49\u5929\u6570\u7ed3\u679c \uff08\u76f4\u63a5def \u5b9a\u4e49\u53d6\u503c\u8303\u56f4\uff09 \u76f4\u63a5\u5199 \uff081\uff09\uff1a  <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>initial_amount = 100000\ngrowth_rate = 1.1\n\ndef df (initial, rate, day):          #\u5b9a\u4e49\u516c\u5f0f\u76843\u4e2a\u51fd\u6570\u540d\u79f0  \u6b64\u5904 \uff1a\u82f1\u6587\u5192\u53f7\u4e0d\u8981\u6f0f\n    return initial * (rate ** day)      #\u8fd4\u56de\u8fd9\u4e2a\u516c\u5f0f\u7684\u503c\n\namount = df(initial_amount , growth_rate , 7)  #\u5206\u522b\u8d4b\u503c\u7ed9 def\u5b9a\u4e49\u7684\u51fd\u6570\u540d\u79f0\n\nprint(f'\u7b2c7\u5929\u7684\u91d1\u989d\u662f\uff1a{amount}')\n\n<\/code><\/pre>\n\n\n\n<p><strong>\u6253\u5370<strong>\u5b9a\u4e49\u5929\u6570<\/strong>\u7ed3\u679c \uff08\u76f4\u63a5\u5b9a\u4e49\u5929\u6570\uff0c\u533f\u540d\u51fd\u6570lambda\uff09  \uff082\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>initial_amount = 100000\ngrowth_rate = 1.1\n\ngrowth_func = lambda x , rate , days : x * (rate ** days)  #\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u516c\u5f0f\n\namount = growth_func(initial_amount , growth_rate , 7)  #\u8d4b\u503c\u7ed9lambda \u533f\u540d\u51fd\u6570\u76843\u4e2a\u503c\n\nprint(f'\u7b2c7\u5929\u7684\u91d1\u989d\u662f\uff1a{amount}')\n\n<\/code><\/pre>\n\n\n\n<p>\u603b\u7ed3  \uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5bb9\u6613\u6f0f\u5199\uff0c\u9519\u5199<\/p>\n\n\n\n<p>def \u5b9a\u4e49\u51fd\u6570\u540d\u79f0\u8981\u52a0\uff08\uff09\uff0c\u4e14\u540e\u9762\u8981\u6dfb\u52a0\u82f1\u6587\u5192\u53f7<br>\u7ed3\u679c\u7528return \u5199\u516c\u5f0f\u8fd4\u56de\u503c<\/p>\n\n\n\n<p>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def df (initial, rate, day): #\u5b9a\u4e49\u516c\u5f0f\u76843\u4e2a\u51fd\u6570\u540d\u79f0 \u6b64\u5904 \uff1a\u82f1\u6587\u5192\u53f7\u4e0d\u8981\u6f0f\nreturn initial * (rate ** day) #\u8fd4\u56de\u8fd9\u4e2a\u516c\u5f0f\u7684\u503c<\/code><\/pre>\n\n\n\n<p><br><br>lambda \u533f\u540d\u51fd\u6570 \uff0c\u5b9a\u4e49\u51fd\u6570\u540d\u79f0\u4e0d\u8981\u52a0\uff08\uff09\uff0c\u76f4\u63a5\u5728\u51fd\u6570\u540d\u79f0\u540e\uff0c\u5728 \uff1a\uff08\u82f1\u6587\u5192\u53f7\uff09\u540e\u5199\u516c\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>growth_func = lambda x , rate , days : x * (rate ** days) <\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4f8b \uff1a<\/p>\n\n\n\n<p>\u4e0d\u7ba1\u7528def \u8fd8\u662f lambda  \u90fd\u8981\u7ed9\u51fd\u6570\u540d\u79f0\u8d4b\u503c\uff1a<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>amount = df(initial_amount , growth_rate , 7)  #\u5206\u522b\u8d4b\u503c\u7ed9 def\u5b9a\u4e49\u7684\u51fd\u6570\u540d\u79f0<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5207\u8bb0\u5728 Python \u4e2d\uff0c<code>f'<\/code>&nbsp;\u6216&nbsp;<code>f\" \"<\/code>&nbsp;\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u524d\u7f00\uff0c\u79f0\u4e3a&nbsp;<code>f-string<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u5b83\u5141\u8bb8\u60a8\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u4f7f\u7528\u53d8\u91cf\u7684\u503c\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6253\u5370\u6bcf\u4e00\u5929 \uff08for \u5b9a\u4e49\u53d6\u503c\u8303\u56f4\uff09 \u76f4\u63a5\u5199 \uff081\uff09\uff1a \u6253\u5370\u5b9a\u4e49\u5929\u6570\u7ed3\u679c \uff08\u76f4\u63a5def \u5b9a\u4e49\u53d6\u503c\u8303\u56f4\uff09 \u76f4\u63a5\u5199 \uff081\uff09\uff1a \u6253\u5370\u5b9a\u4e49\u5929\u6570\u7ed3\u679c \uff08\u76f4\u63a5\u5b9a\u4e49\u5929\u6570\uff0c\u533f\u540d\u51fd\u6570lambda\uff09 \uff082\uff09\uff1a \u603b\u7ed3 \uff1a \u4ee5\u4e0b\u662f\u5bb9\u6613\u6f0f\u5199\uff0c\u9519\u5199 def \u5b9a\u4e49\u51fd\u6570\u540d\u79f0\u8981\u52a0\uff08\uff09\uff0c\u4e14\u540e\u9762\u8981\u6dfb\u52a0\u82f1\u6587\u5192\u53f7\u7ed3\u679c\u7528return \u5199\u516c\u5f0f\u8fd4\u56de\u503c \u4f8b\uff1a lambda \u533f\u540d\u51fd\u6570 \uff0c\u5b9a\u4e49\u51fd\u6570\u540d\u79f0\u4e0d\u8981\u52a0\uff08\uff09\uff0c\u76f4\u63a5\u5728\u51fd\u6570\u540d\u79f0\u540e\uff0c\u5728 \uff1a\uff08\u82f1\u6587\u5192\u53f7\uff09\u540e\u5199\u516c\u5f0f \u4f8b \uff1a \u4e0d\u7ba1\u7528def \u8fd8\u662f lambda \u90fd\u8981\u7ed9\u51fd\u6570\u540d\u79f0\u8d4b\u503c\uff1a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,14],"tags":[],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-python","category-14"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=509"}],"version-history":[{"count":0,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}