{"id":958102,"date":"2024-12-27T03:15:20","date_gmt":"2024-12-26T19:15:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958102.html"},"modified":"2024-12-27T03:15:22","modified_gmt":"2024-12-26T19:15:22","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%bapi","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958102.html","title":{"rendered":"Python\u5982\u4f55\u6253\u5370\u51fapi"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101233\/97eb72b6-3ba4-4575-ba45-2d6eabd7cf6d.webp\" alt=\"Python\u5982\u4f55\u6253\u5370\u51fapi\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5370\u51fapi\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684math\u5e93\u3001numpy\u5e93\u3001sympy\u5e93\uff0c\u6216\u8005\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba1\u7b97\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528Python\u7684math\u5e93\uff0c\u56e0\u4e3a\u5b83\u5185\u7f6e\u4e86\u5f88\u591a\u6570\u5b66\u5e38\u91cf\u548c\u51fd\u6570\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528MATH\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684math\u5e93\u63d0\u4f9b\u4e86pi\u5e38\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6765\u6253\u5370pi\u7684\u503c\u3002math\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u65e0\u9700\u5b89\u88c5\uff0c\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.pi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>math\u5e93\u7684pi\u5e38\u91cf<\/strong>\uff1a\u901a\u8fc7\u5bfc\u5165math\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95eepi\u5e38\u91cf\uff0c\u8fd9\u4e2a\u5e38\u91cf\u63d0\u4f9b\u4e86\u975e\u5e38\u7cbe\u786e\u7684pi\u503c\uff08\u5c0f\u6570\u70b9\u540e15\u4f4d\u7684\u7cbe\u5ea6\uff09\u3002<\/li>\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u83b7\u53d6pi\u503c\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97\u65f6\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528NUMPY\u5e93<\/p>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e5f\u63d0\u4f9b\u4e86pi\u5e38\u91cf\u3002\u4e0emath\u5e93\u4e0d\u540c\uff0cnumpy\u66f4\u52a0\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.pi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>numpy\u5e93\u7684pi\u5e38\u91cf<\/strong>\uff1a\u7c7b\u4f3c\u4e8emath\u5e93\uff0cnumpy\u4e5f\u63d0\u4f9b\u4e86pi\u5e38\u91cf\uff0c\u5177\u6709\u540c\u6837\u7684\u7cbe\u5ea6\u3002<\/li>\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u5408\u9700\u8981\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u4e2d\u4f7f\u7528pi\u503c\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528SYMPY\u5e93<\/p>\n<\/p>\n<p><p>sympy\u662f\u4e00\u4e2a\u7b26\u53f7\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b26\u53f7\u5316\u7684\u6570\u5b66\u8fd0\u7b97\u548c\u66f4\u9ad8\u7cbe\u5ea6\u7684pi\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import pi<\/p>\n<p>print(pi.evalf())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>sympy\u5e93\u7684pi\u5e38\u91cf<\/strong>\uff1a\u63d0\u4f9b\u7b26\u53f7\u5316\u7684pi\u503c\uff0c\u4f7f\u7528evalf()\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97\u6570\u503c\u5f62\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u7cbe\u5ea6\u3002<\/li>\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7b26\u53f7\u5316\u6570\u5b66\u64cd\u4f5c\u548c\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u6bd4\u5982\u9700\u8981\u975e\u5e38\u9ad8\u7684\u7cbe\u5ea6\u6216\u8fdb\u884c\u7b26\u53f7\u5316\u6c42\u89e3\u65f6\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba1\u7b97PI<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5e93\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u65b9\u6cd5\u8fd1\u4f3c\u8ba1\u7b97pi\u503c\uff0c\u6bd4\u5982\u4f7f\u7528\u83b1\u5e03\u5c3c\u8328\u7ea7\u6570\u3001\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b1\u5e03\u5c3c\u8328\u7ea7\u6570<\/strong>\uff1a<\/p>\n<p>\u83b1\u5e03\u5c3c\u8328\u7ea7\u6570\u662f\u4e00\u79cd\u7ecf\u5178\u7684pi\u8ba1\u7b97\u65b9\u6cd5\uff0c\u901a\u8fc7\u65e0\u7a77\u7ea7\u6570\u6765\u903c\u8fd1pi\u503c\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">def leibniz_pi(n_terms):<\/p>\n<p>    pi_estimate = 0<\/p>\n<p>    for k in range(n_terms):<\/p>\n<p>        pi_estimate += (-1)k \/ (2*k + 1)<\/p>\n<p>    return pi_estimate * 4<\/p>\n<p>print(leibniz_pi(1000000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8499\u7279\u5361\u6d1b\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<p>\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u901a\u8fc7\u6a21\u62df\u968f\u673a\u70b9\u6765\u4f30\u8ba1pi\u503c\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def monte_carlo_pi(n_points):<\/p>\n<p>    inside_circle = 0<\/p>\n<p>    for _ in range(n_points):<\/p>\n<p>        x, y = random.random(), random.random()<\/p>\n<p>        if x&lt;strong&gt;2 + y&lt;\/strong&gt;2 &lt;= 1:<\/p>\n<p>            inside_circle += 1<\/p>\n<p>    return (inside_circle \/ n_points) * 4<\/p>\n<p>print(monte_carlo_pi(1000000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u83b1\u5e03\u5c3c\u8328\u7ea7\u6570<\/strong>\uff1a\u901a\u8fc7\u65e0\u7a77\u7ea7\u6570\u7684\u548c\u6765\u903c\u8fd1pi\uff0c\u8ba1\u7b97\u7b80\u5355\uff0c\u4f46\u6536\u655b\u901f\u5ea6\u8f83\u6162\u3002<\/li>\n<li><strong>\u8499\u7279\u5361\u6d1b\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7\u6982\u7387\u7edf\u8ba1\u7684\u65b9\u6cd5\u6765\u4f30\u8ba1pi\u503c\uff0c\u9002\u5408\u5e76\u884c\u8ba1\u7b97\u548c\u5927\u89c4\u6a21\u6a21\u62df\u3002<\/li>\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u6559\u5b66\u3001\u7b97\u6cd5\u7814\u7a76\uff0c\u6216\u8005\u5728\u6ca1\u6709\u5e93\u652f\u6301\u7684\u73af\u5883\u4e2d\u624b\u52a8\u8ba1\u7b97pi\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u5176\u4ed6\u83b7\u53d6PI\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5728\u7ebf\u670d\u52a1\u6216API\u83b7\u53d6pi\u503c\uff0c\u9002\u5408\u6ca1\u6709\u8ba1\u7b97\u80fd\u529b\u6216\u9700\u8981\u8d85\u9ad8\u7cbe\u5ea6\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728\u7ebfAPI<\/strong>\uff1a<\/p>\n<p>\u4e00\u4e9b\u5728\u7ebfAPI\u53ef\u4ee5\u63d0\u4f9b\u9ad8\u7cbe\u5ea6\u7684pi\u503c\uff0c\u9002\u5408\u9700\u8981\u7f51\u7edc\u652f\u6301\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5916\u90e8\u8ba1\u7b97\u8f6f\u4ef6<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528\u5916\u90e8\u6570\u5b66\u8f6f\u4ef6\u5982Mathematica\u3001Maple\u7b49\uff0c\u53ef\u4ee5\u83b7\u53d6\u6781\u9ad8\u7cbe\u5ea6\u7684pi\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u6253\u5370pi\u503c\u7684\u591a\u79cd\u65b9\u6cd5\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u5177\u4f53\u9700\u6c42\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5185\u7f6e\u7684math\u5e93\u6216numpy\u5e93\u5df2\u7ecf\u8db3\u591f\uff0c\u4f46\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u6216\u7b26\u53f7\u8ba1\u7b97\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9sympy\u5e93\u6216\u81ea\u5b9a\u4e49\u7b97\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684pi\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u03c0\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>math.pi<\/code>\u6765\u83b7\u53d6\u03c0\u7684\u503c\u3002\u53ea\u9700\u5bfc\u5165<code>math<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>math.pi<\/code>\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import math\nprint(math.pi)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u03c0\u7684\u503c\uff0c\u7ea6\u4e3a3.141592653589793\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528Python\u8ba1\u7b97\u03c0\u7684\u8fd1\u4f3c\u503c\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u4e2d\u8ba1\u7b97\u03c0\u7684\u8fd1\u4f3c\u503c\uff0c\u4f8b\u5982\u4f7f\u7528\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f\u6216\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate_pi(n_terms):\n    pi_estimate = sum(((-1)**k) \/ (2*k + 1) for k in range(n_terms)) * 4\n    return pi_estimate\n\nprint(calculate_pi(100000))\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8c03\u6574<code>n_terms<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u7684\u7cbe\u786e\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u03c0\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u4ee5\u7279\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u6253\u5370\u03c0\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u03c0\u683c\u5f0f\u5316\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">print(f&quot;{math.pi:.2f}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u201c3.14\u201d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5370\u51fapi\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684math\u5e93\u3001numpy\u5e93\u3001sympy\u5e93\uff0c\u6216\u8005\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba1\u7b97\u3002 [&hellip;]","protected":false},"author":3,"featured_media":958104,"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\/958102"}],"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=958102"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958102\/revisions"}],"predecessor-version":[{"id":958105,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958102\/revisions\/958105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958104"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}