{"id":1006607,"date":"2024-12-27T10:45:58","date_gmt":"2024-12-27T02:45:58","guid":{"rendered":""},"modified":"2024-12-27T10:46:02","modified_gmt":"2024-12-27T02:46:02","slug":"python%e5%a6%82%e4%bd%95%e5%b9%b3%e6%96%b9%e6%a0%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1006607.html","title":{"rendered":"python\u5982\u4f55\u5e73\u65b9\u6839"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082650\/6eaf90c6-bd2f-4336-ba39-57a006e8e9e5.webp\" alt=\"python\u5982\u4f55\u5e73\u65b9\u6839\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u3001math\u6a21\u5757\u7684sqrt\u51fd\u6570\u3001cmath\u6a21\u5757\u5904\u7406\u590d\u6570\u3001numpy\u5e93\u7684sqrt\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528math\u6a21\u5757\u7684sqrt\u51fd\u6570\u662f\u6700\u5e38\u7528\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\u3002\u8be5\u65b9\u6cd5\u4e0d\u4ec5\u6613\u4e8e\u4f7f\u7528\uff0c\u800c\u4e14\u80fd\u591f\u5904\u7406\u6d6e\u70b9\u6570\uff0c\u8fd4\u56de\u7cbe\u786e\u7684\u7ed3\u679c\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u662f\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e42\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5e73\u65b9\u6839\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u6570\u8bbe\u7f6e\u4e3a0.5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 9<\/p>\n<p>sqrt_value = number  0.5<\/p>\n<p>print(sqrt_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u4e0d\u9700\u8981\u5bfc\u5165\u4efb\u4f55\u6a21\u5757\uff0c\u7b80\u6d01\u660e\u4e86\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\uff0c\u56e0\u4e3a\u8d1f\u6570\u5728\u5b9e\u6570\u8303\u56f4\u5185\u6ca1\u6709\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528math\u6a21\u5757\u7684sqrt\u51fd\u6570<\/p>\n<\/p>\n<p><p>math\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\u7684\u51fd\u6570\u3002\u5176\u4e2d\uff0c<code>sqrt<\/code>\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u975e\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>number = 9<\/p>\n<p>sqrt_value = math.sqrt(number)<\/p>\n<p>print(sqrt_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528math.sqrt\u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u80fd\u591f\u76f4\u63a5\u5904\u7406\u6d6e\u70b9\u6570\uff0c\u5e76\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6bd4\u5e42\u8fd0\u7b97\u7b26\u66f4\u4e3a\u7cbe\u786e\u548c\u9ad8\u6548\u3002<\/strong>\u552f\u4e00\u7684\u9650\u5236\u662f\u5b83\u4e0d\u652f\u6301\u590d\u6570\uff0c\u5982\u679c\u8f93\u5165\u8d1f\u6570\u4f1a\u5f15\u53d1ValueError\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528cmath\u6a21\u5757\u5904\u7406\u590d\u6570<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6570\u5e73\u65b9\u6839\u7684\u60c5\u51b5\uff0ccmath\u6a21\u5757\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002cmath\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u590d\u6570\u7684\u5e73\u65b9\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>number = -9<\/p>\n<p>sqrt_value = cmath.sqrt(number)<\/p>\n<p>print(sqrt_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528cmath.sqrt\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd\u5373\u4f7f\u8f93\u5165\u4e3a\u8d1f\u6570\uff0c\u4e5f\u80fd\u8fd4\u56de\u4e00\u4e2a\u590d\u6570\u4f5c\u4e3a\u7ed3\u679c\u3002<\/strong>\u8fd9\u5728\u5904\u7406\u4fe1\u53f7\u5904\u7406\u3001\u63a7\u5236\u7cfb\u7edf\u6216\u5176\u4ed6\u9700\u8981\u590d\u6570\u8ba1\u7b97\u7684\u9886\u57df\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528numpy\u5e93\u7684sqrt\u51fd\u6570<\/p>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u62ec\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u5e73\u65b9\u6839\u7684<code>sqrt<\/code>\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([4, 9, 16, 25])<\/p>\n<p>sqrt_values = np.sqrt(numbers)<\/p>\n<p>print(sqrt_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy.sqrt\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u80fd\u591f\u9ad8\u6548\u5730\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\u5904\u7406\u3002<\/strong>\u6b64\u5916\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5904\u7406\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fd4\u56de\u6d6e\u70b9\u6570\u6216\u590d\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4e0d\u540c\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0cmath.sqrt\u662f\u9996\u9009\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u590d\u6570\uff0c\u4f7f\u7528cmath.sqrt\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u6570\u7ec4\uff0c\u5219numpy.sqrt\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b\u7684\u5904\u7406<\/strong>\uff1a\u5728\u8fdb\u884c\u5e73\u65b9\u6839\u8ba1\u7b97\u65f6\uff0c\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7c7b\u578b\u4e0e\u65b9\u6cd5\u5339\u914d\u3002math.sqrt\u548c\u5e42\u8fd0\u7b97\u7b26\u9002\u7528\u4e8e\u975e\u8d1f\u5b9e\u6570\uff0c\u800ccmath.sqrt\u9002\u7528\u4e8e\u590d\u6570\u3002numpy.sqrt\u80fd\u591f\u81ea\u52a8\u5904\u7406\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u8f93\u5165\u5e94\u4e3a\u6570\u7ec4\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4e0e\u7cbe\u5ea6<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0cnumpy.sqrt\u901a\u5e38\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\uff0cmath.sqrt\u548ccmath.sqrt\u63d0\u4f9b\u4e86\u53ef\u9760\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u7efc\u5408\u8003\u8651\u9009\u62e9\u6700\u4f18\u89e3<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u5e94\u7528\uff0cmath.sqrt\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u6216\u9700\u8981\u590d\u6570\u652f\u6301\u65f6\uff0cnumpy\u548ccmath\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6df1\u5165\u4e86\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u7279\u70b9\u548c\u5e94\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u540c\u60c5\u5883\u4e0b\u7075\u6d3b\u8fd0\u7528Python\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u529f\u80fd\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u7cbe\u786e\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u65b9\u6839\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\u3002\u901a\u8fc7<code>math.sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u4efb\u610f\u975e\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import math<\/code>\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u8c03\u7528<code>math.sqrt(16)<\/code>\u6765\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\uff0c\u7ed3\u679c\u5c06\u662f4.0\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u8ba1\u7b97\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e58\u65b9\u8fd0\u7b97\u7b26<code>&lt;strong&gt;<\/code>\uff0c\u901a\u8fc7<code>number &lt;\/strong&gt; 0.5<\/code>\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u6b64\u5916\uff0cNumPy\u5e93\u4e5f\u63d0\u4f9b\u4e86<code>sqrt()<\/code>\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5e73\u65b9\u6839\u7684\u8ba1\u7b97\u662f\u5426\u6709\u4efb\u4f55\u9650\u5236\u6216\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u8981\u786e\u4fdd\u8f93\u5165\u503c\u4e3a\u975e\u8d1f\u6570\uff0c\u56e0\u4e3a\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u5728\u5b9e\u6570\u8303\u56f4\u5185\u662f\u672a\u5b9a\u4e49\u7684\u3002\u5982\u679c\u5c1d\u8bd5\u5bf9\u8d1f\u6570\u4f7f\u7528<code>math.sqrt()<\/code>\uff0c\u5c06\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u5bf9\u4e8e\u590d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u8fd9\u6837\u53ef\u4ee5\u5904\u7406\u8d1f\u6570\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u3001math\u6a21\u5757\u7684sqrt\u51fd\u6570\u3001cmath\u6a21\u5757\u5904\u7406\u590d\u6570\u3001numpy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1006619,"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\/1006607"}],"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=1006607"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006607\/revisions"}],"predecessor-version":[{"id":1006621,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006607\/revisions\/1006621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006619"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}