{"id":1107810,"date":"2025-01-08T16:53:37","date_gmt":"2025-01-08T08:53:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107810.html"},"modified":"2025-01-08T16:53:40","modified_gmt":"2025-01-08T08:53:40","slug":"python%e9%87%8c%e5%a6%82%e4%bd%95%e5%af%b9%e6%95%b0%e5%ad%97%e5%8f%96%e5%b9%b3%e6%96%b9%e6%a0%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1107810.html","title":{"rendered":"python\u91cc\u5982\u4f55\u5bf9\u6570\u5b57\u53d6\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\/25071700\/782844ec-97bc-4b06-955f-df84e0072db9.webp\" alt=\"python\u91cc\u5982\u4f55\u5bf9\u6570\u5b57\u53d6\u5e73\u65b9\u6839\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bf9\u6570\u5b57\u53d6\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528math\u5e93\u3001\u4f7f\u7528cmath\u5e93\u3001\u4f7f\u7528NumPy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528math\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>\u4f7f\u7528math\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684math\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u62ec\u8ba1\u7b97\u5e73\u65b9\u6839\u7684sqrt\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u6b63\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>number = 16<\/p>\n<p>sqrt_number = math.sqrt(number)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cmath.sqrt()\u51fd\u6570\u88ab\u7528\u6765\u8ba1\u7b97\u6570\u5b5716\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c4.0\u3002<strong>math\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u4f7f\u7528cmath\u5e93<\/strong><\/p>\n<\/p>\n<p><p>cmath\u5e93\u662f\u7528\u4e8e\u590d\u6570\u8fd0\u7b97\u7684\u5e93\u3002\u5982\u679c\u4f60\u9700\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0ccmath\u5e93\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5904\u7406\u590d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>number = -16<\/p>\n<p>sqrt_number = cmath.sqrt(number)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ccmath.sqrt()\u51fd\u6570\u88ab\u7528\u6765\u8ba1\u7b97\u6570\u5b57-16\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c4j\u3002j\u4ee3\u8868\u865a\u6570\u5355\u4f4d\u3002<strong>cmath\u5e93\u5141\u8bb8\u6211\u4eec\u5728\u5904\u7406\u590d\u6570\u65f6\u8fdb\u884c\u5e73\u65b9\u6839\u8fd0\u7b97\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u4f7f\u7528NumPy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ecsqrt\u51fd\u6570\u3002NumPy\u7684sqrt\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u6570\u7ec4\u548c\u6807\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>number = 16<\/p>\n<p>sqrt_number = np.sqrt(number)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p>numbers = np.array([4, 9, 16])<\/p>\n<p>sqrt_numbers = np.sqrt(numbers)<\/p>\n<p>print(f&quot;The square roots of {numbers} are {sqrt_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cnp.sqrt()\u51fd\u6570\u88ab\u7528\u6765\u8ba1\u7b97\u6570\u5b5716\u548c\u6570\u7ec4[4, 9, 16]\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<strong>NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u636e\u8fd0\u7b97\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001<strong>\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u548c\uff08pow\uff09\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u4e0a\u8ff0\u5e93\u51fd\u6570\u76f4\u89c2\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u6709\u6548\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 16<\/p>\n<p>sqrt_number = number  0.5<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p>sqrt_number = pow(number, 0.5)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5e42\u8fd0\u7b97\u7b26\uff08<strong>\uff09\u548cpow\u51fd\u6570\u88ab\u7528\u6765\u8ba1\u7b97\u6570\u5b5716\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u5916\u90e8\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5b9a\u4e49\u81ea\u5df1\u7684\u5e73\u65b9\u6839\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u6216\u5176\u4ed6\u6570\u503c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sqrt_newton(x, tolerance=1e-10):<\/p>\n<p>    if x &lt; 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Cannot compute square root of a negative number&quot;)<\/p>\n<p>    estimate = x<\/p>\n<p>    while True:<\/p>\n<p>        new_estimate = (estimate + x \/ estimate) \/ 2<\/p>\n<p>        if abs(new_estimate - estimate) &lt; tolerance:<\/p>\n<p>            return new_estimate<\/p>\n<p>        estimate = new_estimate<\/p>\n<p>number = 16<\/p>\n<p>sqrt_number = sqrt_newton(number)<\/p>\n<p>print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3asqrt_newton\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u5141\u8bb8\u6211\u4eec\u5b8c\u5168\u63a7\u5236\u8ba1\u7b97\u8fc7\u7a0b\uff0c\u5e76\u53ef\u4ee5\u8c03\u6574\u5bb9\u5dee\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001<strong>\u5904\u7406\u65e0\u6548\u8f93\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u65e0\u6548\u8f93\u5165\uff0c\u6bd4\u5982\u8d1f\u6570\u6216\u975e\u6570\u5b57\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u65e0\u6548\u8f93\u5165\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_sqrt(x):<\/p>\n<p>    try:<\/p>\n<p>        if x &lt; 0:<\/p>\n<p>            raise ValueError(&quot;Cannot compute square root of a negative number&quot;)<\/p>\n<p>        return math.sqrt(x)<\/p>\n<p>    except TypeError:<\/p>\n<p>        raise ValueError(&quot;Input must be a number&quot;)<\/p>\n<p>number = 16<\/p>\n<p>try:<\/p>\n<p>    sqrt_number = safe_sqrt(number)<\/p>\n<p>    print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p>number = -16<\/p>\n<p>try:<\/p>\n<p>    sqrt_number = safe_sqrt(number)<\/p>\n<p>    print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p>number = &quot;sixteen&quot;<\/p>\n<p>try:<\/p>\n<p>    sqrt_number = safe_sqrt(number)<\/p>\n<p>    print(f&quot;The square root of {number} is {sqrt_number}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3asafe_sqrt\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u8ba1\u7b97\u5e73\u65b9\u6839\u4e4b\u524d\u68c0\u67e5\u8f93\u5165\u662f\u5426\u6709\u6548\u3002\u5982\u679c\u8f93\u5165\u65e0\u6548\uff0c\u51fd\u6570\u4f1a\u5f15\u53d1\u9002\u5f53\u7684\u5f02\u5e38\u5e76\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u6d88\u606f\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e03\u3001<strong>\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u8ba1\u7b97\u5e73\u65b9\u6839\u6027\u80fd\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528NumPy\u5e93<\/strong>\uff1aNumPy\u5e93\u7684sqrt\u51fd\u6570\u5bf9\u6570\u7ec4\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.random.rand(1000000)<\/p>\n<p>sqrt_numbers = np.sqrt(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import math<\/p>\n<p>def compute_sqrt(x):<\/p>\n<p>    return math.sqrt(x)<\/p>\n<p>numbers = [i for i in range(1000000)]<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    sqrt_numbers = list(executor.map(compute_sqrt, numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528PyPy\u89e3\u91ca\u5668<\/strong>\uff1aPyPy\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-shell\">pypy script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u3001\u591a\u7ebf\u7a0b\u548cPyPy\u89e3\u91ca\u5668\u6765\u4f18\u5316\u5e73\u65b9\u6839\u8ba1\u7b97\u7684\u6027\u80fd\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u4eec\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516b\u3001<strong>\u5e94\u7528\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u5c55\u793a\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u51e0\u4f55\u8ba1\u7b97<\/strong>\uff1a\u8ba1\u7b97\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u659c\u8fb9\u957f\u5ea6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def hypotenuse(a, b):<\/p>\n<p>    return math.sqrt(a&lt;strong&gt;2 + b&lt;\/strong&gt;2)<\/p>\n<p>a = 3<\/p>\n<p>b = 4<\/p>\n<p>c = hypotenuse(a, b)<\/p>\n<p>print(f&quot;The length of the hypotenuse is {c}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7edf\u8ba1\u5206\u6790<\/strong>\uff1a\u8ba1\u7b97\u6807\u51c6\u5dee\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = [10, 12, 23, 23, 16, 23, 21, 16]<\/p>\n<p>mean = np.mean(data)<\/p>\n<p>variance = np.mean((data - mean)2)<\/p>\n<p>std_deviation = np.sqrt(variance)<\/p>\n<p>print(f&quot;The standard deviation is {std_deviation}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7269\u7406\u8ba1\u7b97<\/strong>\uff1a\u8ba1\u7b97\u81ea\u7531\u843d\u4f53\u8fd0\u52a8\u4e2d\u7684\u4f4d\u79fb\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def free_fall_displacement(time, gravity=9.81):<\/p>\n<p>    return 0.5 * gravity * time2<\/p>\n<p>time = 5<\/p>\n<p>displacement = free_fall_displacement(time)<\/p>\n<p>print(f&quot;The displacement is {displacement} meters&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u8ba1\u7b97\u5e73\u65b9\u6839\u5728\u51e0\u4f55\u8ba1\u7b97\u3001\u7edf\u8ba1\u5206\u6790\u548c\u7269\u7406\u8ba1\u7b97\u4e2d\u7684\u5b9e\u9645\u5e94\u7528\u3002<strong>\u8fd9\u4e9b\u793a\u4f8b\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u4eec\u5c55\u793a\u4e86\u5e73\u65b9\u6839\u8ba1\u7b97\u7684\u5e7f\u6cdb\u7528\u9014\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bf9\u6570\u5b57\u53d6\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528math\u5e93\u3001\u4f7f\u7528cmath\u5e93\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u65e0\u6548\u8f93\u5165\u548c\u6027\u80fd\u4f18\u5316\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u5065\u58ee\u6027\u548c\u6027\u80fd\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\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\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u4f20\u5165\u4f60\u60f3\u8981\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c<code>import math<\/code>\u548c<code>math.sqrt(16)<\/code>\u5c06\u8fd4\u56de4.0\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002Python\u5141\u8bb8\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code>&lt;strong&gt;<\/code>\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\u3002\u5bf9\u4e8e\u4efb\u610f\u6570\u5b57x\uff0c\u5e73\u65b9\u6839\u53ef\u4ee5\u901a\u8fc7<code>x &lt;\/strong&gt; 0.5<\/code>\u6765\u5f97\u5230\u3002\u4f8b\u5982\uff0c<code>16 ** 0.5<\/code>\u540c\u6837\u4f1a\u8fd4\u56de4.0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u4f1a\u5f15\u53d1<code>ValueError<\/code>\uff0c\u56e0\u4e3a\u5b9e\u6570\u8303\u56f4\u5185\u6ca1\u6709\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u5982\u679c\u60f3\u8981\u8ba1\u7b97\u8d1f\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u3002\u901a\u8fc7<code>cmath<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath.sqrt()<\/code>\u6765\u5904\u7406\u8d1f\u6570\u3002\u4f8b\u5982\uff0c<code>import cmath<\/code>\u548c<code>cmath.sqrt(-16)<\/code>\u5c06\u8fd4\u56de4j\uff0c\u8fd9\u662f\u4e00\u4e2a\u865a\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bf9\u6570\u5b57\u53d6\u5e73\u65b9\u6839\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528math\u5e93\u3001\u4f7f\u7528cmath\u5e93\u3001\u4f7f\u7528NumPy\u5e93\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1107817,"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\/1107810"}],"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=1107810"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107810\/revisions"}],"predecessor-version":[{"id":1107820,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107810\/revisions\/1107820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107817"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}