{"id":982655,"date":"2024-12-27T07:12:39","date_gmt":"2024-12-26T23:12:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982655.html"},"modified":"2024-12-27T07:12:41","modified_gmt":"2024-12-26T23:12:41","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%91%e4%b8%8b%e6%95%b4%e9%99%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982655.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5411\u4e0b\u6574\u9664"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211041\/148a3fd3-c19e-4fdf-8c6f-9baea8b631cb.webp\" alt=\"python\u4e2d\u5982\u4f55\u5411\u4e0b\u6574\u9664\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u5b9e\u73b0\u3001\u8be5\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5bf9\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u6574\u9664\uff0c\u5e76\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\u3001\u5373\u4f7f\u5546\u4e3a\u8d1f\u6570\uff0c\u4e5f\u4f1a\u5411\u4e0b\u53d6\u6574\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5411\u4e0b\u6574\u9664\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5e38\u7528\u4e8e\u9700\u8981\u5c06\u7ed3\u679c\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u6216\u5904\u7406\u7d22\u5f15\u8ba1\u7b97\u7684\u573a\u666f\u3002\u5411\u4e0b\u6574\u9664\u7684\u884c\u4e3a\u4e0d\u540c\u4e8e\u666e\u901a\u7684\u9664\u6cd5\u8fd0\u7b97\uff0c\u56e0\u4e3a\u5b83\u603b\u662f\u4f1a\u5411\u8d1f\u65e0\u7a77\u53d6\u6574\uff0c\u800c\u4e0d\u662f\u5411\u96f6\u53d6\u6574\u3002\u4f8b\u5982\uff0c-7 \/\/ 3\u7684\u7ed3\u679c\u662f-3\uff0c\u800c\u4e0d\u662f-2\u3002\u8fd9\u79cd\u884c\u4e3a\u53ef\u4ee5\u5728\u5904\u7406\u8d1f\u6570\u65f6\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u662f\u4e00\u79cd\u4fdd\u8bc1\u7ed3\u679c\u4e00\u81f4\u6027\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u4e2d\u7684\u5411\u4e0b\u6574\u9664<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u7b26\u201c\/\/\u201d\u7528\u4e8e\u5bf9\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u6574\u9664\u5e76\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\u3002\u8be5\u8fd0\u7b97\u7b26\u9002\u7528\u4e8e\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u5e76\u4e14\u5728\u8fdb\u884c\u8d1f\u6570\u8fd0\u7b97\u65f6\u4f1a\u81ea\u52a8\u5411\u4e0b\u53d6\u6574\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u5546\u662f\u8d1f\u6570\uff0c\u7ed3\u679c\u4e5f\u4f1a\u5411\u8d1f\u65e0\u7a77\u53d6\u6574\uff0c\u800c\u4e0d\u662f\u5411\u96f6\u53d6\u6574\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 7 \/\/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2<\/p>\n<p>result = -7 \/\/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a-3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u5411\u4e0b\u53d6\u6574\u7684\u7279\u6027\u4f7f\u5f97\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u5728\u5904\u7406\u7d22\u5f15\u8ba1\u7b97\u3001\u65f6\u95f4\u8ba1\u7b97\u548c\u5176\u4ed6\u9700\u8981\u6574\u6570\u7ed3\u679c\u7684\u573a\u5408\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5411\u4e0b\u6574\u9664\u4e0e\u666e\u901a\u9664\u6cd5\u7684\u533a\u522b<\/p>\n<\/p>\n<p><p>\u666e\u901a\u9664\u6cd5\u8fd0\u7b97\u7b26\u201c\/\u201d\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u6570\u7ed3\u679c\uff0c\u800c\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u7b26\u201c\/\/\u201d\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed3\u679c\u3002\u666e\u901a\u9664\u6cd5\u603b\u662f\u7cbe\u786e\u8ba1\u7b97\u5546\uff0c\u5373\u4f7f\u5546\u662f\u4e00\u4e2a\u5c0f\u6570\u3002\u7136\u800c\uff0c\u5411\u4e0b\u6574\u9664\u603b\u662f\u4f1a\u5411\u4e0b\u53d6\u6574\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 7 \/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2.3333333333333335<\/p>\n<p>result = 7 \/\/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5411\u4e0b\u6574\u9664\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8d1f\u6570\u65f6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = -7 \/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a-2.3333333333333335<\/p>\n<p>result = -7 \/\/ 3<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a-3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5411\u4e0b\u6574\u9664\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7d22\u5f15\u8ba1\u7b97<\/strong>\uff1a\u5728\u5904\u7406\u6570\u7ec4\u548c\u5217\u8868\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u8ba1\u7b97\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u5206\u6210n\u7b49\u5206\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5411\u4e0b\u6574\u9664\u6765\u786e\u5b9a\u6bcf\u6bb5\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65f6\u95f4\u8ba1\u7b97<\/strong>\uff1a\u5728\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u5b8c\u6574\u7684\u5c0f\u65f6\u3001\u5929\u6570\u6216\u5468\u6570\u7b49\u3002\u4f8b\u5982\uff0c\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u65f6\uff0c\u4f7f\u7528\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u5c0f\u65f6\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u9875\u529f\u80fd<\/strong>\uff1a\u5728\u5b9e\u73b0\u5206\u9875\u529f\u80fd\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u5e2e\u52a9\u8ba1\u7b97\u603b\u9875\u6570\u3002\u4f8b\u5982\uff0c\u5f53\u6bcf\u9875\u663e\u793a10\u6761\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5411\u4e0b\u6574\u9664\u6765\u8ba1\u7b97\u603b\u9875\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5411\u4e0b\u6574\u9664\u4e0e\u6570\u5b66\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684math\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u4e0e\u5411\u4e0b\u6574\u9664\u76f8\u5173\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cmath.floor()\u53ef\u4ee5\u7528\u4e8e\u5411\u4e0b\u53d6\u6574\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\u867d\u7136math.floor()\u4e0e\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u4f46\u5b83\u66f4\u9002\u5408\u7528\u4e8e\u5904\u7406\u6d6e\u70b9\u6570\u7684\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.floor(7 \/ 3)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2<\/p>\n<p>result = math.floor(-7 \/ 3)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a-3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5411\u4e0b\u6574\u9664\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8d1f\u6570\u8fd0\u7b97<\/strong>\uff1a\u5728\u5904\u7406\u8d1f\u6570\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0e\u9884\u671f\u4e0d\u540c\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u5b83\u603b\u662f\u5411\u8d1f\u65e0\u7a77\u53d6\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u7ed3\u679c\u7c7b\u578b\u4e0e\u8f93\u5165\u7c7b\u578b\u4e00\u81f4\u3002\u5982\u679c\u8f93\u5165\u662f\u6574\u6570\uff0c\u7ed3\u679c\u4e5f\u662f\u6574\u6570\uff1b\u5982\u679c\u8f93\u5165\u662f\u6d6e\u70b9\u6570\uff0c\u7ed3\u679c\u4e5f\u662f\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6574\u9664\u8fd0\u7b97\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u6bd4\u666e\u901a\u9664\u6cd5\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u6d6e\u70b9\u6570\u7684\u8ba1\u7b97\u548c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5411\u4e0b\u6574\u9664\u7684\u6269\u5c55\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5411\u91cf\u5316\u8ba1\u7b97<\/strong>\uff1a\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5411\u91cf\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u6574\u6570\u7ed3\u679c\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e38\u620f\u5f00\u53d1<\/strong>\uff1a\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5750\u6807\u548c\u7f51\u683c\u8ba1\u7b97\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06\u6d6e\u70b9\u6570\u5750\u6807\u8f6c\u6362\u4e3a\u6574\u6570\u7f51\u683c\u5750\u6807\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u95f4\u6bb5\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5411\u4e0b\u6574\u9664\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u9700\u8981\u6574\u6570\u7ed3\u679c\u7684\u8ba1\u7b97\u573a\u666f\u3002\u901a\u8fc7\u7406\u89e3\u548c\u6b63\u786e\u4f7f\u7528\u5411\u4e0b\u6574\u9664\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5904\u7406\u7d22\u5f15\u8ba1\u7b97\u3001\u65f6\u95f4\u8ba1\u7b97\u548c\u5176\u4ed6\u9700\u8981\u6574\u6570\u7ed3\u679c\u7684\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u7406\u89e3\u5411\u4e0b\u6574\u9664\u4e0e\u666e\u901a\u9664\u6cd5\u7684\u533a\u522b\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8d1f\u6570\u65f6\u3002\u65e0\u8bba\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6e38\u620f\u5f00\u53d1\u8fd8\u662f\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ef\u9760\u7684\u65b9\u6cd5\u6765\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5411\u4e0b\u6574\u9664\u65f6\uff0c\u4f7f\u7528\u7684\u8fd0\u7b97\u7b26\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u659c\u6760\u8fd0\u7b97\u7b26\uff08\/\/\uff09\u8fdb\u884c\u5411\u4e0b\u6574\u9664\u3002\u8fd9\u79cd\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u4e0d\u5927\u4e8e\u7ed3\u679c\u7684\u6700\u5927\u6574\u6570\u3002\u4f8b\u5982\uff0c5 \/\/ 2 \u7684\u7ed3\u679c\u662f2\uff0c\u800c -5 \/\/ 2 \u7684\u7ed3\u679c\u662f-3\u3002<\/p>\n<p><strong>\u5411\u4e0b\u6574\u9664\u4e0e\u666e\u901a\u9664\u6cd5\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>\u5411\u4e0b\u6574\u9664\u4e0e\u666e\u901a\u9664\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u7ed3\u679c\u7684\u7c7b\u578b\u548c\u53d6\u503c\u3002\u666e\u901a\u9664\u6cd5\uff08\/\uff09\u8fd4\u56de\u6d6e\u70b9\u6570\uff0c\u800c\u5411\u4e0b\u6574\u9664\uff08\/\/\uff09\u8fd4\u56de\u6574\u6570\uff0c\u5e76\u4e14\u603b\u662f\u5411\u4e0b\u53d6\u6574\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u662f\u8d1f\u6570\u7684\u7ed3\u679c\u4e5f\u4f1a\u5411\u66f4\u5c0f\u7684\u6574\u6570\u65b9\u5411\u53d6\u6574\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u5411\u4e0b\u6574\u9664\uff1f<\/strong><br \/>\u5904\u7406\u8d1f\u6570\u65f6\uff0c\u5411\u4e0b\u6574\u9664\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u8ba9\u4e00\u4e9b\u7528\u6237\u611f\u5230\u56f0\u60d1\u3002\u5bf9\u4e8e\u8d1f\u6570\uff0c\u5411\u4e0b\u6574\u9664\u7684\u7ed3\u679c\u662f\u5411\u8d1f\u65e0\u7a77\u5927\u65b9\u5411\u53d6\u6574\u3002\u4f8b\u5982\uff0c-3 \/\/ 2 \u7684\u7ed3\u679c\u662f-2\uff0c\u800c\u4e0d\u662f-1\u3002\u8fd9\u79cd\u884c\u4e3a\u7b26\u5408\u6570\u5b66\u4e2d\u201c\u5411\u4e0b\u201d\u6574\u9664\u7684\u5b9a\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5411\u4e0b\u6574\u9664\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\/\/\u201d\u8fd0\u7b97\u7b26\u5b9e\u73b0\u3001\u8be5\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5bf9\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u6574\u9664\uff0c\u5e76\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\u3001 [&hellip;]","protected":false},"author":3,"featured_media":982661,"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\/982655"}],"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=982655"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982655\/revisions"}],"predecessor-version":[{"id":982663,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982655\/revisions\/982663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982661"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}