{"id":1117279,"date":"2025-01-08T18:24:30","date_gmt":"2025-01-08T10:24:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117279.html"},"modified":"2025-01-08T18:24:32","modified_gmt":"2025-01-08T10:24:32","slug":"python%e4%b8%ad%e7%9a%84%e6%b1%82%e4%bd%99%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117279.html","title":{"rendered":"python\u4e2d\u7684\u6c42\u4f59\u51fd\u6570\u5982\u4f55\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081255\/bdaa6dfc-6f1c-47fc-8233-3e16bb71f25f.webp\" alt=\"python\u4e2d\u7684\u6c42\u4f59\u51fd\u6570\u5982\u4f55\u5199\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u6c42\u4f59\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6c42\u4f59\u51fd\u6570\u88ab\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u540e\u7684\u4f59\u6570\u3002<strong>Python\u4e2d\u7684\u6c42\u4f59\u8fd0\u7b97\u7b26\u662f<code>%<\/code><\/strong>\uff0c\u901a\u8fc7\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u6c42\u4f59\u64cd\u4f5c\u3002<strong>\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>divmod()<\/code>\u3001\u624b\u52a8\u5b9e\u73b0\u6c42\u4f59<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>%<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u6c42\u4f59\u6570\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u683c\u5f0f\u4e3a<code>a % b<\/code>\uff0c\u5176\u4e2d<code>a<\/code>\u662f\u88ab\u9664\u6570\uff0c<code>b<\/code>\u662f\u9664\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nder = a % b<\/p>\n<p>print(f&quot;{a} % {b} = {remainder}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c10\u9664\u4ee53\u7684\u4f59\u6570\u662f1\uff0c\u6240\u4ee5\u8f93\u51fa\u7ed3\u679c\u662f<code>10 % 3 = 1<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>divmod()<\/code><\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>divmod(a, b)<\/code>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5546\u548c\u4f59\u6570\u7684\u5143\u7ec4\u3002<strong>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u5f97\u5546\u548c\u4f59\u6570<\/strong>\uff0c\u683c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">divmod(a, b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528<code>divmod()<\/code>\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>quotient, remainder = divmod(a, b)<\/p>\n<p>print(f&quot;\u5546: {quotient}, \u4f59\u6570: {remainder}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>divmod(10, 3)<\/code>\u8fd4\u56de(3, 1)\uff0c\u8868\u793a\u5546\u662f3\uff0c\u4f59\u6570\u662f1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u5b9e\u73b0\u6c42\u4f59<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u548c<code>divmod()<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u624b\u52a8\u5b9e\u73b0\u6c42\u4f59\u64cd\u4f5c\u3002<strong>\u624b\u52a8\u6c42\u4f59\u7684\u57fa\u672c\u601d\u8def\u662f\uff1a\u4ece\u88ab\u9664\u6570\u4e2d\u51cf\u53bb\u5c3d\u53ef\u80fd\u591a\u7684\u9664\u6570\uff0c\u76f4\u5230\u5269\u4e0b\u7684\u503c\u5c0f\u4e8e\u9664\u6570<\/strong>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def manual_mod(a, b):<\/p>\n<p>    while a &gt;= b:<\/p>\n<p>        a -= b<\/p>\n<p>    return a<\/p>\n<p>a = 10<\/p>\n<p>b = 3<\/p>\n<p>remainder = manual_mod(a, b)<\/p>\n<p>print(f&quot;\u624b\u52a8\u6c42\u4f59: {a} % {b} = {remainder}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5faa\u73af\u51cf\u53bb\u9664\u6570\uff0c\u76f4\u5230\u88ab\u9664\u6570\u5c0f\u4e8e\u9664\u6570\u65f6\uff0c\u5269\u4e0b\u7684\u503c\u5c31\u662f\u4f59\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591a\u5173\u4e8e\u6c42\u4f59\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u5224\u65ad\u5947\u5076\u6570<\/h4>\n<\/p>\n<p><p>\u6c42\u4f59\u8fd0\u7b97\u7b26<code>%<\/code>\u5e38\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5947\u6570\u8fd8\u662f\u5076\u6570\u3002\u4e00\u4e2a\u6570\u5bf92\u53d6\u4f59\uff0c\u5982\u679c\u7ed3\u679c\u662f0\uff0c\u5219\u4e3a\u5076\u6570\uff1b\u5426\u5219\u4e3a\u5947\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 4<\/p>\n<p>if num % 2 == 0:<\/p>\n<p>    print(f&quot;{num}\u662f\u5076\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num}\u662f\u5947\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5faa\u73af\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5faa\u73af\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6c42\u4f59\u8fd0\u7b97\u7b26\u6765\u786e\u4fdd\u7d22\u5f15\u5728\u6570\u7ec4\u8303\u56f4\u5185\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>index = 7<\/p>\n<p>print(arr[index % len(arr)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>index % len(arr)<\/code>\u786e\u4fdd\u7d22\u5f157\u88ab\u8f6c\u6362\u6210\u5728\u6570\u7ec4\u8303\u56f4\u5185\u7684\u7d22\u5f152\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u65f6\u95f4\u6362\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u65f6\u95f4\u6362\u7b97\u4e2d\uff0c\u6c42\u4f59\u8fd0\u7b97\u7b26\u4e5f\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5c06\u5206\u949f\u8f6c\u6362\u4e3a\u5c0f\u65f6\u548c\u5206\u949f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_minutes = 125<\/p>\n<p>hours = total_minutes \/\/ 60<\/p>\n<p>minutes = total_minutes % 60<\/p>\n<p>print(f&quot;{total_minutes}\u5206\u949f\u7b49\u4e8e{hours}\u5c0f\u65f6{minutes}\u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u6c42\u4f59\u51fd\u6570\u7684\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528<code>%<\/code>\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528<code>divmod()<\/code>\u51fd\u6570\u3001\u624b\u52a8\u5b9e\u73b0\u6c42\u4f59<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f\u3002\u6c42\u4f59\u8fd0\u7b97\u5728\u7f16\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4f8b\u5982\u5224\u65ad\u5947\u5076\u6570\u3001\u5faa\u73af\u6570\u7ec4\u3001\u65f6\u95f4\u6362\u7b97\u7b49\u3002\u7406\u89e3\u5e76\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7075\u6d3b\u8fd0\u7528\u6c42\u4f59\u8fd0\u7b97\uff0c\u89e3\u51b3\u5404\u79cd\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u6c42\u4f59\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26 <code>%<\/code> \u6765\u5b9e\u73b0\u6c42\u4f59\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>7 % 3<\/code> \u7684\u7ed3\u679c\u4e3a <code>1<\/code>\uff0c\u56e0\u4e3a 7 \u9664\u4ee5 3 \u7684\u4f59\u6570\u662f 1\u3002\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u6570\u5b66\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u3002<\/p>\n<p><strong>Python\u4e2d\u6c42\u4f59\u51fd\u6570\u7684\u4f7f\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u6c42\u4f59\u51fd\u6570\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5947\u6570\u8fd8\u662f\u5076\u6570\u3002\u5982\u679c\u4e00\u4e2a\u6570\u5bf92\u6c42\u4f59\u4e3a0\uff0c\u8bf4\u660e\u5b83\u662f\u5076\u6570\uff1b\u5982\u679c\u4e3a1\uff0c\u8bf4\u660e\u5b83\u662f\u5947\u6570\u3002\u6b64\u5916\uff0c\u6c42\u4f59\u8fd0\u7b97\u4e5f\u5e38\u7528\u4e8e\u5faa\u73af\u3001\u5206\u7ec4\u4ee5\u53ca\u5728\u7b97\u6cd5\u4e2d\u68c0\u67e5\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u4e00\u4e2a\u6c42\u4f59\u51fd\u6570\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6c42\u4f59\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>def<\/code>\u8bed\u53e5\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def custom_mod(a, b):\n    return a % b\n<\/code><\/pre>\n<p>\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u5373\u4e3a\u8fd9\u4e24\u4e2a\u6570\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c<code>custom_mod(10, 3)<\/code> \u5c06\u8fd4\u56de <code>1<\/code>\u3002\u8fd9\u79cd\u81ea\u5b9a\u4e49\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u4f60\u5728\u7279\u5b9a\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u6c42\u4f59\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u6c42\u4f59\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6c42\u4f59\u51fd\u6570\u88ab\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u540e\u7684\u4f59\u6570\u3002Python\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1117283,"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\/1117279"}],"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=1117279"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117279\/revisions"}],"predecessor-version":[{"id":1117285,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117279\/revisions\/1117285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117283"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}