{"id":941236,"date":"2024-12-26T21:29:05","date_gmt":"2024-12-26T13:29:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/941236.html"},"modified":"2024-12-26T21:29:07","modified_gmt":"2024-12-26T13:29:07","slug":"python-%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e6%a0%b9%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/941236.html","title":{"rendered":"python \u5982\u4f55\u8868\u793a\u6839\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075535\/51e258fb-b421-4847-806e-de34e8b052a4.webp\" alt=\"python \u5982\u4f55\u8868\u793a\u6839\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8868\u793a\u6839\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u3001math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002<\/strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code>&lt;strong&gt;<\/code>\u6765\u8ba1\u7b97\u6839\u53f7\uff0c\u4f8b\u5982\u4f7f\u7528<code>x&lt;\/strong&gt;0.5<\/code>\u6765\u8868\u793ax\u7684\u5e73\u65b9\u6839\u3002NumPy\u5e93\u4e2d\u7684sqrt\u51fd\u6570\u540c\u6837\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8ba1\u7b97\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u7ec4\u6216\u77e9\u9635\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u89e3\u91caPython\u4e2d\u8868\u793a\u6839\u53f7\u7684\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528math\u6a21\u5757\u7684sqrt\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93math\u6a21\u5757\u63d0\u4f9b\u4e86sqrt\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\u4e14\u7b80\u6d01\uff0c\u9002\u5408\u5904\u7406\u5355\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u65b9\u6839<\/strong><\/h2>\n<p>num = 9<\/p>\n<p>root = math.sqrt(num)<\/p>\n<p>print(f&quot;\u4f7f\u7528math\u6a21\u5757\u8ba1\u7b97\uff0c{num}\u7684\u5e73\u65b9\u6839\u662f\uff1a{root}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86math\u6a21\u5757\u5e76\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u8ba1\u7b979\u7684\u5e73\u65b9\u6839\uff0c\u7ed3\u679c\u4e3a3.0\u3002math\u6a21\u5757\u662fPython\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u4f7f\u7528\u65b9\u4fbf\u4e14\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u652f\u6301\u5e42\u8fd0\u7b97\u7b26<code><\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u901a\u8fc7\u5c06\u6307\u6570\u8bbe\u4e3a0.5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5e73\u65b9\u6839\u7684\u8ba1\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u5bfc\u5165\u4efb\u4f55\u6a21\u5757\uff0c\u9002\u5408\u7b80\u5355\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u8ba1\u7b97\u5e73\u65b9\u6839<\/p>\n<p>num = 16<\/p>\n<p>root = num  0.5<\/p>\n<p>print(f&quot;\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u8ba1\u7b97\uff0c{num}\u7684\u5e73\u65b9\u6839\u662f\uff1a{root}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e8616\u7684\u5e73\u65b9\u6839\uff0c\u7ed3\u679c\u4e3a4.0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u6d01\uff0c\u7f3a\u70b9\u662f\u5bf9\u4e8e\u8d1f\u6570\u8ba1\u7b97\u4f1a\u5bfc\u81f4\u6570\u5b66\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u62ecsqrt\u51fd\u6570\u3002NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u5904\u7406\u6570\u7ec4\u6216\u77e9\u9635\u7684\u5e73\u65b9\u6839\u8fd0\u7b97\uff0c\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 4, 9, 16, 25])<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839<\/strong><\/h2>\n<p>roots = np.sqrt(arr)<\/p>\n<p>print(f&quot;\u4f7f\u7528NumPy\u8ba1\u7b97\uff0c\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9\u6839\u662f\uff1a{roots}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>np.sqrt()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839\u3002NumPy\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6548\u7387\u5f88\u9ad8\uff0c\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u5e38\u7528\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5bf9\u6bd4\u548c\u603b\u7ed3<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>math\u6a21\u5757<\/strong>\uff1a\u9002\u5408\u7b80\u5355\u7684\u5355\u4e2a\u6570\u5b57\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u4f7f\u7528\u65b9\u4fbf\u4e14\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e42\u8fd0\u7b97\u7b26<\/strong>\uff1a\u540c\u6837\u9002\u5408\u7b80\u5355\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u4f46\u5728\u5904\u7406\u8d1f\u6570\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u5408\u5904\u7406\u6570\u7ec4\u6216\u77e9\u9635\u7684\u5e73\u65b9\u6839\u8fd0\u7b97\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u5982\u679c\u4ec5\u4ec5\u662f\u8ba1\u7b97\u5355\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u6839\uff0cmath\u6a21\u5757\u6216\u8005\u5e42\u8fd0\u7b97\u7b26\u5c31\u8db3\u591f\u4e86\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\uff0cNumPy\u65e0\u7591\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8d1f\u6570\u7684\u5e73\u65b9\u6839<\/strong>\uff1a\u5728\u6570\u5b66\u4e0a\uff0c\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u662f\u4e00\u4e2a\u865a\u6570\uff0c\u56e0\u6b64\u76f4\u63a5\u4f7f\u7528<code>math.sqrt()<\/code>\u6216<code>x0.5<\/code>\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u5bf9\u4e8e\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>cmath<\/code>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u652f\u6301\u590d\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0cNumPy\u7684\u6027\u80fd\u8981\u4f18\u4e8emath\u6a21\u5757\u548c\u5e42\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3aNumPy\u7ecf\u8fc7\u4f18\u5316\uff0c\u80fd\u591f\u5229\u7528\u5e95\u5c42\u7684C\u548cFortran\u5e93\u8fdb\u884c\u9ad8\u6548\u7684\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong>\uff1a\u6d6e\u70b9\u6570\u8ba1\u7b97\u53ef\u80fd\u4f1a\u4ea7\u751f\u7cbe\u5ea6\u8bef\u5dee\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u5927\u91cf\u8ba1\u7b97\u65f6\u3002\u8fd9\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u53ef\u4ee5\u5ffd\u7565\u7684\uff0c\u4f46\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u793a\u548c\u8ba1\u7b97\u6839\u53f7\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u8fd8\u662f\u590d\u6742\u7684\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\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\u901a\u8fc7\u8c03\u7528<code>math.sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c<code>math.sqrt(16)<\/code>\u5c06\u8fd4\u56de4.0\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u8be5\u51fd\u6570\u4e4b\u524d\u5148\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u8868\u793a\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08<code>&lt;strong&gt;<\/code>\uff09\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6570x\uff0c\u5e73\u65b9\u6839\u53ef\u4ee5\u8868\u793a\u4e3a<code>x &lt;\/strong&gt; 0.5<\/code>\u3002\u4f8b\u5982\uff0c<code>25 ** 0.5<\/code>\u5c06\u8fd4\u56de5.0\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u4e0d\u5e0c\u671b\u5f15\u5165\u989d\u5916\u6a21\u5757\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u4f1a\u5bfc\u81f4\u6570\u5b66\u9519\u8bef\uff0c\u56e0\u4e3a\u5728\u5b9e\u6570\u8303\u56f4\u5185\u6ca1\u6709\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u8981\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u590d\u6570\u652f\u6301\u3002\u4f7f\u7528<code>cmath.sqrt()<\/code>\u53ef\u4ee5\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c<code>cmath.sqrt(-1)<\/code>\u5c06\u8fd4\u56de<code>1j<\/code>\uff0c\u8868\u793a\u865a\u6570\u5355\u4f4d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5feb\u901f\u8ba1\u7b97\u591a\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>math.sqrt()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>[math.sqrt(x) for x in [1, 4, 9, 16]]<\/code>\u5c06\u8fd4\u56de<code>[1.0, 2.0, 3.0, 4.0]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u591a\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u6839\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u3001math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":941240,"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\/941236"}],"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=941236"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941236\/revisions"}],"predecessor-version":[{"id":941241,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941236\/revisions\/941241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/941240"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=941236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=941236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=941236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}