{"id":1161148,"date":"2025-01-13T19:13:07","date_gmt":"2025-01-13T11:13:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161148.html"},"modified":"2025-01-13T19:13:10","modified_gmt":"2025-01-13T11:13:10","slug":"python2-7%e5%a6%82%e4%bd%95%e7%ae%97%e6%a8%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161148.html","title":{"rendered":"python2.7\u5982\u4f55\u7b97\u6a21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202511\/e8bb18c8-976c-44f9-ac46-406689429aa2.webp\" alt=\"python2.7\u5982\u4f55\u7b97\u6a21\" \/><\/p>\n<p><p> \u5728Python 2.7\u4e2d\u8ba1\u7b97\u6a21\uff08\u53d6\u4f59\u6570\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u64cd\u4f5c\u7b26\u3001divmod()\u51fd\u6570\u3001\u548cmath.fmod()\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09<\/strong>\u662f\u6700\u5e38\u7528\u7684\u53d6\u6a21\u65b9\u5f0f\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\u660e\u4e86\uff1a<code>a % b<\/code>\uff0c\u8868\u793aa\u9664\u4ee5b\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>result = a % b  # result is 1<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c10 \u9664\u4ee5 3 \u5546\u6570\u4e3a 3\uff0c\u4f59\u6570\u4e3a 1\uff0c\u6240\u4ee5 <code>10 % 3<\/code> \u7684\u7ed3\u679c\u662f 1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09\u5728Python\u4e2d\u662f\u6700\u5e38\u7528\u7684\u53d6\u6a21\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u80fd\u591f\u5904\u7406\u6574\u6570\uff0c\u8fd8\u80fd\u591f\u5904\u7406\u6d6e\u70b9\u6570\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u662f <code>a % b<\/code>\uff0c\u5176\u4e2da\u662f\u88ab\u9664\u6570\uff0cb\u662f\u9664\u6570\uff0c\u7ed3\u679c\u662fa\u9664\u4ee5b\u7684\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6574\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6574\u6570\u53d6\u6a21\uff0c\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u7684\u8ba1\u7b97\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 17<\/p>\n<p>b = 5<\/p>\n<p>result = a % b  # result is 2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c17\u9664\u4ee55\uff0c\u5546\u6570\u662f3\uff0c\u4f59\u6570\u662f2\uff0c\u6240\u4ee5 <code>17 % 5<\/code> \u7684\u7ed3\u679c\u662f2\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6d6e\u70b9\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u540c\u6837\u9002\u7528\u4e8e\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10.5<\/p>\n<p>b = 2.5<\/p>\n<p>result = a % b  # result is 0.5<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c10.5 \u9664\u4ee5 2.5\uff0c\u5546\u6570\u662f4\uff0c\u4f59\u6570\u662f0.5\uff0c\u6240\u4ee5 <code>10.5 % 2.5<\/code> \u7684\u7ed3\u679c\u662f0.5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001divmod() \u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>divmod()<\/strong> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u540c\u65f6\u8fd4\u56de\u5546\u6570\u548c\u4f59\u6570\u3002\u5176\u8bed\u6cd5\u662f <code>divmod(a, b)<\/code>\uff0c\u5176\u4e2da\u662f\u88ab\u9664\u6570\uff0cb\u662f\u9664\u6570\uff0c\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5305\u542b\u5546\u6570\u548c\u4f59\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6574\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 17<\/p>\n<p>b = 5<\/p>\n<p>result = divmod(a, b)  # result is (3, 2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>divmod(17, 5)<\/code> \u8fd4\u56de <code>(3, 2)<\/code>\uff0c\u5176\u4e2d3\u662f\u5546\u6570\uff0c2\u662f\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6d6e\u70b9\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>divmod() \u540c\u6837\u9002\u7528\u4e8e\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10.5<\/p>\n<p>b = 2.5<\/p>\n<p>result = divmod(a, b)  # result is (4.0, 0.5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>divmod(10.5, 2.5)<\/code> \u8fd4\u56de <code>(4.0, 0.5)<\/code>\uff0c\u5176\u4e2d4.0\u662f\u5546\u6570\uff0c0.5\u662f\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001math.fmod() \u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>math.fmod()<\/strong> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u6d6e\u70b9\u6570\u7684\u6a21\u3002\u5176\u8bed\u6cd5\u662f <code>math.fmod(a, b)<\/code>\uff0c\u5176\u4e2da\u662f\u88ab\u9664\u6570\uff0cb\u662f\u9664\u6570\uff0c\u8fd4\u56de\u503c\u662fa\u9664\u4ee5b\u7684\u4f59\u6570\u3002\u4e0e\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u4e0d\u540c\u7684\u662f\uff0cmath.fmod() \u59cb\u7ec8\u8fd4\u56de\u6d6e\u70b9\u6570\uff0c\u5373\u4f7f\u64cd\u4f5c\u6570\u662f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6574\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 17<\/p>\n<p>b = 5<\/p>\n<p>result = math.fmod(a, b)  # result is 2.0<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>math.fmod(17, 5)<\/code> \u8fd4\u56de <code>2.0<\/code>\uff0c\u5176\u4e2d2.0\u662f\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6d6e\u70b9\u6570\u53d6\u6a21<\/h4>\n<\/p>\n<p><p>math.fmod() \u4e3b\u8981\u7528\u4e8e\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 10.5<\/p>\n<p>b = 2.5<\/p>\n<p>result = math.fmod(a, b)  # result is 0.5<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>math.fmod(10.5, 2.5)<\/code> \u8fd4\u56de <code>0.5<\/code>\uff0c\u5176\u4e2d0.5\u662f\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 2.7\u4e2d\uff0c\u53d6\u6a21\u8fd0\u7b97\u6709\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4e3b\u8981\u5305\u62ec\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\uff08%\uff09\u3001divmod() \u51fd\u6570\u548c math.fmod() \u51fd\u6570\u3002<strong>\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6574\u6570\u548c\u6d6e\u70b9\u6570\u8ba1\u7b97\u573a\u666f\u3002<strong>divmod() \u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u540c\u65f6\u83b7\u53d6\u5546\u6570\u548c\u4f59\u6570\u7684\u4fbf\u6377\u65b9\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4e24\u4e2a\u503c\u7684\u573a\u666f\u3002\u800c <strong>math.fmod() \u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6d6e\u70b9\u6570\u7684\u53d6\u6a21\u8fd0\u7b97<\/strong>\uff0c\u63d0\u4f9b\u4e86\u4e0e\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u4e0d\u540c\u7684\u884c\u4e3a\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42<\/strong>\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548\u3002\u5982\u679c\u9700\u8981\u540c\u65f6\u83b7\u53d6\u5546\u6570\u548c\u4f59\u6570\uff0c\u53ef\u4ee5\u9009\u62e9 divmod() \u51fd\u6570\uff1b\u5982\u679c\u9700\u8981\u5904\u7406\u6d6e\u70b9\u6570\u53d6\u6a21\u8fd0\u7b97\u4e14\u9700\u8981\u66f4\u9ad8\u7684\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u9009\u62e9 math.fmod() \u51fd\u6570\u3002\u4e86\u89e3\u5e76\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python 2.7\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u6a21\u8fd0\u7b97\u7b26\uff1f<\/strong><br \/>\u5728Python 2.7\u4e2d\uff0c\u6a21\u8fd0\u7b97\u7b26\u662f\u201c%\u201d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c<code>result = 10 % 3<\/code>\u7684\u7ed3\u679c\u662f1\uff0c\u56e0\u4e3a10\u9664\u4ee53\u7684\u4f59\u6570\u662f1\u3002<\/p>\n<p><strong>\u6a21\u8fd0\u7b97\u5728Python 2.7\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u6a21\u8fd0\u7b97\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u5076\u6570\u6216\u5947\u6570\u3002\u901a\u8fc7\u4f7f\u7528<code>if num % 2 == 0<\/code>\u53ef\u4ee5\u5224\u65ad<code>num<\/code>\u662f\u5426\u4e3a\u5076\u6570\u3002\u6b64\u5916\uff0c\u6a21\u8fd0\u7b97\u8fd8\u5e38\u7528\u4e8e\u5faa\u73af\u7ed3\u6784\u4e2d\uff0c\u6bd4\u5982\u5728\u5904\u7406\u6570\u7ec4\u7d22\u5f15\u65f6\uff0c\u786e\u4fdd\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python 2.7\u4e2d\u5bf9\u8d1f\u6570\u8fdb\u884c\u6a21\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python 2.7\u4e2d\uff0c\u5bf9\u8d1f\u6570\u8fdb\u884c\u6a21\u8fd0\u7b97\u65f6\uff0c\u7ed3\u679c\u7684\u7b26\u53f7\u53d6\u51b3\u4e8e\u88ab\u9664\u6570\u3002\u4f8b\u5982\uff0c<code>-10 % 3<\/code>\u7684\u7ed3\u679c\u662f2\uff0c\u56e0\u4e3a-10\u88ab3\u6574\u9664\u540e\uff0c\u4f59\u6570\u4e3a2\uff08-10 + 12 = 2\uff09\u3002\u8fd9\u79cd\u7279\u6027\u5728\u5904\u7406\u8d1f\u6570\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u4ee5\u786e\u4fdd\u7ed3\u679c\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 2.7\u4e2d\u8ba1\u7b97\u6a21\uff08\u53d6\u4f59\u6570\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u64cd\u4f5c\u7b26\u3001divmod()\u51fd\u6570\u3001\u548c [&hellip;]","protected":false},"author":3,"featured_media":1161158,"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\/1161148"}],"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=1161148"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161148\/revisions"}],"predecessor-version":[{"id":1161162,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161148\/revisions\/1161162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161158"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}