{"id":2910,"date":"2025-06-01T17:24:25","date_gmt":"2025-06-04T09:26:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2910"},"modified":"2025-06-04T17:24:25","modified_gmt":"2025-06-04T09:26:24","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84mod%e5%8f%96%e9%a4%98%e9%81%8b%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84mod%e5%8f%96%e9%a4%98%e9%81%8b%e7%ae%97%e7%ac%a6\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684\u53d6\u9918\u904b\u7b97\u7b26\uff1amod() \u51fd\u6578\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, mod, \u91cd\u8f09\u985e, \u53d6\u9918\u904b\u7b97\u7b26, \u53d6\u9918\u904b\u7b97, Python \u6559\u5b78\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u529f\u80fd\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5176\u4e2d\u4e00\u500b\u57fa\u672c\u7684\u6578\u5b78\u904b\u7b97\u5c31\u662f\u53d6\u9918\u904b\u7b97\u7b26\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Python \u4e2d\u4f7f\u7528 mod() \u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u8b93\u4f60\u57282025\u5e74\u80fd\u5920\u9ad8\u6548\u5730\u9032\u884c\u7de8\u7a0b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u53d6\u9918\u904b\u7b97\u7b26\uff1f<\/h2>\n<p>\u5728\u6578\u5b78\u4e2d\uff0c\u53d6\u9918\u904b\u7b97\u7b26\uff08modulus operator\uff09\u662f\u7528\u4f86\u8a08\u7b97\u5169\u500b\u6578\u5b57\u76f8\u9664\u5f8c\u7684\u9918\u6578\u3002\u5728 Python \u4e2d\uff0c\u9019\u500b\u64cd\u4f5c\u53ef\u4ee5\u900f\u904e `%` \u7b26\u865f\u4f86\u5be6\u73fe\uff0c\u6216\u4f7f\u7528 `math.fmod()` \u51fd\u6578\u4f86\u7372\u5f97\u6d6e\u9ede\u6578\u7684\u9918\u6578\u3002<\/p>\n<h2>\u4f7f\u7528 mod() \u51fd\u6578\u8a08\u7b97\u9918\u6578<\/h2>\n<p>\u5728 Python \u4e2d\uff0c`mod()` \u51fd\u6578\u4e26\u4e0d\u662f\u5167\u5efa\u7684\u6a19\u6e96\u51fd\u6578\u3002\u5be6\u969b\u4e0a\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `%` \u7b26\u865f\u4f86\u9054\u6210\u985e\u4f3c\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u53d6\u9918\u904b\u7b97\u7b26\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u5169\u500b\u6578\u5b57<br \/>\nx = 10<br \/>\ny = 3<\/p>\n<p># \u4f7f\u7528\u53d6\u9918\u904b\u7b97\u7b26\u8a08\u7b97\u9918\u6578<br \/>\nz = x % y<\/p>\n<p># \u8f38\u51fa\u7d50\u679c<br \/>\nprint(z)  # \u8f38\u51fa 1<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u6578\u5b57 `x` \u548c `y`\uff0c\u7136\u5f8c\u4f7f\u7528 `%` \u7b26\u865f\u8a08\u7b97\u9918\u6578\uff0c\u6700\u5f8c\u8f38\u51fa\u7d50\u679c\u70ba 1\u3002<\/p>\n<h2>\u91cd\u8f09\u985e\u7684\u53d6\u9918\u904b\u7b97\u7b26<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u81ea\u5b9a\u7fa9\u985e\u5225\u4e2d\u4f7f\u7528\u53d6\u9918\u904b\u7b97\u7b26\uff0c\u53ef\u4ee5\u91cd\u8f09\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u7bc4\u5982\u4f55\u5728\u985e\u4e2d\u91cd\u8f09 `%` \u904b\u7b97\u7b26\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyNumber:<br \/>\n    def __init__(self, value):<br \/>\n        self.value = value<\/p>\n<p>    def __mod__(self, other):<br \/>\n        return MyNumber(self.value % other.value)<\/p>\n<p># \u5b9a\u7fa9\u5169\u500b MyNumber \u7269\u4ef6<br \/>\nnum1 = MyNumber(10)<br \/>\nnum2 = MyNumber(3)<\/p>\n<p># \u4f7f\u7528\u91cd\u8f09\u7684\u53d6\u9918\u904b\u7b97\u7b26<br \/>\nresult = num1 % num2<\/p>\n<p># \u8f38\u51fa\u7d50\u679c<br \/>\nprint(result.value)  # \u8f38\u51fa 1<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b `MyNumber` \u985e\u4e26\u91cd\u8f09\u4e86 `%` \u904b\u7b97\u7b26\uff0c\u4f7f\u5176\u80fd\u5920\u8fd4\u56de\u4e00\u500b\u65b0\u7684 `MyNumber` \u7269\u4ef6\uff0c\u5176\u5305\u542b\u9918\u6578\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>1. **TypeError**\uff1a\u7576\u4f60\u5617\u8a66\u5c0d\u975e\u6578\u5b57\u985e\u578b\u9032\u884c\u53d6\u9918\u904b\u7b97\u6642\uff0c\u6703\u5f15\u767c TypeError\u3002\u78ba\u4fdd\u6240\u6709\u64cd\u4f5c\u6578\u90fd\u662f\u6578\u5b57\u985e\u578b\u3002<br \/>\n2. **ZeroDivisionError**\uff1a\u5982\u679c\u4f60\u5617\u8a66\u4ee5\u96f6\u4f5c\u70ba\u9664\u6578\uff0c\u6703\u5f15\u767c ZeroDivisionError\u3002\u8acb\u5728\u8a08\u7b97\u524d\u6aa2\u67e5\u9664\u6578\u662f\u5426\u70ba\u96f6\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u53d6\u9918\u904b\u7b97\u7b26\u5728\u591a\u500b\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u78ba\u5b9a\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u5076\u6578\u6216\u5947\u6578\uff1a`if x % 2 == 0:`<br \/>\n&#8211; \u5728\u5faa\u74b0\u4e2d\u5be6\u73fe\u74b0\u72c0\u6578\u7d44\u7684\u7d22\u5f15\uff1a`index = (index + 1) % length`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u53d6\u9918\u904b\u7b97\u7b26\u662f\u4e00\u500b\u57fa\u672c\u800c\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7121\u8ad6\u662f\u5728\u6578\u5b78\u904b\u7b97\u9084\u662f\u6578\u64da\u8655\u7406\u4e2d\u90fd\u6709\u5ee3\u6cdb\u7684\u61c9\u7528\u3002\u901a\u904e\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 `%` \u7b26\u865f\u53ca\u91cd\u8f09\u5176\u5728\u985e\u4e2d\u7684\u884c\u70ba\uff0c\u4f60\u5c07\u80fd\u5920\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6578\u5b57\u904b\u7b97\uff0c\u4e26\u63d0\u5347\u7a0b\u5f0f\u7684\u6548\u7387\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a Python \u76f8\u95dc\u7684\u77e5\u8b58\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u88e1\u7684\u9032\u4e00\u6b65\u6559\u5b78](https:\/\/vocus.cc) \u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: Python \u4e2d\u7684\u53d6\u9918\u904b\u7b97\u7b26 % \u548c mod() \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: \u5728 Python \u4e2d\uff0c\u53d6\u9918\u904b\u7b97\u7b26 `%` \u662f\u5167\u5efa\u7684\uff0c\u800c `mod()` \u4e26\u4e0d\u662f\u6a19\u6e96\u51fd\u6578\u3002\u901a\u5e38\u4f7f\u7528 `%` \u4f86\u8a08\u7b97\u9918\u6578\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u985e\u4e2d\u91cd\u8f09\u53d6\u9918\u904b\u7b97\u7b26\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u900f\u904e\u5b9a\u7fa9 `__mod__` \u65b9\u6cd5\u4f86\u91cd\u8f09\u53d6\u9918\u904b\u7b97\u7b26\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u81ea\u5b9a\u7fa9\u985e\u5225\u652f\u6301 `%` \u64cd\u4f5c\u3002<\/p>\n<p>**Q3: \u53d6\u9918\u904b\u7b97\u7b26\u5728\u6578\u64da\u8655\u7406\u4e2d\u6709\u54ea\u4e9b\u61c9\u7528\uff1f**<br \/>\nA3: \u53d6\u9918\u904b\u7b97\u7b26\u5e38\u7528\u65bc\u6aa2\u67e5\u6578\u5b57\u7684\u5947\u5076\u6027\u3001\u74b0\u72c0\u6578\u7d44\u7684\u7d22\u5f15\u8a08\u7b97\u4ee5\u53ca\u5206\u7d44\u8a08\u7b97\u7b49\u5834\u666f\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684mod()\u53d6\u9918\u904b\u7b97\u7b26\u53ef\u4ee5\u7528\u65bc\u91cd\u8f09\u985e\uff0c\u53ef\u4ee5\u7528\u65bc\u8a08\u7b97\u5169\u500b\u6578\u5b57\u7684\u9918\u6578\u3002\u672c\u6587\u5c07\u4ecb\u7d39mod()\u53d6\u9918\u904b\u7b97\u7b26\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5728Python\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-KW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2910"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2910\/revisions"}],"predecessor-version":[{"id":2911,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2910\/revisions\/2911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}