{"id":1039688,"date":"2024-12-31T12:34:38","date_gmt":"2024-12-31T04:34:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039688.html"},"modified":"2024-12-31T12:34:40","modified_gmt":"2024-12-31T04:34:40","slug":"python%e4%b8%ad%e7%9a%84%e5%9c%86%e5%91%a8%e7%8e%87%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039688.html","title":{"rendered":"python\u4e2d\u7684\u5706\u5468\u7387\u5982\u4f55\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/411dd606-d76b-484a-954f-d844dab925ae.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u7684\u5706\u5468\u7387\u5982\u4f55\u5b9a\u4e49\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u5706\u5468\u7387\u5982\u4f55\u5b9a\u4e49<\/strong>\uff1a<br \/>\u5728Python\u4e2d\uff0c<strong>\u5706\u5468\u7387\uff08\u03c0\uff09\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165math\u6a21\u5757\u4e2d\u7684pi\u5e38\u91cf\u6765\u5b9a\u4e49\u3001\u4f7f\u7528numpy\u5e93\u4e2d\u7684pi\u5e38\u91cf\u3001\u624b\u52a8\u5b9a\u4e49\u4e00\u4e2a\u8fd1\u4f3c\u503c<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u5706\u5468\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528math\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3amath\u7684\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u5706\u5468\u7387\u03c0\u3002\u5bfc\u5165math\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528pi\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>pi = math.pi<\/p>\n<p>print(pi)  # \u8f93\u51fa\uff1a3.141592653589793<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>math\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002math.pi\u5e38\u91cf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u5706\u5468\u7387\u503c\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u5b66\u8ba1\u7b97\u548c\u5e94\u7528\u3002\u7531\u4e8emath\u6a21\u5757\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5176\u7cbe\u5ea6\u548c\u53ef\u9760\u6027\u975e\u5e38\u9ad8\uff0c\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u5706\u5468\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86math\u6a21\u5757\uff0cnumpy\u5e93\u4e5f\u63d0\u4f9b\u4e86pi\u5e38\u91cf\u3002numpy\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>pi = np.pi<\/p>\n<p>print(pi)  # \u8f93\u51fa\uff1a3.141592653589793<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>numpy\u5e93\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u5706\u5468\u7387\u03c0\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6570\u5b66\u5e38\u91cf\u548c\u51fd\u6570\u3002\u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u6570\u503c\u8ba1\u7b97\u3002numpy\u7684pi\u5e38\u91cf\u4e0emath\u6a21\u5757\u4e2d\u7684pi\u5e38\u91cf\u5728\u7cbe\u5ea6\u4e0a\u662f\u76f8\u540c\u7684\uff0c\u4f46numpy\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u5df2\u7ecf\u4f7f\u7528\u4e86numpy\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528numpy.pi\u6765\u83b7\u53d6\u5706\u5468\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u5b9a\u4e49\u4e00\u4e2a\u8fd1\u4f3c\u503c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u5bfc\u5165\u4efb\u4f55\u5e93\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u5b9a\u4e49\u4e00\u4e2a\u5706\u5468\u7387\u7684\u8fd1\u4f3c\u503c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u5e38\u91cf\u7cbe\u786e\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u5e94\u7528\u4e2d\u4e5f\u8db3\u591f\u4e86\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pi = 3.141592653589793<\/p>\n<p>print(pi)  # \u8f93\u51fa\uff1a3.141592653589793<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u624b\u52a8\u5b9a\u4e49\u5706\u5468\u7387\u7684\u597d\u5904\u5728\u4e8e\u4e0d\u9700\u8981\u4f9d\u8d56\u4efb\u4f55\u5916\u90e8\u5e93\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u4f60\u5b9a\u4e49\u7684\u03c0\u503c\u7684\u4f4d\u6570\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u6765\u8bf4\uff0c\u4fdd\u7559\u5230\u5c0f\u6570\u70b9\u540e15\u4f4d\u5df2\u7ecf\u8db3\u591f\u4e86\uff0c\u4f46\u5728\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528math\u6a21\u5757\u6216numpy\u5e93\u4e2d\u7684pi\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8ba1\u7b97\u5706\u5468\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5df1\u8ba1\u7b97\u5706\u5468\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7ecf\u5178\u7684\u8ba1\u7b97\u03c0\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f\u548c\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f<\/h4>\n<\/p>\n<p><p>\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f\u662f\u4e00\u79cd\u8ba1\u7b97\u5706\u5468\u7387\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u4f46\u6536\u655b\u901f\u5ea6\u8f83\u6162\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def leibniz_pi(n_terms):<\/p>\n<p>    pi = 0<\/p>\n<p>    for i in range(n_terms):<\/p>\n<p>        pi += ((-1)  i) \/ (2 * i + 1)<\/p>\n<p>    return pi * 4<\/p>\n<p>pi = leibniz_pi(1000000)<\/p>\n<p>print(pi)  # \u8f93\u51fa\uff1a3.1415916535897743<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u83b1\u5e03\u5c3c\u8328\u516c\u5f0f\u901a\u8fc7\u9010\u9879\u7d2f\u52a0\u6765\u903c\u8fd1\u5706\u5468\u7387\uff0c\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u03c0 = 4 * (1 &#8211; 1\/3 + 1\/5 &#8211; 1\/7 + 1\/9 &#8211; &#8230;)<\/p>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u4f46\u9700\u8981\u5927\u91cf\u7684\u8ba1\u7b97\u624d\u80fd\u83b7\u5f97\u8f83\u9ad8\u7684\u7cbe\u5ea6\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u6559\u5b66\u6216\u6f14\u793a\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8499\u7279\u5361\u7f57\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u968f\u673a\u6570\u7684\u5706\u5468\u7387\u8ba1\u7b97\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7406\u89e3\u6982\u7387\u548c\u968f\u673a\u6027\u3002<\/p>\n<\/p>\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>pi = monte_carlo_pi(1000000)<\/p>\n<p>print(pi)  # \u8f93\u51fa\uff1a3.141728<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u901a\u8fc7\u5728\u5355\u4f4d\u6b63\u65b9\u5f62\u5185\u968f\u673a\u751f\u6210\u70b9\uff0c\u8ba1\u7b97\u843d\u5728\u5355\u4f4d\u5706\u5185\u7684\u70b9\u7684\u6bd4\u4f8b\u6765\u903c\u8fd1\u5706\u5468\u7387\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u5355\u4f4d\u6b63\u65b9\u5f62\u5185\u968f\u673a\u751f\u6210\u70b9\uff08x, y\uff09\uff0c\u5176\u4e2dx\u548cy\u90fd\u662f[0, 1]\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002<\/li>\n<li>\u8ba1\u7b97\u70b9\u5230\u539f\u70b9\u7684\u8ddd\u79bb\uff0c\u5224\u65ad\u5176\u662f\u5426\u843d\u5728\u5355\u4f4d\u5706\u5185\uff08x^2 + y^2 &lt;= 1\uff09\u3002<\/li>\n<li>\u8ba1\u7b97\u843d\u5728\u5355\u4f4d\u5706\u5185\u7684\u70b9\u7684\u6bd4\u4f8b\uff0c\u4e58\u4ee54\u5373\u4e3a\u03c0\u7684\u8fd1\u4f3c\u503c\u3002<\/li>\n<\/ol>\n<p><p>\u867d\u7136\u8499\u7279\u5361\u7f57\u65b9\u6cd5\u5728\u6982\u5ff5\u4e0a\u975e\u5e38\u76f4\u89c2\uff0c\u4f46\u5176\u7cbe\u5ea6\u53d6\u51b3\u4e8e\u751f\u6210\u7684\u968f\u673a\u70b9\u7684\u6570\u91cf\u3002\u4f7f\u7528\u5927\u91cf\u968f\u673a\u70b9\u53ef\u4ee5\u83b7\u5f97\u8f83\u9ad8\u7684\u7cbe\u5ea6\uff0c\u4f46\u8ba1\u7b97\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u8ba1\u7b97\u5706\u5468\u7387\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u6765\u52a0\u6df1\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8ba1\u7b97\u5706\u7684\u9762\u79ef\u548c\u5468\u957f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5706\u5468\u7387\u8ba1\u7b97\u5706\u7684\u9762\u79ef\u548c\u5468\u957f\u662f\u6700\u5e38\u89c1\u7684\u5e94\u7528\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def circle_area(radius):<\/p>\n<p>    return math.pi * radius  2<\/p>\n<p>def circle_circumference(radius):<\/p>\n<p>    return 2 * math.pi * radius<\/p>\n<p>radius = 5<\/p>\n<p>area = circle_area(radius)<\/p>\n<p>circumference = circle_circumference(radius)<\/p>\n<p>print(f&quot;\u9762\u79ef: {area}, \u5468\u957f: {circumference}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5706\u7684\u9762\u79ef\u516c\u5f0f\u4e3aA = \u03c0r^2\uff0c\u5468\u957f\u516c\u5f0f\u4e3aC = 2\u03c0r\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5df2\u77e5\u7684\u534a\u5f84\u5feb\u901f\u8ba1\u7b97\u51fa\u5706\u7684\u9762\u79ef\u548c\u5468\u957f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5085\u91cc\u53f6\u53d8\u6362<\/h4>\n<\/p>\n<p><p>\u5085\u91cc\u53f6\u53d8\u6362\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6570\u5b66\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4fe1\u53f7\u5904\u7406\u3001\u56fe\u50cf\u5904\u7406\u548c\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u5728\u5085\u91cc\u53f6\u53d8\u6362\u4e2d\uff0c\u5706\u5468\u7387\u03c0\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def fourier_transform(signal):<\/p>\n<p>    n = len(signal)<\/p>\n<p>    frequencies = np.fft.fftfreq(n)<\/p>\n<p>    spectrum = np.fft.fft(signal)<\/p>\n<p>    return frequencies, spectrum<\/p>\n<p>signal = np.sin(2 * np.pi * np.linspace(0, 1, 100))<\/p>\n<p>frequencies, spectrum = fourier_transform(signal)<\/p>\n<p>print(f&quot;\u9891\u7387: {frequencies}, \u9891\u8c31: {spectrum}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5085\u91cc\u53f6\u53d8\u6362\u5c06\u65f6\u95f4\u57df\u4fe1\u53f7\u8f6c\u6362\u4e3a\u9891\u57df\u4fe1\u53f7\uff0c\u63ed\u793a\u4e86\u4fe1\u53f7\u7684\u9891\u7387\u6210\u5206\u3002numpy\u5e93\u4e2d\u7684fft\u6a21\u5757\u63d0\u4f9b\u4e86\u5feb\u901f\u5085\u91cc\u53f6\u53d8\u6362\u7684\u5b9e\u73b0\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86\u5706\u5468\u7387\u03c0\u6765\u8ba1\u7b97\u9891\u7387\u548c\u9891\u8c31\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u548c\u4f7f\u7528\u5706\u5468\u7387\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528math\u6a21\u5757\u3001numpy\u5e93\u3001\u624b\u52a8\u5b9a\u4e49\u4ee5\u53ca\u8ba1\u7b97\u5706\u5468\u7387\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u8ba1\u7b97\u7cbe\u5ea6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5706\u5468\u7387\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u5706\u7684\u51e0\u4f55\u5c5e\u6027\u3001\u5085\u91cc\u53f6\u53d8\u6362\u7b49\u9886\u57df\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u5706\u5468\u7387\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5706\u5468\u7387\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5706\u5468\u7387\uff0c\u4f8b\u5982\u4f7f\u7528\u6570\u5b66\u5e93\u7684\u5e38\u91cf\u3001\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u6216\u8005\u901a\u8fc7\u6570\u503c\u7ea7\u6570\u7b49\u65b9\u5f0f\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>math.pi<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u5706\u5468\u7387\u503c\u3002\u82e5\u60f3\u4e86\u89e3\u4e0d\u540c\u7684\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5efa\u8bae\u53c2\u8003\u76f8\u5173\u6570\u5b66\u77e5\u8bc6\u6216Python\u4e2d\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5982NumPy\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u83b7\u53d6\u5706\u5468\u7387\uff1f<\/strong><br \/>Python\u4e2d\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u83b7\u53d6\u5706\u5468\u7387\uff0c\u5305\u62ec<code>math<\/code>\u3001<code>numpy<\/code>\u548c<code>sympy<\/code>\u3002<code>math<\/code>\u5e93\u63d0\u4f9b\u4e86\u5706\u5468\u7387\u7684\u5e38\u91cf<code>math.pi<\/code>\uff0c\u800c<code>numpy<\/code>\u5e93\u4e5f\u5177\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u7528\u6237\uff0c<code>sympy<\/code>\u5e93\u5219\u63d0\u4f9b\u4e86\u7b26\u53f7\u8ba1\u7b97\u7684\u80fd\u529b\uff0c\u53ef\u4ee5\u751f\u6210\u66f4\u9ad8\u7cbe\u5ea6\u7684\u5706\u5468\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u5706\u5468\u7387\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u6765\u4f30\u7b97\u5706\u5468\u7387\u3002\u901a\u8fc7\u5728\u4e00\u4e2a\u5355\u4f4d\u6b63\u65b9\u5f62\u5185\u968f\u673a\u6492\u70b9\uff0c\u5e76\u8ba1\u7b97\u843d\u5728\u5185\u5207\u5706\u5185\u7684\u70b9\u7684\u6bd4\u4f8b\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u516c\u5f0f\u8ba1\u7b97\u51fa\u5706\u5468\u7387\u7684\u8fd1\u4f3c\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import random\n\ndef estimate_pi(num_samples):\n    inside_circle = 0\n    for _ in range(num_samples):\n        x, y = random.uniform(0, 1), random.uniform(0, 1)\n        if x&lt;strong&gt;2 + y&lt;\/strong&gt;2 &lt;= 1:\n            inside_circle += 1\n    return (inside_circle \/ num_samples) * 4\n\nprint(estimate_pi(1000000))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u901a\u8fc7\u751f\u6210\u968f\u673a\u70b9\uff0c\u8ba1\u7b97\u5706\u5185\u70b9\u7684\u6bd4\u4f8b\u6765\u4f30\u7b97\u5706\u5468\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u5706\u5468\u7387\u5982\u4f55\u5b9a\u4e49\uff1a\u5728Python\u4e2d\uff0c\u5706\u5468\u7387\uff08\u03c0\uff09\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165math\u6a21\u5757\u4e2d\u7684pi\u5e38\u91cf\u6765\u5b9a\u4e49\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1039694,"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\/1039688"}],"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=1039688"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039688\/revisions"}],"predecessor-version":[{"id":1039698,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039688\/revisions\/1039698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039694"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}