{"id":1017858,"date":"2024-12-27T12:34:58","date_gmt":"2024-12-27T04:34:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1017858.html"},"modified":"2024-12-27T12:35:00","modified_gmt":"2024-12-27T04:35:00","slug":"python%e6%a0%b9%e5%8f%b7%e4%b8%8b%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1017858.html","title":{"rendered":"python\u6839\u53f7\u4e0b\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\/25160649\/295db098-5bb8-428b-b787-802b4727aa0a.webp\" alt=\"python\u6839\u53f7\u4e0b\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u6839\u53f7\u4e0b\u7684\u8868\u793a\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0<\/strong>\uff0c\u4f8b\u5982<strong>\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code>&lt;\/strong&gt;<\/code>\u3001<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3001\u4ee5\u53ca<code>numpy<\/code>\u5e93\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u51c6\u786e\u6027\u8f83\u9ad8\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6839\u53f7\u53ef\u4ee5\u901a\u8fc7\u5e42\u8fd0\u7b97\u7b26\u6765\u8868\u793a\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6570\u5b57x\uff0c\u6839\u53f7\u4e0bx\u53ef\u4ee5\u8868\u793a\u4e3a<code>x0.5<\/code>\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4e0d\u9700\u8981\u5f15\u5165\u4efb\u4f55\u989d\u5916\u7684\u5e93\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 16<\/p>\n<p>result = x  0.5<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u662f\u5185\u7f6e\u7684\uff0c\u4e0d\u9700\u8981\u5bfc\u5165\u4efb\u4f55\u6a21\u5757\u5373\u53ef\u4f7f\u7528\u3002\u7136\u800c\uff0c\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u7279\u6027\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u6216\u9ad8\u7cbe\u5ea6\u9700\u6c42\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u8bef\u5dee\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u7684<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>sqrt<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\u3002<code>math.sqrt()<\/code>\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u66f4\u7cbe\u786e\u7684\u6570\u5b66\u8ba1\u7b97\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u4e3a\u6b64\u7c7b\u64cd\u4f5c\u8bbe\u8ba1\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 16<\/p>\n<p>result = math.sqrt(x)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u6709\u51e0\u4e2a\u4f18\u70b9\u3002\u9996\u5148\uff0c\u5b83\u80fd\u591f\u5904\u7406\u8d1f\u6570\u7684\u60c5\u51b5\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u590d\u6570\uff1b\u5176\u6b21\uff0c\u5b83\u5728\u5904\u7406\u6d6e\u70b9\u6570\u65f6\u6bd4\u5e42\u8fd0\u7b97\u7b26\u66f4\u52a0\u7cbe\u786e\u3002\u5c24\u5176\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c<code>math.sqrt()<\/code>\u662f\u4e00\u4e2a\u66f4\u53ef\u9760\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\uff0c<code>numpy<\/code>\u5e93\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u6781\u5176\u9ad8\u6548\u7684\u9009\u62e9\u3002<code>numpy<\/code>\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = 16<\/p>\n<p>result = np.sqrt(x)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy.sqrt()<\/code>\u4e0d\u4ec5\u80fd\u591f\u5904\u7406\u5355\u4e2a\u6570\u503c\uff0c\u8fd8\u80fd\u591f\u76f4\u63a5\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u4e14\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u548c\u6027\u80fd\u3002\u8fd9\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u8868\u793a\u6839\u53f7\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230\u4f60\u6240\u5904\u7406\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u8fd0\u7b97\u7684\u590d\u6742\u6027\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u5e42\u8fd0\u7b97\u7b26\u5df2\u7ecf\u8db3\u591f\uff1b\u800c\u5728\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u573a\u666f\u4e0b\uff0c<code>math.sqrt()<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u6d89\u53ca\u5230\u5927\u91cf\u7684\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\uff0c\u90a3\u4e48\u4f7f\u7528<code>numpy<\/code>\u5e93\u5c06\u4f1a\u5927\u5927\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u8003\u8651\u56e0\u7d20<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6839\u53f7\u8ba1\u7b97\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5176\u4ed6\u56e0\u7d20\uff0c\u4f8b\u5982\u8ba1\u7b97\u7684\u7cbe\u5ea6\u4ee5\u53ca\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5bf9\u4e8e\u6d89\u53ca\u5230\u91d1\u878d\u3001\u79d1\u5b66\u7b49\u9886\u57df\u7684\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u8003\u8651\u6570\u503c\u8bef\u5dee\u7684\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5728\u5904\u7406\u8d1f\u6570\u65f6\uff0cPython\u4f1a\u8fd4\u56de\u4e00\u4e2a\u590d\u6570\uff0c\u56e0\u6b64\u9700\u8981\u786e\u4fdd\u8f93\u5165\u503c\u5728\u8ba1\u7b97\u4e4b\u524d\u662f\u975e\u8d1f\u7684\uff0c\u6216\u8005\u6839\u636e\u9700\u8981\u5904\u7406\u590d\u6570\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u8868\u793a\u6839\u53f7\u7684\u65b9\u6cd5\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u5408\u7406\u9009\u62e9\u548c\u642d\u914d\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u8ba9\u4f60\u7684\u7a0b\u5e8f\u66f4\u52a0\u9ad8\u6548\u548c\u53ef\u9760\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\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u4e00\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\nresult = math.sqrt(16)  # \u7ed3\u679c\u4e3a4.0\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528<code>&lt;strong&gt;<\/code>\u8fd0\u7b97\u7b26\u4e5f\u80fd\u5b9e\u73b0\u5e73\u65b9\u6839\u7684\u8ba1\u7b97\uff0c\u4f8b\u5982<code>number &lt;\/strong&gt; 0.5<\/code>\u3002<\/p>\n<p><strong>Python\u662f\u5426\u652f\u6301\u590d\u6570\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u590d\u6570\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\u3002\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u590d\u6570\u7684\u5e73\u65b9\u6839\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import cmath\nresult = cmath.sqrt(-1)  # \u7ed3\u679c\u4e3a1j\n<\/code><\/pre>\n<p>\u8fd9\u4f7f\u5f97\u5728\u5904\u7406\u590d\u6570\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\u7684\u5e38\u89c1\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u5e73\u65b9\u6839\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u8bd5\u56fe\u5bf9\u8d1f\u6570\u4f7f\u7528<code>math.sqrt()<\/code>\uff0c\u8fd9\u4f1a\u5bfc\u81f4<code>ValueError<\/code>\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8d1f\u6570\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\uff0c\u6216\u8005\u5728\u8ba1\u7b97\u4e4b\u524d\u68c0\u67e5\u6570\u5b57\u662f\u5426\u4e3a\u8d1f\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\u4e5f\u662f\u91cd\u8981\u7684\u4e00\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6839\u53f7\u4e0b\u7684\u8868\u793a\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26&lt;\/strong&gt;\u3001math\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1017864,"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\/1017858"}],"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=1017858"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017858\/revisions"}],"predecessor-version":[{"id":1017866,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017858\/revisions\/1017866"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1017864"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1017858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1017858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1017858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}