{"id":952154,"date":"2024-12-27T01:21:33","date_gmt":"2024-12-26T17:21:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/952154.html"},"modified":"2024-12-27T01:21:35","modified_gmt":"2024-12-26T17:21:35","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e7%ab%8b%e6%96%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/952154.html","title":{"rendered":"python\u5982\u4f55\u6253\u7acb\u65b9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090029\/bf2b9aad-ea27-4272-8f59-833cc2bf8695.webp\" alt=\"python\u5982\u4f55\u6253\u7acb\u65b9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba1\u7b97\u7acb\u65b9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08<\/strong>\uff09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570pow()\u3001\u4f7f\u7528numpy\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u8ba1\u7b97\u7acb\u65b9\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08<strong>\uff09\u662fPython\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u53ea\u9700\u8981\u5c06\u6570\u5b57\u4e0e3\u76f8\u8fde\u63a5\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>x &lt;\/strong&gt; 3<\/code>\u5c31\u80fd\u8ba1\u7b97x\u7684\u7acb\u65b9\u3002\u5728\u5904\u7406\u7b80\u5355\u6570\u503c\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570pow()\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<code>pow(x, 3)<\/code>\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56dex\u7684\u7acb\u65b9\u3002pow\u51fd\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u80fd\u5904\u7406\u5927\u591a\u6570\u6570\u503c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u3002numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u9ad8\u6548\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u4f7f\u7528numpy\u7684<code>np.power()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u7acb\u65b9\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u5b9e\u73b0\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08\uff09<\/h3>\n<\/p>\n<p><p>\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u8fd0\u7b97\u7b26\u4e4b\u4e00\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684n\u6b21\u65b9\u3002\u8ba1\u7b97\u7acb\u65b9\u53ea\u9700\u5c06n\u8bbe\u4e3a3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>x = 5<\/p>\n<p>result = x  3<\/p>\n<p>print(&quot;5\u7684\u7acb\u65b9\u662f\uff1a&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u7b80\u5355\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u503c\u8fd0\u7b97\u3002\u4e0d\u8fc7\uff0c\u5f53\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6548\u7387\u53ef\u80fd\u4e0d\u5982\u4e13\u7528\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570pow()<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570pow()\uff0c\u7528\u4e8e\u8ba1\u7b97\u6307\u6570\u8fd0\u7b97\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u4f7f\u7528\u975e\u5e38\u65b9\u4fbf\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u8ba1\u7b97\u590d\u6742\u7684\u6307\u6570\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>x = 5<\/p>\n<p>result = pow(x, 3)<\/p>\n<p>print(&quot;5\u7684\u7acb\u65b9\u662f\uff1a&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pow()<\/code>\u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u4f7f\u7528\u65f6\u65e0\u9700\u5bfc\u5165\u4efb\u4f55\u5e93\u3002\u4e0e\u5e42\u8fd0\u7b97\u7b26\u76f8\u6bd4\uff0c<code>pow()<\/code>\u51fd\u6570\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u76f4\u89c2\uff0c\u7279\u522b\u662f\u5728\u590d\u6742\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u5927\u6570\u636e\u5904\u7406\uff0cnumpy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5355\u4e2a\u6570\u7684\u7acb\u65b9<\/strong><\/h2>\n<p>x = 5<\/p>\n<p>result = np.power(x, 3)<\/p>\n<p>print(&quot;5\u7684\u7acb\u65b9\u662f\uff1a&quot;, result)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u7acb\u65b9<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>result_arr = np.power(arr, 3)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u7acb\u65b9\u662f\uff1a&quot;, result_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u5b66\u8fd0\u7b97\u7684\u573a\u666f\uff0c\u5982<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3001\u6570\u636e\u5206\u6790\u7b49\uff0cnumpy\u662f\u4e00\u4e2a\u975e\u5e38\u5408\u9002\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0cnumpy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u8ba1\u7b97\u7acb\u65b9\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b80\u5355\u6570\u503c\u8fd0\u7b97<\/strong>\uff1a\u5982\u679c\u53ea\u662f\u8fdb\u884c\u7b80\u5355\u7684\u6570\u503c\u8fd0\u7b97\uff0c\u4f8b\u5982\u5bf9\u5355\u4e2a\u6570\u503c\u8fdb\u884c\u7acb\u65b9\u8ba1\u7b97\uff0c\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u6216pow()\u51fd\u6570\u90fd\u662f\u975e\u5e38\u5408\u9002\u7684\u9009\u62e9\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u8bed\u6cd5\u76f4\u89c2\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u8ba1\u7b97\u6216\u5927\u6570\u636e\u5904\u7406<\/strong>\uff1a\u5f53\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u65f6\uff0cnumpy\u5e93\u662f\u9996\u9009\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u6570\u7ec4\u7684\u7acb\u65b9\uff0c\u8fd8\u80fd\u591f\u5229\u7528\u5176\u5f3a\u5927\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u53ef\u80fd\u662f\u9996\u8981\u8003\u8651\u56e0\u7d20\u3002\u6b64\u65f6\uff0c\u5e94\u9009\u62e9\u6700\u5bb9\u6613\u7406\u89e3\u7684\u8bed\u6cd5\u3002\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\u6216\u811a\u672c\uff0c\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7acb\u65b9\u8ba1\u7b97\u65f6\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u548cpow()\u51fd\u6570\u5728\u5904\u7406\u5355\u4e2a\u6570\u503c\u65f6\u6027\u80fd\u76f8\u5f53\uff0c\u800cnumpy\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u66f4\u4e3a\u4f18\u5f02\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0b\u5b9e\u9a8c\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>\u4f7f\u7528\u8ba1\u7b97\u5355\u4e2a\u6570\u7684\u7acb\u65b9<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>x = 1000000<\/p>\n<p>for _ in range(1000000):<\/p>\n<p>    result = x  3<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u8017\u65f6\uff1a&quot;, end_time - start_time)<\/p>\n<h2><strong>\u4f7f\u7528pow()\u8ba1\u7b97\u5355\u4e2a\u6570\u7684\u7acb\u65b9<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>for _ in range(1000000):<\/p>\n<p>    result = pow(x, 3)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;pow()\u51fd\u6570\u8017\u65f6\uff1a&quot;, end_time - start_time)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u8ba1\u7b97\u6570\u7ec4\u7684\u7acb\u65b9<\/strong><\/h2>\n<p>arr = np.array([x] * 1000000)<\/p>\n<p>start_time = time.time()<\/p>\n<p>result_arr = np.power(arr, 3)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;numpy\u8ba1\u7b97\u8017\u65f6\uff1a&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5bf9\u4e8e\u5355\u4e2a\u6570\u503c\u7684\u91cd\u590d\u8ba1\u7b97\uff0c\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u548cpow()\u51fd\u6570\u7684\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\uff1b\u800c\u5f53\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cnumpy\u7684\u6027\u80fd\u4f18\u52bf\u663e\u800c\u6613\u89c1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u7acb\u65b9\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u548cpow()\u51fd\u6570\u9002\u5408\u7b80\u5355\u7684\u6570\u503c\u8fd0\u7b97\uff0cnumpy\u5219\u9002\u5408\u5927\u6570\u636e\u5904\u7406\u548c\u590d\u6742\u8ba1\u7b97\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u7684\u9009\u62e9\u548c\u4f18\u5316\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u5b9e\u73b0\u6700\u4f73\u7684\u6027\u80fd\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7acb\u65b9\u4f53\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u7acb\u65b9\u4f53\uff0c\u4f8b\u5982Matplotlib\u548cPyOpenGL\u3002\u4f7f\u7528Matplotlib\u76843D\u7ed8\u56fe\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7ed8\u5236\u4e00\u4e2a\u7acb\u65b9\u4f53\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d.art3d import Poly3DCollection\n\nfig = plt.figure()\nax = fig.add_subplot(111, projection=&#39;3d&#39;)\n\n# \u5b9a\u4e49\u7acb\u65b9\u4f53\u7684\u9876\u70b9\nvertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],\n            [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]]\n\n# \u5b9a\u4e49\u7acb\u65b9\u4f53\u7684\u9762\nfaces = [[vertices[j] for j in [0, 1, 2, 3]],\n         [vertices[j] for j in [4, 5, 6, 7]],\n         [vertices[j] for j in [0, 1, 5, 4]],\n         [vertices[j] for j in [2, 3, 7, 6]],\n         [vertices[j] for j in [1, 2, 6, 5]],\n         [vertices[j] for j in [4, 7, 3, 0]]]\n\n# \u7ed8\u5236\u7acb\u65b9\u4f53\nax.add_collection3d(Poly3DCollection(faces, facecolors=&#39;cyan&#39;, linewidths=1, edgecolors=&#39;r&#39;, alpha=.25))\nax.set_xlabel(&#39;X axis&#39;)\nax.set_ylabel(&#39;Y axis&#39;)\nax.set_zlabel(&#39;Z axis&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u7acb\u65b9\u4f53\u7684\u4f53\u79ef\u548c\u8868\u9762\u79ef\uff1f<\/strong><br \/>\u4f53\u79ef\u548c\u8868\u9762\u79ef\u7684\u8ba1\u7b97\u76f8\u5bf9\u7b80\u5355\u3002\u5bf9\u4e8e\u8fb9\u957f\u4e3a<code>a<\/code>\u7684\u7acb\u65b9\u4f53\uff0c\u4f53\u79ef\u53ef\u4ee5\u901a\u8fc7\u516c\u5f0f<code>V = a^3<\/code>\u6765\u8ba1\u7b97\uff0c\u800c\u8868\u9762\u79ef\u53ef\u4ee5\u901a\u8fc7\u516c\u5f0f<code>A = 6 * a^2<\/code>\u6765\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def cube_volume(side_length):\n    return side_length ** 3\n\ndef cube_surface_area(side_length):\n    return 6 * (side_length ** 2)\n\nside = 3  # \u5047\u8bbe\u8fb9\u957f\u4e3a3\nprint(f&quot;\u7acb\u65b9\u4f53\u7684\u4f53\u79ef\u662f: {cube_volume(side)}&quot;)\nprint(f&quot;\u7acb\u65b9\u4f53\u7684\u8868\u9762\u79ef\u662f: {cube_surface_area(side)}&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u4f7f\u7528\u7c7b\u6765\u8868\u793a\u7acb\u65b9\u4f53\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u7acb\u65b9\u4f53\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ec\u8fb9\u957f\u3001\u8ba1\u7b97\u4f53\u79ef\u548c\u8868\u9762\u79ef\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">class Cube:\n    def __init__(self, side_length):\n        self.side_length = side_length\n\n    def volume(self):\n        return self.side_length ** 3\n\n    def surface_area(self):\n        return 6 * (self.side_length ** 2)\n\n# \u521b\u5efa\u7acb\u65b9\u4f53\u5bf9\u8c61\nmy_cube = Cube(4)\nprint(f&quot;\u7acb\u65b9\u4f53\u7684\u4f53\u79ef\u662f: {my_cube.volume()}&quot;)\nprint(f&quot;\u7acb\u65b9\u4f53\u7684\u8868\u9762\u79ef\u662f: {my_cube.surface_area()}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u8868\u793a\u548c\u64cd\u4f5c\u7acb\u65b9\u4f53\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba1\u7b97\u7acb\u65b9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\uff08\uff09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570pow()\u3001\u4f7f\u7528numpy\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6709 [&hellip;]","protected":false},"author":3,"featured_media":952157,"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\/952154"}],"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=952154"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952154\/revisions"}],"predecessor-version":[{"id":952159,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952154\/revisions\/952159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/952157"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=952154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=952154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=952154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}