{"id":1019112,"date":"2024-12-27T12:50:59","date_gmt":"2024-12-27T04:50:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019112.html"},"modified":"2024-12-27T12:51:08","modified_gmt":"2024-12-27T04:51:08","slug":"python%e5%bc%80%e6%a0%b9%e5%8f%b7%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019112.html","title":{"rendered":"python\u5f00\u6839\u53f7\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161645\/27ede283-4cfa-426a-9402-aa6529575e69.webp\" alt=\"python\u5f00\u6839\u53f7\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u3001<code>&lt;\/strong&gt;<\/code>\u8fd0\u7b97\u7b26\u3001\u4ee5\u53ca<code>numpy.sqrt()<\/code>\u51fd\u6570\u53ef\u4ee5\u8868\u793a\u5f00\u6839\u53f7<strong>\u3002\u5176\u4e2d\uff0c<code>math.sqrt()<\/code>\u662f\u6807\u51c6\u5e93\u4e2d\u7684\u65b9\u6cd5\uff0c<code>&lt;\/strong&gt;<\/code>\u8fd0\u7b97\u7b26\u662f\u76f4\u63a5\u8fdb\u884c\u5e42\u8fd0\u7b97\uff0c<code>numpy.sqrt()<\/code>\u662f\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u7684\u5f00\u6839\u53f7\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p><strong><code>math.sqrt()<\/code>\u51fd\u6570<\/strong>\u662f\u5904\u7406\u6d6e\u70b9\u6570\u5f00\u6839\u53f7\u7684\u6807\u51c6\u65b9\u6cd5\u4e4b\u4e00\u3002\u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165math\u6a21\u5757\u3002math\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u8fd0\u7b97\u51fd\u6570\uff0c\u5176\u4e2d<code>math.sqrt()<\/code>\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u975e\u8d1f\u6570\u5b57\u7684\u5e73\u65b9\u6839\u3002\u6bd4\u5982\u8981\u8ba1\u7b979\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.sqrt(9)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a3.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong><code>&lt;\/strong&gt;<\/code>\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u7684\u5e42\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u4efb\u610f\u6b21\u65b9\u3002\u901a\u8fc7\u5c06\u6b21\u65b9\u6570\u8bbe\u7f6e\u4e3a0.5\uff0c\u53ef\u4ee5\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c\u8981\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 16  0.5<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5f00\u6839\u53f7\u7684\u4e0d\u540c\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MATH.SQRT()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>math.sqrt()<\/code>\u51fd\u6570\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6570\u5b66\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u975e\u8d1f\u6570\u5b57\u7684\u5e73\u65b9\u6839\u3002\u5b83\u662f\u5904\u7406\u5f00\u6839\u53f7\u95ee\u9898\u7684\u4e00\u4e2a\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u7528<code>math.sqrt()<\/code>\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165math\u6a21\u5757\u3002math\u6a21\u5757\u5305\u542b\u4e86\u8bb8\u591a\u6570\u5b66\u8ba1\u7b97\u7684\u51fd\u6570\uff0c<code>math.sqrt()<\/code>\u662f\u5176\u4e2d\u4e4b\u4e00\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f20\u5165\u4e00\u4e2a\u975e\u8d1f\u6570\u5b57\u5373\u53ef\u3002\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.sqrt(25)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u8d1f\u6570\u548c\u5f02\u5e38<\/h4>\n<\/p>\n<p><p><code>math.sqrt()<\/code>\u51fd\u6570\u4e0d\u652f\u6301\u8d1f\u6570\u3002\u5982\u679c\u4f20\u5165\u4e00\u4e2a\u8d1f\u6570\uff0c\u4f1a\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528<code>math.sqrt()<\/code>\u4e4b\u524d\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u6570\u503c\u662f\u975e\u8d1f\u7684\u3002\u5982\u679c\u9700\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u6a21\u5757<code>cmath<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>result = cmath.sqrt(-25)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a5j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code>math.sqrt()<\/code>\u51fd\u6570\u5728\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u7269\u7406\u5b66\u4e2d\u8ba1\u7b97\u8ddd\u79bb\u3001\u5728\u51e0\u4f55\u5b66\u4e2d\u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u659c\u8fb9\u3001\u6216\u5728\u7edf\u8ba1\u5b66\u4e2d\u8ba1\u7b97\u6807\u51c6\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p><code><\/code>\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u7684\u5e42\u8fd0\u7b97\u7b26\uff0c\u901a\u8fc7\u8bbe\u7f6e\u4e3a0.5\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u5bfc\u5165\u4efb\u4f55\u6a21\u5757\uff0c\u975e\u5e38\u76f4\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code><\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u901a\u8fc7\u5c06\u6b21\u5e42\u8bbe\u7f6e\u4e3a0.5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 36  0.5<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a6.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code><\/code>\u8fd0\u7b97\u7b26\u7684\u4f18\u70b9\u662f\u7b80\u6d01\uff0c\u4e0d\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8d1f\u6570\u7684\u8ba1\u7b97\uff0c\u4f46\u662f\u7ed3\u679c\u662f\u4e00\u4e2a\u590d\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = (-36)  0.5<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a6j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f3a\u70b9\u662f\u5b83\u53ef\u80fd\u4e0d\u5982<code>math.sqrt()<\/code>\u7684\u8bed\u4e49\u6e05\u6670\uff0c\u56e0\u4e3a<code>math.sqrt()<\/code>\u660e\u786e\u8868\u793a\u4e86\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u610f\u56fe\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code><\/code>\u8fd0\u7b97\u7b26\u5728\u9700\u8981\u5feb\u901f\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u573a\u5408\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5728\u811a\u672c\u6216\u7b80\u5355\u8ba1\u7b97\u4e2d\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u66f4\u9ad8\u6b21\u5e42\u7684\u6839\uff0c\u4f8b\u5982\u7acb\u65b9\u6839\uff08\u6b21\u5e42\u4e3a1\/3\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NUMPY.SQRT()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>numpy.sqrt()<\/code>\u51fd\u6570\u662fNumPy\u5e93\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839\u3002NumPy\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.sqrt()<\/code>\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165NumPy\u6765\u4f7f\u7528<code>numpy.sqrt()<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 4, 9, 16])<\/p>\n<p>result = np.sqrt(array)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[1. 2. 3. 4.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5927\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p><code>numpy.sqrt()<\/code>\u51fd\u6570\u7279\u522b\u9002\u5408\u7528\u4e8e\u5927\u6570\u636e\u96c6\u7684\u8ba1\u7b97\u3002\u5b83\u80fd\u591f\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839\uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code>numpy.sqrt()<\/code>\u5e7f\u6cdb\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u4e2d\uff0c\u53ef\u4ee5\u7528\u6765\u5bf9\u50cf\u7d20\u503c\u8fdb\u884c\u5e73\u65b9\u6839\u53d8\u6362\u4ee5\u589e\u5f3a\u5bf9\u6bd4\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528SYMPY\u5e93\u8ba1\u7b97\u5e73\u65b9\u6839<\/h3>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u7cbe\u786e\u8ba1\u7b97\u548c\u7b26\u53f7\u8868\u793a\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5SymPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165SymPy\u6765\u4f7f\u7528\u5176\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<p>result = sp.sqrt(8)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2*sqrt(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7b26\u53f7\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>SymPy\u53ef\u4ee5\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u80fd\u591f\u8fd4\u56de\u7cbe\u786e\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6570\u503c\u8fd1\u4f3c\u3002\u4f8b\u5982\uff0c\u8ba1\u7b978\u7684\u5e73\u65b9\u6839\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de<code>2*sqrt(2)<\/code>\uff0c\u800c\u4e0d\u662f\u8fd1\u4f3c\u503c<code>2.8284<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>SymPy\u4e3b\u8981\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u6570\u5b66\u8ba1\u7b97\u7684\u573a\u5408\uff0c\u5982\u6570\u5b66\u7814\u7a76\u3001\u6559\u80b2\u3001\u7b26\u53f7\u6c42\u89e3\u7b49\u3002\u5b83\u53ef\u4ee5\u4e0eLaTeX\u7ed3\u5408\u751f\u6210\u6570\u5b66\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e0d\u540c\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5355\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c<code>math.sqrt()<\/code>\u548c<code><\/code>\u8fd0\u7b97\u7b26\u7684\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\uff0c\u90fd\u662f\u76f8\u5bf9\u9ad8\u6548\u7684\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u7ec4\u8ba1\u7b97\uff0c<code>numpy.sqrt()<\/code>\u7684\u6027\u80fd\u660e\u663e\u4f18\u4e8e<code>math.sqrt()<\/code>\uff0c\u56e0\u4e3aNumPy\u9488\u5bf9\u6570\u7ec4\u8fd0\u7b97\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6613\u7528\u6027<\/h4>\n<\/p>\n<p><p><code>math.sqrt()<\/code>\u548c<code><\/code>\u8fd0\u7b97\u7b26\u90fd\u975e\u5e38\u6613\u7528\uff0c\u9002\u5408\u4e8e\u7b80\u5355\u8ba1\u7b97\u573a\u5408\u3002<code>numpy.sqrt()<\/code>\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u5927\u6570\u636e\u96c6\uff0c\u800cSymPy\u9002\u5408\u7b26\u53f7\u8ba1\u7b97\u548c\u7cbe\u786e\u6570\u5b66\u8868\u8fbe\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>numpy.sqrt()<\/code>\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u6279\u91cf\u8fd0\u7b97\u3002\u5728\u9700\u8981\u7b26\u53f7\u8ba1\u7b97\u6216\u7cbe\u786e\u6570\u5b66\u8868\u8fbe\u7684\u573a\u5408\uff0cSymPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3Python\u4e2d\u5f00\u6839\u53f7\u7684\u5e94\u7528\uff0c\u4e0b\u9762\u7ed9\u51fa\u51e0\u4e2a\u5b9e\u8df5\u4e2d\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51e0\u4f55\u5e94\u7528\uff1a\u8ba1\u7b97\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u659c\u8fb9<\/h4>\n<\/p>\n<p><p>\u5728\u76f4\u89d2\u4e09\u89d2\u5f62\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52fe\u80a1\u5b9a\u7406\u8ba1\u7b97\u659c\u8fb9\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u5df2\u77e5\u4e24\u76f4\u89d2\u8fb9\u957f\u4e3a3\u548c4\uff0c\u659c\u8fb9\u7684\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u5e73\u65b9\u548c\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u5f97\u5230\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 3<\/p>\n<p>b = 4<\/p>\n<p>c = math.sqrt(a&lt;strong&gt;2 + b&lt;\/strong&gt;2)<\/p>\n<p>print(c)  # \u8f93\u51fa\uff1a5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5206\u6790\uff1a\u6807\u51c6\u5dee\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6807\u51c6\u5dee\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7edf\u8ba1\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u636e\u96c6\u7684\u6807\u51c6\u5dee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 2, 3, 4, 5])<\/p>\n<p>mean = np.mean(data)<\/p>\n<p>variance = np.mean((data - mean)  2)<\/p>\n<p>std_dev = np.sqrt(variance)<\/p>\n<p>print(std_dev)  # \u8f93\u51fa\uff1a1.4142135623730951<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7269\u7406\u5e94\u7528\uff1a\u8ba1\u7b97\u81ea\u7531\u843d\u4f53\u8fd0\u52a8\u7684\u901f\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u7269\u7406\u5b66\u4e2d\uff0c\u81ea\u7531\u843d\u4f53\u8fd0\u52a8\u7684\u901f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u521d\u901f\u5ea6\u548c\u52a0\u901f\u5ea6\u8ba1\u7b97\u5f97\u5230\u3002\u5047\u8bbe\u521d\u901f\u5ea6\u4e3a0\uff0c\u52a0\u901f\u5ea6\u4e3a9.8 m\/s\u00b2\uff0c\u7ecf\u8fc7\u65f6\u95f4t\u540e\u7684\u901f\u5ea6\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>t = 5  # \u65f6\u95f4\u4e3a5\u79d2<\/p>\n<p>g = 9.8  # \u91cd\u529b\u52a0\u901f\u5ea6\u4e3a9.8 m\/s\u00b2<\/p>\n<p>v = g * t<\/p>\n<p>print(v)  # \u8f93\u51fa\uff1a49.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u9519\u8bef\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u5e76\u77e5\u9053\u5982\u4f55\u8c03\u8bd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8d1f\u6570\u5e73\u65b9\u6839\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f7f\u7528<code>math.sqrt()<\/code>\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528<code>cmath<\/code>\u5e93\u8fdb\u884c\u590d\u6570\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>result = cmath.sqrt(-1)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a1j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u7c7b\u578b\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>numpy.sqrt()<\/code>\u65f6\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u662f<code>numpy<\/code>\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u666e\u901a\u7684Python\u5217\u8868\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u6b63\u786e<\/strong><\/h2>\n<p>array = np.array([1, 4, 9])<\/p>\n<p>result = np.sqrt(array)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[1. 2. 3.]<\/p>\n<h2><strong>\u9519\u8bef<\/strong><\/h2>\n<h2><strong>result = np.sqrt([1, 4, 9])  # \u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7cbe\u5ea6\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code><\/code>\u8fd0\u7b97\u7b26\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u3002\u5bf9\u4e8e\u7cbe\u786e\u8ba1\u7b97\uff0c\u5efa\u8bae\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\u6216SymPy\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>result = Decimal(16)  Decimal(&#39;0.5&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001Python\u5f00\u6839\u53f7\u7684\u672a\u6765\u53d1\u5c55\u65b9\u5411<\/h3>\n<\/p>\n<p><p>\u968f\u7740\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u53d1\u5c55\uff0c\u5bf9\u5f00\u6839\u53f7\u8ba1\u7b97\u7684\u9700\u6c42\u4e5f\u5728\u4e0d\u65ad\u589e\u52a0\u3002Python\u5728\u8fd9\u65b9\u9762\u7684\u652f\u6301\u4e5f\u5728\u4e0d\u65ad\u5b8c\u5584\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u672a\u6765\uff0cPython\u53ca\u5176\u5e93\u53ef\u80fd\u4f1a\u8fdb\u4e00\u6b65\u4f18\u5316\u5f00\u6839\u53f7\u8ba1\u7b97\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5927\u6570\u636e\u548c\u5e76\u884c\u8ba1\u7b97\u4e2d\u3002\u8fd9\u53ef\u80fd\u6d89\u53ca\u5bf9\u5e95\u5c42\u7b97\u6cd5\u548c\u786c\u4ef6\u52a0\u901f\u7684\u6539\u8fdb\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6269\u5c55\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u968f\u7740\u91cf\u5b50\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\u7684\u53d1\u5c55\uff0c\u5f00\u6839\u53f7\u8ba1\u7b97\u5728\u8fd9\u4e9b\u9886\u57df\u7684\u5e94\u7528\u4e5f\u53ef\u80fd\u4f1a\u589e\u52a0\u3002Python\u53ef\u80fd\u4f1a\u63d0\u4f9b\u66f4\u591a\u7684\u5de5\u5177\u548c\u5e93\u6765\u652f\u6301\u8fd9\u4e9b\u65b0\u5174\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6613\u7528\u6027\u63d0\u5347<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u66f4\u591a\u7684\u7528\u6237\u80fd\u591f\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\uff0cPython\u53ef\u80fd\u4f1a\u5728\u8bed\u6cd5\u548c\u51fd\u6570\u8bbe\u8ba1\u4e0a\u8fdb\u884c\u6539\u8fdb\uff0c\u4f7f\u5f97\u5f00\u6839\u53f7\u8ba1\u7b97\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u5305\u62ec<code>math.sqrt()<\/code>\u3001<code><\/code>\u8fd0\u7b97\u7b26\u3001<code>numpy.sqrt()<\/code>\u548cSymPy\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u5408\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u901a\u8fc7\u5b9e\u8df5\u548c\u5b66\u4e60\uff0cPython\u7684\u5f00\u6839\u53f7\u8ba1\u7b97\u53ef\u4ee5\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u3001\u7269\u7406\u6a21\u62df\u7b49\u591a\u4e2a\u9886\u57df\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u5e76\u4fc3\u8fdbPython\u5728\u6570\u5b66\u8ba1\u7b97\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>math<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\nresult = math.sqrt(16)\nprint(result)  # \u8f93\u51fa: 4.0\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cPython\u7684\u5e42\u8fd0\u7b97\u7b26<code>**<\/code>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = 16 ** 0.5\nprint(result)  # \u8f93\u51fa: 4.0\n<\/code><\/pre>\n<p><strong>Python\u662f\u5426\u652f\u6301\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>math.sqrt()<\/code>\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\uff0c\u8fd9\u4e2a\u6a21\u5757\u4e13\u4e3a\u590d\u6570\u8ba1\u7b97\u8bbe\u8ba1\u3002\u5982\u679c\u60f3\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import cmath\nresult = cmath.sqrt(-16)\nprint(result)  # \u8f93\u51fa: 4j\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u590d\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u7528\u4ec0\u4e48\u65b9\u6cd5\u6765\u8ba1\u7b97\u4efb\u610f\u6b21\u65b9\u7684\u6839\uff1f<\/strong><br \/>\u9664\u4e86\u5e73\u65b9\u6839\uff0cPython\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u4efb\u610f\u6b21\u65b9\u7684\u6839\u3002\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code>**<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u8981\u8ba1\u7b978\u7684\u7acb\u65b9\u6839\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">result = 8 ** (1\/3)\nprint(result)  # \u8f93\u51fa: 2.0\n<\/code><\/pre>\n<p>\u8fd9\u8868\u793a8\u76841\/3\u6b21\u65b9\uff0c\u5373\u7acb\u65b9\u6839\u3002\u5bf9\u4e8e\u5176\u4ed6\u6b21\u65b9\u7684\u6839\uff0c\u8c03\u6574\u6307\u6570\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528math.sqrt()\u51fd\u6570\u3001&lt;\/strong&gt;\u8fd0\u7b97\u7b26\u3001\u4ee5\u53canumpy.sqr [&hellip;]","protected":false},"author":3,"featured_media":1019129,"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\/1019112"}],"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=1019112"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019112\/revisions"}],"predecessor-version":[{"id":1019131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019112\/revisions\/1019131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019129"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}