{"id":984922,"date":"2024-12-27T07:32:21","date_gmt":"2024-12-26T23:32:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984922.html"},"modified":"2024-12-27T07:32:24","modified_gmt":"2024-12-26T23:32:24","slug":"python%e5%a6%82%e4%bd%95%e9%99%a4%e6%b3%95%e5%8f%96%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/984922.html","title":{"rendered":"python\u5982\u4f55\u9664\u6cd5\u53d6\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212323\/73812081-ee80-4101-9066-40ceacdc6ee4.webp\" alt=\"python\u5982\u4f55\u9664\u6cd5\u53d6\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8fdb\u884c\u9664\u6cd5\u53d6\u6574\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u3001int()\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3001math.floor()\u65b9\u6cd5\u8fdb\u884c\u5411\u4e0b\u53d6\u6574\u3002<\/strong> \u5176\u4e2d\uff0c\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u7684\u6574\u6570\u9664\u6cd5\u8fd0\u7b97\u7b26\uff0c\u80fd\u591f\u8fd4\u56de\u9664\u6cd5\u7ed3\u679c\u7684\u6574\u6570\u90e8\u5206\u3002<strong>\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\u3002<\/strong> \u901a\u8fc7\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u4e24\u4e2a\u6570\u76f8\u9664\u540e\u7684\u5546\u7684\u6574\u6570\u90e8\u5206\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u201c\/\/\u201d\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u7528\u4e8e\u6267\u884c\u6574\u6570\u9664\u6cd5\u3002\u5b83\u8fd4\u56de\u9664\u6cd5\u7ed3\u679c\u7684\u6574\u6570\u90e8\u5206\uff0c\u800c\u820d\u5f03\u5c0f\u6570\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 7 \/\/ 2<\/p>\n<p>print(result)  # \u8f93\u51fa\u4e3a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4f8b\u4e2d\uff0c7\u9664\u4ee52\u5f97\u5230\u7684\u5546\u4e3a3.5\uff0c\u800c\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u540e\uff0c\u7ed3\u679c\u5219\u4e3a3\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u4e5f\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001int()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u83b7\u53d6\u9664\u6cd5\u6574\u6570\u7ed3\u679c\u7684\u65b9\u6cd5\u662f\u901a\u8fc7int()\u51fd\u6570\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u76f4\u63a5\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u66f4\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = int(7 \/ 2)<\/p>\n<p>print(result)  # \u8f93\u51fa\u4e3a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c7\u9664\u4ee52\u7684\u7ed3\u679c\u662f3.5\uff0c\u901a\u8fc7\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\uff0c\u6211\u4eec\u5f97\u5230\u4e863\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u66f4\u597d\u5730\u7ed3\u5408\u4f7f\u7528\uff0c\u5982\u5f53\u4f60\u5e0c\u671b\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001math.floor()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u8981\u83b7\u5f97\u66f4\u7075\u6d3b\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684floor()\u65b9\u6cd5\u3002\u5b83\u4f1a\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5927\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.floor(7 \/ 2)<\/p>\n<p>print(result)  # \u8f93\u51fa\u4e3a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>math.floor()\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u8d1f\u6570\u5e76\u8fd4\u56de\u66f4\u63a5\u8fd1\u8d1f\u65e0\u7a77\u7684\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4e25\u683c\u63a7\u5236\u5411\u4e0b\u53d6\u6574\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u76f8\u5173\u65b9\u6cd5<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528divmod()\u51fd\u6570<\/strong>\uff1adivmod()\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u8fd4\u56de\u5546\u548c\u4f59\u6570\uff0c\u8fd9\u5728\u67d0\u4e9b\u9700\u8981\u540c\u65f6\u83b7\u53d6\u8fd9\u4e24\u4e2a\u503c\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">quotient, rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nder = divmod(7, 2)<\/p>\n<p>print(quotient)  # \u8f93\u51fa\u4e3a3<\/p>\n<p>print(remainder)  # \u8f93\u51fa\u4e3a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528numpy\u5e93<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u6216\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u53ef\u80fd\u4f1a\u7528\u5230numpy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86floor_divide\u51fd\u6570\u6765\u8fdb\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>result = np.floor_divide(7, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa\u4e3a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6027\u80fd\u548c\u6548\u7387<\/p>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u65b9\u9762\uff0c\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u6700\u5feb\u7684\uff0c\u56e0\u4e3a\u5b83\u662fPython\u8bed\u8a00\u5185\u7f6e\u7684\u57fa\u672c\u64cd\u4f5c\u3002int()\u51fd\u6570\u53ef\u80fd\u4f1a\u7a0d\u5fae\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5230\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u3002math.floor()\u7531\u4e8e\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\uff0c\u56e0\u6b64\u5728\u6267\u884c\u901f\u5ea6\u4e0a\u4e5f\u53ef\u80fd\u4f1a\u7a0d\u6162\u3002\u4e0d\u8fc7\uff0c\u5728\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u8fd9\u4e9b\u6027\u80fd\u5dee\u5f02\u662f\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\u7684\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5faa\u73af\u8fed\u4ee3<\/strong>\uff1a\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u9664\u6cd5\u53d6\u6574\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u6b65\u957f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u53ef\u4ee5\u786e\u4fdd\u5faa\u73af\u53d8\u91cf\u6bcf\u6b21\u9012\u589e\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u9875\u5904\u7406<\/strong>\uff1a\u5728\u6570\u636e\u5206\u9875\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u8ba1\u7b97\u603b\u9875\u6570\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\u9664\u6cd5\u6765\u83b7\u53d6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d44\u6e90\u5206\u914d<\/strong>\uff1a\u5728\u9700\u8981\u5c06\u8d44\u6e90\u5e73\u5747\u5206\u914d\u7ed9\u591a\u4e2a\u5b9e\u4f53\u65f6\uff0c\u6574\u6570\u9664\u6cd5\u80fd\u591f\u786e\u4fdd\u5206\u914d\u7684\u516c\u5e73\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8d1f\u6570\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u8d1f\u6570\uff0c\u9664\u6cd5\u53d6\u6574\u7684\u7ed3\u679c\u53ef\u80fd\u4e0e\u9884\u671f\u4e0d\u540c\u3002\u5728Python\u4e2d\uff0c\u8d1f\u6570\u7684\u6574\u6570\u9664\u6cd5\u7ed3\u679c\u603b\u662f\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong>\uff1a\u5728\u8fdb\u884c\u6d6e\u70b9\u6570\u8fd0\u7b97\u65f6\uff0c\u8981\u6ce8\u610f\u53ef\u80fd\u7684\u7cbe\u5ea6\u635f\u5931\uff0c\u7279\u522b\u662f\u5728\u540e\u7eed\u8ba1\u7b97\u4e2d\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u7ed3\u679c\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7c7b\u578b\u8f6c\u6362\u6216\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u9664\u6cd5\u53d6\u6574\u65b9\u5f0f\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6574\u6570\u9664\u6cd5\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u573a\u666f\uff0cPython\u63d0\u4f9b\u7684\u5de5\u5177\u90fd\u80fd\u591f\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8fdb\u884c\u6574\u6570\u9664\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u53cc\u659c\u6760\uff08\/\/\uff09\u8fd0\u7b97\u7b26\u8fdb\u884c\u6574\u6570\u9664\u6cd5\u3002\u8fd9\u79cd\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\uff0c\u820d\u53bb\u5c0f\u6570\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>5 \/\/ 2<\/code>\u7684\u7ed3\u679c\u662f<code>2<\/code>\uff0c\u800c<code>7 \/\/ 3<\/code>\u7684\u7ed3\u679c\u662f<code>2<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u9700\u8981\u53ea\u83b7\u53d6\u6574\u6570\u7ed3\u679c\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5e0c\u671b\u540c\u65f6\u83b7\u5f97\u6574\u6570\u548c\u5c0f\u6570\u90e8\u5206\uff0c\u8be5\u5982\u4f55\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u666e\u901a\u7684\u9664\u6cd5\u8fd0\u7b97\u7b26\uff08\/\uff09\u6765\u83b7\u5f97\u6d6e\u70b9\u6570\u7ed3\u679c\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u63d0\u53d6\u6574\u6570\u90e8\u5206\uff0c\u540c\u65f6\u4fdd\u7559\u5c0f\u6570\u90e8\u5206\u3002\u6bd4\u5982\uff0c<code>result = 7 \/ 3<\/code>\u5c06\u8fd4\u56de<code>2.3333...<\/code>\uff0c\u800c<code>int(result)<\/code>\u5c06\u8fd4\u56de<code>2<\/code>\uff0c\u8fd9\u6837\u7684\u65b9\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u6574\u6570\u9664\u6cd5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8d1f\u6570\u7684\u6574\u6570\u9664\u6cd5\u65f6\uff0cPython\u7684\u884c\u4e3a\u4e0e\u4e00\u4e9b\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e0d\u540c\u3002\u4f7f\u7528\u53cc\u659c\u6760\uff08\/\/\uff09\u65f6\uff0c\u5b83\u4f1a\u5411\u4e0b\u53d6\u6574\u3002\u6bd4\u5982\uff0c<code>-7 \/\/ 3<\/code>\u7684\u7ed3\u679c\u662f<code>-3<\/code>\uff0c\u800c<code>-7 \/\/ -3<\/code>\u7684\u7ed3\u679c\u5219\u662f<code>2<\/code>\u3002\u4e86\u89e3\u8fd9\u4e00\u70b9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5904\u7406\u8d1f\u6570\u65f6\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u9664\u6cd5\u4e2d\u7684\u9664\u96f6\u9519\u8bef\uff1f<\/strong><br \/>\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c\u9664\u96f6\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u5728\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u524d\u5148\u68c0\u67e5\u9664\u6570\u662f\u5426\u4e3a\u96f6\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u5224\u65ad\uff0c\u4f8b\u5982<code>if divisor != 0:<\/code>\u6765\u786e\u4fdd\u9664\u6570\u4e0d\u4e3a\u96f6\uff0c\u8fdb\u800c\u5b89\u5168\u5730\u6267\u884c\u9664\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6837\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8fdb\u884c\u9664\u6cd5\u53d6\u6574\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u3001int()\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3001math.floor() [&hellip;]","protected":false},"author":3,"featured_media":984932,"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\/984922"}],"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=984922"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984922\/revisions"}],"predecessor-version":[{"id":984934,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984922\/revisions\/984934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/984932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=984922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=984922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=984922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}