{"id":1066032,"date":"2024-12-31T16:22:39","date_gmt":"2024-12-31T08:22:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066032.html"},"modified":"2024-12-31T16:22:42","modified_gmt":"2024-12-31T08:22:42","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e6%95%b0%e5%ad%a6%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066032.html","title":{"rendered":"python\u5982\u4f55\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b986ee64-c6a2-4a9c-a836-3830913794fe.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\" \/><\/p>\n<p><p> <strong>Python\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>NumPy<\/code>\u5e93\u3001\u4f7f\u7528<code>SymPy<\/code>\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u5185\u7f6e\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u7528\u4e8e\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\uff0c<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c<code>NumPy<\/code>\u5e93\u9002\u7528\u4e8e\u5927\u578b\u6570\u636e\u96c6\u548c\u6570\u7ec4\u8fd0\u7b97\uff0c<code>SymPy<\/code>\u5e93\u5219\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\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\u4f7f\u7528\u5185\u7f6e\u7684\u7b97\u672f\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u5305\u62ec\u52a0\u6cd5\uff08<code>+<\/code>\uff09\u3001\u51cf\u6cd5\uff08<code>-<\/code>\uff09\u3001\u4e58\u6cd5\uff08<code>*<\/code>\uff09\u3001\u9664\u6cd5\uff08<code>\/<\/code>\uff09\u3001\u6574\u9664\uff08<code>\/\/<\/code>\uff09\u3001\u53d6\u4f59\uff08<code>%<\/code>\uff09\u548c\u6307\u6570\uff08<code><\/code>\uff09\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u6570\u5b57\u4e4b\u95f4\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u6cd5<\/p>\n<p>a = 5 + 3<\/p>\n<p>print(a)  # \u8f93\u51fa\uff1a8<\/p>\n<h2><strong>\u51cf\u6cd5<\/strong><\/h2>\n<p>b = 10 - 2<\/p>\n<p>print(b)  # \u8f93\u51fa\uff1a8<\/p>\n<h2><strong>\u4e58\u6cd5<\/strong><\/h2>\n<p>c = 4 * 7<\/p>\n<p>print(c)  # \u8f93\u51fa\uff1a28<\/p>\n<h2><strong>\u9664\u6cd5<\/strong><\/h2>\n<p>d = 16 \/ 4<\/p>\n<p>print(d)  # \u8f93\u51fa\uff1a4.0<\/p>\n<h2><strong>\u6574\u9664<\/strong><\/h2>\n<p>e = 17 \/\/ 3<\/p>\n<p>print(e)  # \u8f93\u51fa\uff1a5<\/p>\n<h2><strong>\u53d6\u4f59<\/strong><\/h2>\n<p>f = 17 % 3<\/p>\n<p>print(f)  # \u8f93\u51fa\uff1a2<\/p>\n<h2><strong>\u6307\u6570<\/strong><\/h2>\n<p>g = 2  3<\/p>\n<p>print(g)  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>math<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u5982\u4e09\u89d2\u51fd\u6570\u3001\u5bf9\u6570\u3001\u5e42\u51fd\u6570\u7b49\u3002\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\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>h = math.sqrt(16)<\/p>\n<p>print(h)  # \u8f93\u51fa\uff1a4.0<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>i = math.log(2.71828)<\/p>\n<p>print(i)  # \u8f93\u51fa\uff1a1.0<\/p>\n<h2><strong>\u8ba1\u7b97\u5e42<\/strong><\/h2>\n<p>j = math.pow(2, 3)<\/p>\n<p>print(j)  # \u8f93\u51fa\uff1a8.0<\/p>\n<h2><strong>\u8ba1\u7b97\u4e09\u89d2\u51fd\u6570<\/strong><\/h2>\n<p>k = math.sin(math.pi \/ 2)<\/p>\n<p>print(k)  # \u8f93\u51fa\uff1a1.0<\/p>\n<h2><strong>\u5e38\u91cf<\/strong><\/h2>\n<p>pi_value = math.pi<\/p>\n<p>print(pi_value)  # \u8f93\u51fa\uff1a3.141592653589793<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>NumPy<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>NumPy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5b83\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u3002<code>NumPy<\/code>\u5e93\u975e\u5e38\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u6570\u7ec4\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2, 3, 4])<\/p>\n<p>array2 = np.array([5, 6, 7, 8])<\/p>\n<h2><strong>\u6570\u7ec4\u52a0\u6cd5<\/strong><\/h2>\n<p>sum_array = array1 + array2<\/p>\n<p>print(sum_array)  # \u8f93\u51fa\uff1a[ 6  8 10 12]<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>product_array = array1 * array2<\/p>\n<p>print(product_array)  # \u8f93\u51fa\uff1a[ 5 12 21 32]<\/p>\n<h2><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>matrix_product = np.dot(matrix1, matrix2)<\/p>\n<p>print(matrix_product)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>[[19 22]<\/strong><\/h2>\n<h2><strong> [43 50]]<\/strong><\/h2>\n<h2><strong>\u6570\u7ec4\u6c42\u548c<\/strong><\/h2>\n<p>total_sum = np.sum(array1)<\/p>\n<p>print(total_sum)  # \u8f93\u51fa\uff1a10<\/p>\n<h2><strong>\u6570\u7ec4\u5747\u503c<\/strong><\/h2>\n<p>mean_value = np.mean(array1)<\/p>\n<p>print(mean_value)  # \u8f93\u51fa\uff1a2.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>SymPy<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>SymPy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u548c\u4ee3\u6570\u64cd\u4f5c\u3002\u4f7f\u7528<code>SymPy<\/code>\u5e93\u53ef\u4ee5\u8fdb\u884c\u5fae\u79ef\u5206\u3001\u65b9\u7a0b\u6c42\u89e3\u7b49\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u7b26\u53f7\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>expr = x2 + 2*x + 1<\/p>\n<h2><strong>\u6c42\u5bfc<\/strong><\/h2>\n<p>derivative = sp.diff(expr, x)<\/p>\n<p>print(derivative)  # \u8f93\u51fa\uff1a2*x + 2<\/p>\n<h2><strong>\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expr, x)<\/p>\n<p>print(integral)  # \u8f93\u51fa\uff1ax&lt;strong&gt;3\/3 + x&lt;\/strong&gt;2 + x<\/p>\n<h2><strong>\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solution = sp.solve(expr, x)<\/p>\n<p>print(solution)  # \u8f93\u51fa\uff1a[-1]<\/p>\n<h2><strong>\u8ba1\u7b97\u8868\u8fbe\u5f0f\u503c<\/strong><\/h2>\n<p>value = expr.subs(x, 2)<\/p>\n<p>print(value)  # \u8f93\u51fa\uff1a9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5176\u4ed6\u6570\u5b66\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u5e93\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u5982<code>scipy<\/code>\u3001<code>pandas<\/code>\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u5f3a\u5927\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy as sp<\/p>\n<p>from scipy import integrate<\/p>\n<h2><strong>\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>def func(x):<\/p>\n<p>    return x2<\/p>\n<h2><strong>\u79ef\u5206\u8ba1\u7b97<\/strong><\/h2>\n<p>result, error = integrate.quad(func, 0, 1)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a0.3333333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u8fd0\u7b97\u652f\u6301\uff0c\u65e2\u6709\u5185\u7f6e\u7684\u57fa\u672c\u8fd0\u7b97\u7b26\uff0c\u4e5f\u6709\u529f\u80fd\u5f3a\u5927\u7684\u6570\u5b66\u5e93\uff0c\u5982<code>math<\/code>\u3001<code>NumPy<\/code>\u3001<code>SymPy<\/code>\u7b49\u3002<\/strong> \u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u4ece\u7b80\u5355\u7684\u7b97\u672f\u8fd0\u7b97\u5230\u590d\u6742\u7684\u7b26\u53f7\u8ba1\u7b97\u548c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u4efb\u52a1\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u652f\u6301\u54ea\u4e9b\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u8fdb\u884c\u591a\u79cd\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\uff08+\uff09\u3001\u51cf\u6cd5\uff08-\uff09\u3001\u4e58\u6cd5\uff08*\uff09\u3001\u9664\u6cd5\uff08\/\uff09\u3001\u53d6\u6574\u9664\uff08\/\/\uff09\u548c\u53d6\u4f59\uff08%\uff09\u3002\u6b64\u5916\uff0cPython\u8fd8\u652f\u6301\u5e42\u8fd0\u7b97\uff08**\uff09\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u51fd\u6570\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>math<\/code>\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u53ef\u4ee5\u6267\u884c\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u4f8b\u5982\u5e73\u65b9\u6839\u3001\u5bf9\u6570\u3001\u4e09\u89d2\u51fd\u6570\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import math<\/code>\u540e\uff0c\u53ef\u4ee5\u8c03\u7528<code>math.sqrt()<\/code>\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u6216\u4f7f\u7528<code>math.log()<\/code>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6d6e\u70b9\u6570\u8fd0\u7b97\u7684\u7cbe\u5ea6\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6d6e\u70b9\u6570\u8fd0\u7b97\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u95ee\u9898\uff0c\u8fd9\u662f\u56e0\u4e3a\u8ba1\u7b97\u673a\u5185\u90e8\u5bf9\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u5f0f\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u5341\u8fdb\u5236\u6d6e\u70b9\u6570\u7684\u66f4\u9ad8\u7cbe\u5ea6\u63a7\u5236\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u6570\u5b57\u7684\u5c0f\u6570\u4f4d\u6570\uff0c\u786e\u4fdd\u7ed3\u679c\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684math\u6a21\u5757\u3001\u4f7f\u7528NumP [&hellip;]","protected":false},"author":3,"featured_media":1066043,"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\/1066032"}],"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=1066032"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066032\/revisions"}],"predecessor-version":[{"id":1066044,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066032\/revisions\/1066044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066043"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}