{"id":943967,"date":"2024-12-26T22:57:22","date_gmt":"2024-12-26T14:57:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943967.html"},"modified":"2024-12-26T22:57:23","modified_gmt":"2024-12-26T14:57:23","slug":"python%e5%a6%82%e4%bd%95%e7%ae%97%e6%a0%b9%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943967.html","title":{"rendered":"python\u5982\u4f55\u7b97\u6839\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081153\/42f00615-3c58-48da-8a4a-9a793c2a2e83.webp\" alt=\"python\u5982\u4f55\u7b97\u6839\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u65b9\u6839\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u5e93\u3001\u6307\u6570\u8fd0\u7b97\u3001SciPy\u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u6307\u6570\u8fd0\u7b97\u7b26<code>&lt;\/strong&gt;<\/code>\u4ee5\u53ca\u4f7f\u7528NumPy\u5e93\u7684<code>numpy.sqrt<\/code>\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c<code>math.sqrt<\/code>\u901f\u5ea6\u8f83\u5feb\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u6307\u6570\u8fd0\u7b97\u7b26\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u800cNumPy\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u901a\u8fc7\u8c03\u7528\u5e95\u5c42\u7684C\u5e93\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u56e0\u800c\u5728\u6027\u80fd\u4e0a\u975e\u5e38\u9ad8\u6548\u3002\u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u5e76\u8c03\u7528<code>sqrt<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>number = 25<\/p>\n<p>sqrt_value = math.sqrt(number)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>math.sqrt<\/code>\u51fd\u6570\u8ba1\u7b97\u6570\u5b5725\u7684\u5e73\u65b9\u6839\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a5.0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6307\u6570\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>math.sqrt<\/code>\u51fd\u6570\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4f7f\u7528\u6307\u6570\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u3002\u6307\u6570\u8fd0\u7b97\u7b26<code><\/code>\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u6570\u7684\u5e42\u6b21\u8fd0\u7b97\u3002\u5728\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6307\u6570\u8bbe\u7f6e\u4e3a0.5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 25<\/p>\n<p>sqrt_value = number  0.5<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\uff0c\u76f4\u63a5\u4f7f\u7528Python\u7684\u57fa\u672c\u8fd0\u7b97\u7b26\u5373\u53ef\u5b9e\u73b0\u3002\u540c\u6837\u80fd\u591f\u5f97\u51fa25\u7684\u5e73\u65b9\u6839\u662f5.0\u3002\u6307\u6570\u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230\u5176\u4ed6\u6839\u53f7\u8fd0\u7b97\uff0c\u4f8b\u5982\u7acb\u65b9\u6839\u3001\u56db\u6b21\u6839\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u7684\u60c5\u51b5\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5176<code>numpy.sqrt<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([4, 16, 25, 36])<\/p>\n<p>sqrt_values = np.sqrt(array)<\/p>\n<p>print(f&quot;The square roots are {sqrt_values}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>numpy.sqrt<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u6839\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5143\u7d20\u5e73\u65b9\u6839\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6027\u80fd\u6bd4\u8f83\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\u662f\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1a<code>math.sqrt<\/code>\u901a\u5e38\u662f\u6700\u5feb\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u8c03\u7528\u5e95\u5c42\u7684C\u5e93\u51fd\u6570\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u9700\u8981\u5e76\u884c\u8ba1\u7b97\u7684\u573a\u666f\uff0cNumPy\u4f1a\u8868\u73b0\u5f97\u66f4\u597d\uff0c\u56e0\u4e3a\u5b83\u662f\u9488\u5bf9\u6570\u7ec4\u64cd\u4f5c\u8fdb\u884c\u4e86\u4f18\u5316\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u5982\u679c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53ea\u9700\u8981\u8ba1\u7b97\u5355\u4e2a\u6570\u503c\u7684\u5e73\u65b9\u6839\uff0c<code>math.sqrt<\/code>\u6216\u6307\u6570\u8fd0\u7b97\u7b26\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\u3002\u5982\u679c\u60a8\u9700\u8981\u5904\u7406\u6570\u7ec4\u6216\u77e9\u9635\u4e2d\u7684\u591a\u4e2a\u6570\u503c\uff0cNumPy\u5c06\u63d0\u4f9b\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u7684\u5e73\u65b9\u6839<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u8fd9\u4f1a\u4ea7\u751f\u4e00\u4e2a\u590d\u6570\u3002Python\u7684<code>cmath<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u590d\u6570\u7684\u51fd\u6570\uff0c\u5176\u4e2d<code>cmath.sqrt<\/code>\u53ef\u4ee5\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>complex_number = -25<\/p>\n<p>sqrt_value = cmath.sqrt(complex_number)<\/p>\n<p>print(f&quot;The square root of {complex_number} is {sqrt_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u8ba1\u7b97\u4e86\u8d1f\u6570-25\u7684\u5e73\u65b9\u6839\uff0c\u8fd4\u56de\u4e00\u4e2a\u590d\u6570\u7ed3\u679c5j\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6570\u7684\u5e94\u7528\u573a\u666f\uff0c<code>cmath<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u7279\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u5355\u503c\u8ba1\u7b97\uff0c<code>math.sqrt<\/code>\u548c\u6307\u6570\u8fd0\u7b97\u7b26\u662f\u7406\u60f3\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff1b\u800c\u5bf9\u4e8e\u590d\u6570\u8ba1\u7b97\uff0c<code>cmath<\/code>\u6a21\u5757\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002\u901a\u8fc7\u7406\u89e3\u548c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u80fd\u591f\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u5e73\u65b9\u6839\u8ba1\u7b97\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\u8ba1\u7b97\u5e73\u65b9\u6839\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt()<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165<code>math<\/code>\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>sqrt()<\/code>\u51fd\u6570\u6765\u83b7\u5f97\u4e00\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c<code>math.sqrt(16)<\/code>\u5c06\u8fd4\u56de4.0\uff0c\u8868\u793a16\u7684\u5e73\u65b9\u6839\u662f4\u3002<\/p>\n<p><strong>Python\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>math.sqrt()<\/code>\uff0cPython\u8fd8\u652f\u6301\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code>&lt;strong&gt;<\/code>\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u901a\u8fc7\u5c06\u6570\u5b57\u7684\u5e42\u8bbe\u4e3a0.5\uff0c\u53ef\u4ee5\u5f97\u5230\u5176\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c<code>16 &lt;\/strong&gt; 0.5<\/code>\u4e5f\u4f1a\u8fd4\u56de4.0\u3002\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86<code>numpy.sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u6570\u7ec4\u548c\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8d1f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u4f1a\u5f15\u53d1<code>ValueError<\/code>\uff0c\u56e0\u4e3a\u5728\u5b9e\u6570\u8303\u56f4\u5185\uff0c\u8d1f\u6570\u6ca1\u6709\u5e73\u65b9\u6839\u3002\u5982\u679c\u9700\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>complex<\/code>\u7c7b\u578b\u6216\u8005<code>cmath<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff0c<code>cmath.sqrt(-16)<\/code>\u5c06\u8fd4\u56de4j\uff0c\u8868\u793a\u8d1f16\u7684\u5e73\u65b9\u6839\u4e3a4\u4e58\u4ee5\u865a\u6570\u5355\u4f4dj\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u65b9\u6839\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u5e93\u3001\u6307\u6570\u8fd0\u7b97\u3001SciPy\u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":943970,"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\/943967"}],"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=943967"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943967\/revisions"}],"predecessor-version":[{"id":943971,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943967\/revisions\/943971"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/943970"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=943967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=943967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=943967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}