{"id":954265,"date":"2024-12-27T01:54:43","date_gmt":"2024-12-26T17:54:43","guid":{"rendered":""},"modified":"2024-12-27T01:54:45","modified_gmt":"2024-12-26T17:54:45","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e7%a8%8b%e6%89%93%e6%8a%98%e5%95%86%e5%93%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/954265.html","title":{"rendered":"Python\u5982\u4f55\u7f16\u7a0b\u6253\u6298\u5546\u54c1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094526\/e3ee3650-ad4e-46d0-a07f-08299bc9cebb.webp\" alt=\"Python\u5982\u4f55\u7f16\u7a0b\u6253\u6298\u5546\u54c1\" \/><\/p>\n<p><p> <strong>Python\u7f16\u7a0b\u7528\u4e8e\u8ba1\u7b97\u5546\u54c1\u6298\u6263\u3001\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u548c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u8ba1\u7b97\u548c\u7ba1\u7406\u6298\u6263\u5546\u54c1\u3002<\/strong>\u4f7f\u7528Python\u7f16\u7a0b\u6765\u5904\u7406\u6253\u6298\u5546\u54c1\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u5546\u54c1\u6216\u9891\u7e41\u53d8\u52a8\u7684\u6298\u6263\u65f6\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6765\u8ba1\u7b97\u6298\u6263\u540e\u7684\u4ef7\u683c\uff0c\u53ef\u4ee5\u8f7b\u677e\u590d\u7528\u4ee3\u7801\uff0c\u63d0\u9ad8\u6548\u7387\u3002\u6b64\u5916\uff0c\u5229\u7528\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\uff08\u5982\u4f1a\u5458\u7b49\u7ea7\u3001\u4fc3\u9500\u6d3b\u52a8\uff09\u5e94\u7528\u4e0d\u540c\u7684\u6298\u6263\u7b56\u7565\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6253\u6298\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u6298\u6263\u540e\u7684\u4ef7\u683c\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u4e3a\u7b80\u6d01\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u57fa\u7840\u6298\u6263\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u5546\u54c1\u539f\u4ef7\u548c\u6298\u6263\u7387\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8fd4\u56de\u6298\u6263\u540e\u7684\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_discounted_price(original_price, discount_rate):<\/p>\n<p>    discounted_price = original_price * (1 - discount_rate)<\/p>\n<p>    return discounted_price<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>original_price<\/code>\u662f\u5546\u54c1\u7684\u539f\u4ef7\uff0c\u800c<code>discount_rate<\/code>\u662f\u6298\u6263\u7387\uff08\u4f8b\u5982\uff0c20%\u7684\u6298\u6263\u7387\u5c06\u8f93\u5165\u4e3a0.2\uff09\u3002\u51fd\u6570\u8ba1\u7b97\u7684\u6838\u5fc3\u662f\u5c06\u539f\u4ef7\u4e58\u4ee5\u6298\u6263\u540e\u7684\u6bd4\u4f8b\uff0c\u5373<code>1 - discount_rate<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u8003\u8651\u8fb9\u754c\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u5230\u67d0\u4e9b\u8fb9\u754c\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u6298\u6263\u7387\u4e0d\u5e94\u8be5\u8d85\u8fc71\uff0c\u4e5f\u4e0d\u5e94\u8be5\u4e3a\u8d1f\u503c\u3002\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u6761\u4ef6\u68c0\u67e5\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u5408\u6cd5\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_discounted_price(original_price, discount_rate):<\/p>\n<p>    if discount_rate &lt; 0 or discount_rate &gt; 1:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Discount rate must be between 0 and 1.&quot;)<\/p>\n<p>    discounted_price = original_price * (1 - discount_rate)<\/p>\n<p>    return discounted_price<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6279\u91cf\u5904\u7406\u5546\u54c1<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u4e00\u7cfb\u5217\u5546\u54c1\u5e94\u7528\u6298\u6263\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6279\u91cf\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u4f7f\u7528\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u6765\u5b58\u50a8\u5546\u54c1\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u5e94\u7528\u6298\u6263\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">products = [<\/p>\n<p>    {&quot;name&quot;: &quot;Product 1&quot;, &quot;original_price&quot;: 100},<\/p>\n<p>    {&quot;name&quot;: &quot;Product 2&quot;, &quot;original_price&quot;: 200},<\/p>\n<p>    {&quot;name&quot;: &quot;Product 3&quot;, &quot;original_price&quot;: 150}<\/p>\n<p>]<\/p>\n<p>discount_rate = 0.2<\/p>\n<p>for product in products:<\/p>\n<p>    product[&quot;discounted_price&quot;] = calculate_discounted_price(product[&quot;original_price&quot;], discount_rate)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u901a\u8fc7\u5c06\u6298\u6263\u540e\u7684\u4ef7\u683c\u5b58\u50a8\u5728\u6bcf\u4e2a\u5546\u54c1\u7684\u5b57\u5178\u4e2d\uff0c\u8f7b\u677e\u5730\u7ba1\u7406\u4e86\u591a\u4e2a\u5546\u54c1\u7684\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u4f7f\u7528Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u3002Pandas\u5141\u8bb8\u66f4\u9ad8\u6548\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: [&quot;Product 1&quot;, &quot;Product 2&quot;, &quot;Product 3&quot;],<\/p>\n<p>    &quot;original_price&quot;: [100, 200, 150]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&quot;discounted_price&quot;] = df[&quot;original_price&quot;].apply(lambda x: calculate_discounted_price(x, discount_rate))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u7b80\u5316\u4e86\u6279\u91cf\u5904\u7406\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5e94\u7528\u6761\u4ef6\u6298\u6263<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6298\u6263\u53ef\u80fd\u4f1a\u57fa\u4e8e\u4e0d\u540c\u6761\u4ef6\u800c\u53d8\u5316\uff0c\u5982\u4f1a\u5458\u7b49\u7ea7\u6216\u7279\u6b8a\u4fc3\u9500\u6d3b\u52a8\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u57fa\u4e8e\u4f1a\u5458\u7b49\u7ea7\u7684\u6298\u6263<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u4f1a\u5458\u7b49\u7ea7\u5e94\u7528\u4e0d\u540c\u7684\u6298\u6263\u7387\u3002\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_discount_rate(member_level):<\/p>\n<p>    discounts = {<\/p>\n<p>        &quot;Silver&quot;: 0.1,<\/p>\n<p>        &quot;Gold&quot;: 0.2,<\/p>\n<p>        &quot;Platinum&quot;: 0.3<\/p>\n<p>    }<\/p>\n<p>    return discounts.get(member_level, 0)<\/p>\n<p>member_level = &quot;Gold&quot;<\/p>\n<p>discount_rate = get_discount_rate(member_level)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5b57\u5178\u5b58\u50a8\u6bcf\u4e2a\u4f1a\u5458\u7b49\u7ea7\u5bf9\u5e94\u7684\u6298\u6263\u7387\uff0c\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u83b7\u53d6\u76f8\u5e94\u7684\u6298\u6263\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u7279\u5b9a\u6d3b\u52a8\u7684\u6298\u6263<\/h4>\n<\/p>\n<p><p>\u5728\u7279\u5b9a\u7684\u8282\u5047\u65e5\u6216\u4fc3\u9500\u6d3b\u52a8\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u989d\u5916\u7684\u6298\u6263\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_discounted_price_with_event(original_price, discount_rate, event_discount=0):<\/p>\n<p>    total_discount_rate = discount_rate + event_discount<\/p>\n<p>    if total_discount_rate &gt; 1:<\/p>\n<p>        total_discount_rate = 1<\/p>\n<p>    discounted_price = original_price * (1 - total_discount_rate)<\/p>\n<p>    return discounted_price<\/p>\n<p>event_discount = 0.05  # \u989d\u5916\u76845%\u6298\u6263<\/p>\n<p>discounted_price = calculate_discounted_price_with_event(100, 0.2, event_discount)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>event_discount<\/code>\u662f\u989d\u5916\u7684\u6298\u6263\uff0c\u51fd\u6570\u901a\u8fc7\u8ba1\u7b97\u603b\u6298\u6263\u7387\u6765\u786e\u5b9a\u6700\u7ec8\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u81ea\u52a8\u5316\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7f16\u7a0b\u5b9e\u73b0\u81ea\u52a8\u5316\u5904\u7406\u6298\u6263\u5546\u54c1\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u81ea\u52a8\u5316\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u81ea\u52a8\u5316\u6267\u884c\u6298\u6263\u8ba1\u7b97\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u66f4\u65b0\u5546\u54c1\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def update_prices():<\/p>\n<p>    # \u66f4\u65b0\u5546\u54c1\u4ef7\u683c\u7684\u4ee3\u7801<\/p>\n<p>    print(&quot;Prices updated.&quot;)<\/p>\n<p>schedule.every().day.at(&quot;00:00&quot;).do(update_prices)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f7f\u7528<code>schedule<\/code>\u5e93\u6bcf\u5929\u66f4\u65b0\u4e00\u6b21\u5546\u54c1\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u4f18\u5316\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u63d0\u9ad8\u6027\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528NumPy<\/strong>\uff1a\u5bf9\u4e8e\u6570\u503c\u8ba1\u7b97\uff0cNumPy\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5e76\u884c\u5904\u7406<\/strong>\uff1a\u4f7f\u7528Python\u7684<code>multiprocessing<\/code>\u5e93\u6765\u5e76\u884c\u5904\u7406\u591a\u4e2a\u5546\u54c1\u3002<\/li>\n<li><strong>\u6570\u636e\u5e93\u5b58\u50a8<\/strong>\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u5b58\u50a8\u5546\u54c1\u4fe1\u606f\u548c\u6298\u6263\u8bb0\u5f55\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u8bfb\u53d6\u548c\u5199\u5165\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_prices = np.array([100, 200, 150])<\/p>\n<p>discounted_prices = original_prices * (1 - discount_rate)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u5e93\u6765\u5904\u7406\u5546\u54c1\u6298\u6263\u7684\u8ba1\u7b97\u548c\u7ba1\u7406\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u548cPandas DataFrame\uff09\u4ee5\u53ca\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u4e14\u9ad8\u6548\u7684\u6298\u6263\u8ba1\u7b97\u3002\u8fdb\u4e00\u6b65\u7684\u81ea\u52a8\u5316\u548c\u6027\u80fd\u4f18\u5316\u5c06\u5e2e\u52a9\u4f01\u4e1a\u66f4\u6709\u6548\u5730\u7ba1\u7406\u5176\u5546\u54c1\u5b9a\u4ef7\u7b56\u7565\u3002\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0c\u672a\u6765\u53ef\u80fd\u4f1a\u6709\u66f4\u591a\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u8fdb\u4e00\u6b65\u7b80\u5316\u548c\u589e\u5f3a\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u6298\u6263\u540e\u7684\u5546\u54c1\u4ef7\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u6298\u6263\u540e\u7684\u5546\u54c1\u4ef7\u683c\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u5c06\u539f\u4ef7\u4e58\u4ee5\u6298\u6263\u7387\uff0c\u7136\u540e\u4ece\u539f\u4ef7\u4e2d\u51cf\u53bb\u8fd9\u4e2a\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5546\u54c1\u7684\u539f\u4ef7\u662f100\u5143\uff0c\u6298\u6263\u7387\u662f20%\uff08\u53730.2\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n<pre><code class=\"language-python\">original_price = 100\ndiscount_rate = 0.2\ndiscounted_price = original_price * (1 - discount_rate)\nprint(discounted_price)  # \u8f93\u51fa\uff1a80.0\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u6253\u6298\u540e\u7684\u4ef7\u683c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u591a\u4e2a\u5546\u54c1\u7684\u6298\u6263\u8ba1\u7b97\uff1f<\/strong><br \/>\u5904\u7406\u591a\u4e2a\u5546\u54c1\u7684\u6298\u6263\u8ba1\u7b97\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b9e\u73b0\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5546\u54c1\u5217\u8868\uff0c\u6bcf\u4e2a\u5546\u54c1\u90fd\u6709\u539f\u4ef7\u548c\u6298\u6263\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e2a\u5546\u54c1\u5e76\u8ba1\u7b97\u6298\u6263\u4ef7\u683c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">products = [\n    {&quot;name&quot;: &quot;\u5546\u54c1A&quot;, &quot;price&quot;: 100, &quot;discount&quot;: 0.2},\n    {&quot;name&quot;: &quot;\u5546\u54c1B&quot;, &quot;price&quot;: 200, &quot;discount&quot;: 0.15},\n]\n\nfor product in products:\n    discounted_price = product[&quot;price&quot;] * (1 - product[&quot;discount&quot;])\n    print(f&quot;{product[&#39;name&#39;]} \u6253\u6298\u540e\u7684\u4ef7\u683c\u662f: {discounted_price}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u8ba1\u7b97\u591a\u4e2a\u5546\u54c1\u7684\u6298\u6263\u53d8\u5f97\u65b9\u4fbf\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6253\u6298\u5546\u54c1\u7684\u8ba1\u7b97\u5668\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u6253\u6298\u5546\u54c1\u7684\u8ba1\u7b97\u5668\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u5546\u54c1\u7684\u539f\u4ef7\u548c\u6298\u6263\u7387\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u6298\u6263\u540e\u7684\u4ef7\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate_discounted_price(original_price, discount_rate):\n    return original_price * (1 - discount_rate)\n\n# \u793a\u4f8b\u8c03\u7528\nprice = 150\ndiscount = 0.25\nfinal_price = calculate_discounted_price(price, discount)\nprint(f&quot;\u6253\u6298\u540e\u7684\u4ef7\u683c\u662f: {final_price}&quot;)  # \u8f93\u51fa\uff1a\u6253\u6298\u540e\u7684\u4ef7\u683c\u662f: 112.5\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u91cd\u7528\u6027\uff0c\u8fd8\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u591a\u6b21\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7f16\u7a0b\u7528\u4e8e\u8ba1\u7b97\u5546\u54c1\u6298\u6263\u3001\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u548c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u8ba1\u7b97\u548c\u7ba1\u7406\u6298\u6263\u5546\u54c1\u3002\u4f7f\u7528Pytho [&hellip;]","protected":false},"author":3,"featured_media":954275,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954265"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=954265"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954265\/revisions"}],"predecessor-version":[{"id":954277,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954265\/revisions\/954277"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/954275"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=954265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=954265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=954265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}