{"id":963440,"date":"2024-12-27T04:18:04","date_gmt":"2024-12-26T20:18:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/963440.html"},"modified":"2024-12-27T04:18:06","modified_gmt":"2024-12-26T20:18:06","slug":"python%e4%b8%admod%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963440.html","title":{"rendered":"python\u4e2dmod\u5982\u4f55\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180733\/6535ee2f-f045-4c00-a836-73eeb5ca212e.webp\" alt=\"python\u4e2dmod\u5982\u4f55\u8f93\u5165\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8ba1\u7b97\u6a21\u6570\uff08modulus\uff09\u901a\u5e38\u4f7f\u7528\u767e\u5206\u53f7\u7b26\u53f7 <code>%<\/code> \u8fd0\u7b97\u7b26\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>a % b<\/code> \u5c06\u8fd4\u56de <code>a<\/code> \u9664\u4ee5 <code>b<\/code> \u7684\u4f59\u6570\u3002\u5bf9\u4e8e\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6a21\u6570\u8fd0\u7b97\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u8981\u7684\u56de\u7b54\uff1a<strong>\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u3001\u5bfc\u5165<code>divmod<\/code>\u51fd\u6570\u3001\u6ce8\u610f\u8d1f\u6570\u6a21\u8fd0\u7b97\u7684\u7ed3\u679c<\/strong>\u3002\u5176\u4e2d\uff0c<code>divmod<\/code>\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u5f97\u5230\u5546\u548c\u4f59\u6570\uff0c\u662f\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5728\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u4f59\u6570\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u5728Python\u4e2d\u88ab\u79f0\u4e3a\u6a21\u6570\u8fd0\u7b97\u7b26\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c06\u4e24\u4e2a\u6574\u6570\u4f5c\u4e3a\u64cd\u4f5c\u6570\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6574\u6570\u9664\u4ee5\u7b2c\u4e8c\u4e2a\u6574\u6570\u7684\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>b = 3<\/p>\n<p>result = a % b<\/p>\n<p>print(f&quot;The rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nder of {a} divided by {b} is {result}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>10 % 3<\/code> \u7684\u7ed3\u679c\u662f <code>1<\/code>\uff0c\u56e0\u4e3a <code>10<\/code> \u9664\u4ee5 <code>3<\/code> \u7b49\u4e8e <code>3<\/code> \u4f59 <code>1<\/code>\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u76f4\u89c2\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u975e\u5e38\u9002\u5408\u5904\u7406\u6574\u9664\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u8d1f\u6570\u6a21\u8fd0\u7b97<\/strong>\uff1a\u5f53\u6d89\u53ca\u5230\u8d1f\u6570\u65f6\uff0cPython\u4e2d\u7684\u6a21\u8fd0\u7b97\u7b26\u9075\u5faa\u4e0e\u6570\u5b66\u4e2d\u5b9a\u4e49\u4e00\u81f4\u7684\u89c4\u5219\u3002\u8d1f\u6570\u7684\u6a21\u8fd0\u7b97\u7ed3\u679c\u901a\u5e38\u4ecd\u7136\u662f\u4e00\u4e2a\u975e\u8d1f\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = -10<\/p>\n<p>b = 3<\/p>\n<p>result = a % b<\/p>\n<p>print(f&quot;The remainder of {a} divided by {b} is {result}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>-10 % 3<\/code> \u7684\u7ed3\u679c\u662f <code>2<\/code>\uff0c\u56e0\u4e3a <code>-10<\/code> \u9664\u4ee5 <code>3<\/code> \u5f97\u5230 <code>-4<\/code> \u4f59 <code>2<\/code>\u3002\u8fd9\u79cd\u7ed3\u679c\u53ef\u80fd\u4f1a\u4e0e\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u7684\u5904\u7406\u65b9\u5f0f\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u6d89\u53ca\u8d1f\u6570\u6a21\u8fd0\u7b97\u7684\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165<code>divmod<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>divmod()<\/code>\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u8fd4\u56de\u5546\u548c\u4f59\u6570\u3002\u8fd9\u5728\u9700\u8981\u540c\u65f6\u83b7\u53d6\u8fd9\u4e24\u4e2a\u503c\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u5e76\u51cf\u5c11\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>b = 3<\/p>\n<p>quotient, remainder = divmod(a, b)<\/p>\n<p>print(f&quot;The quotient of {a} divided by {b} is {quotient}, and the remainder is {remainder}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>divmod(10, 3)<\/code> \u8fd4\u56de <code>(3, 1)<\/code>\uff0c\u5176\u4e2d <code>3<\/code> \u662f\u5546\uff0c<code>1<\/code> \u662f\u4f59\u6570\u3002<code>divmod()<\/code> \u5728\u9700\u8981\u540c\u65f6\u8fdb\u884c\u6574\u9664\u548c\u53d6\u4f59\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e94\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5faa\u73af\u8ba1\u6570<\/strong>\uff1a\u5728\u9700\u8981\u5904\u7406\u5faa\u73af\u6b21\u6570\u65f6\uff0c\u4f7f\u7528\u6a21\u6570\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u5faa\u73af\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5faa\u73af\u5904\u7406\u67d0\u4e9b\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u6570\u8fd0\u7b97\u7b26\u5224\u65ad\u5f53\u524d\u5904\u7406\u7684\u662f\u7b2c\u51e0\u4e2a\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>for i in range(1, 11):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        print(f&quot;{i} is even.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;{i} is odd.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\uff0c\u901a\u8fc7<code>i % 2<\/code>\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5947\u6570\u8fd8\u662f\u5076\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u65f6\u95f4\u8ba1\u7b97<\/strong>\uff1a\u6a21\u6570\u8fd0\u7b97\u5728\u65f6\u95f4\u5904\u7406\u4e0a\u4e5f\u975e\u5e38\u6709\u6548\u3002\u4f8b\u5982\uff0c\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u65f6\uff0c\u6a21\u6570\u8fd0\u7b97\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6\u65f6\u95f4\u7684\u7ec4\u6210\u90e8\u5206\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>total_seconds = 3661<\/p>\n<p>hours = total_seconds \/\/ 3600<\/p>\n<p>remaining_seconds = total_seconds % 3600<\/p>\n<p>minutes = remaining_seconds \/\/ 60<\/p>\n<p>seconds = remaining_seconds % 60<\/p>\n<p>print(f&quot;{total_seconds} seconds is equal to {hours} hours, {minutes} minutes, and {seconds} seconds.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u6a21\u6570\u8fd0\u7b97\u7b26<code>%<\/code>\u63d0\u53d6\u51fa\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u7cbe\u786e\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6ce8\u610f\u8d1f\u6570\u6a21\u8fd0\u7b97\u7684\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8d1f\u6570\u65f6\uff0cPython\u7684\u6a21\u6570\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u4e00\u4e2a\u975e\u8d1f\u7684\u4f59\u6570\uff0c\u8fd9\u4e0e\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u7406\u89e3Python\u4e2d\u6a21\u8fd0\u7b97\u7684\u884c\u4e3a\u5bf9\u4e8e\u907f\u514d\u903b\u8f91\u9519\u8bef\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u793a\u4f8b<\/strong>\uff1a\u7406\u89e3\u8d1f\u6570\u6a21\u8fd0\u7b97\u7684\u89c4\u5219<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = -7<\/p>\n<p>b = 3<\/p>\n<p>result = a % b<\/p>\n<p>print(f&quot;The remainder of {a} divided by {b} is {result}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>-7 % 3<\/code> \u7684\u7ed3\u679c\u662f <code>2<\/code>\uff0c\u56e0\u4e3a <code>-7<\/code> \u9664\u4ee5 <code>3<\/code> \u7ed3\u679c\u662f <code>-3<\/code> \u4f59 <code>2<\/code>\u3002\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u786e\u4fdd\u4e86\u7ed3\u679c\u603b\u662f\u975e\u8d1f\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6a21\u6570\u8fd0\u7b97\u662f\u4e00\u4e2a\u975e\u5e38\u57fa\u7840\u4f46\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u7f16\u7a0b\u573a\u666f\u3002\u901a\u8fc7\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u548c<code>divmod()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u6574\u9664\u548c\u53d6\u4f59\u64cd\u4f5c\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u8d1f\u6570\u65f6\uff0c\u7406\u89e3Python\u4e2d\u6a21\u6570\u8fd0\u7b97\u7684\u89c4\u5219\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u907f\u514d\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u6f5c\u5728\u7684\u9519\u8bef\u3002\u65e0\u8bba\u662f\u5728\u5faa\u73af\u8ba1\u6570\u8fd8\u662f\u65f6\u95f4\u8ba1\u7b97\u4e2d\uff0c\u6a21\u6570\u8fd0\u7b97\u90fd\u63d0\u4f9b\u4e86\u7b80\u6d01\u800c\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528mod\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u4f59\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u4f5c\u4e3amod\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u540e\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c\u8868\u8fbe\u5f0f <code>7 % 3<\/code> \u5c06\u8fd4\u56de1\uff0c\u56e0\u4e3a7\u9664\u4ee53\u7684\u5546\u4e3a2\uff0c\u4f59\u6570\u4e3a1\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528mod\u8fd0\u7b97\u7b26\u5904\u7406\u8d1f\u6570\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u7684mod\u8fd0\u7b97\u7b26\u540c\u6837\u9002\u7528\u4e8e\u8d1f\u6570\u3002\u5f53\u4f7f\u7528\u8d1f\u6570\u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u4f59\u6570\u7684\u7b26\u53f7\u5c06\u4e0e\u9664\u6570\u76f8\u540c\u3002\u4f8b\u5982\uff0c<code>-7 % 3<\/code> \u7684\u7ed3\u679c\u662f2\uff0c\u56e0\u4e3a-7\u9664\u4ee53\u7684\u5546\u4e3a-3\uff0c\u4f59\u6570\u4e3a2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528mod\u8fd0\u7b97\u7b26\u8fdb\u884c\u5217\u8868\u5143\u7d20\u7684\u7b5b\u9009\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548cmod\u8fd0\u7b97\u7b26\u6765\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u83b7\u53d6\u6240\u6709\u5076\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>even_numbers = [x for x in my_list if x % 2 == 0]<\/code>\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5076\u6570\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u6a21\u6570\uff08modulus\uff09\u901a\u5e38\u4f7f\u7528\u767e\u5206\u53f7\u7b26\u53f7 % \u8fd0\u7b97\u7b26\u3002\u5177\u4f53\u6765\u8bf4\uff0ca % b \u5c06\u8fd4\u56de a [&hellip;]","protected":false},"author":3,"featured_media":963446,"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\/963440"}],"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=963440"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963440\/revisions"}],"predecessor-version":[{"id":963450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963440\/revisions\/963450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963446"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}