{"id":920,"date":"2020-11-01T11:25:10","date_gmt":"2020-11-01T11:25:10","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=920"},"modified":"2025-03-27T15:12:11","modified_gmt":"2025-03-27T15:12:11","slug":"python-modulo","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-modulo\/","title":{"rendered":"Python Modulo"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python modulo operator (<code>%<\/code>) and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-modulo-operator'>Introduction to the Python modulo operator <a href=\"#introduction-to-the-python-modulo-operator\" class=\"anchor\" id=\"introduction-to-the-python-modulo-operator\" title=\"Anchor for Introduction to the Python modulo operator\">#<\/a><\/h2>\n\n\n\n<p>Python uses the percent sign (%) as the modulo operator. The modulo operator always satisfies the following equation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">N = D * ( N <span class=\"hljs-comment\">\/\/ D) + (N % D)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this equation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N is the numerator.<\/li>\n\n\n\n<li>D is the denominator.<\/li>\n\n\n\n<li>\/\/ is the <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-floor-division\/\">floor division operator<\/a><\/li>\n\n\n\n<li>And % is the modulo operator<\/li>\n<\/ul>\n\n\n\n<p>If both N and D are positive <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\">integers<\/a>, the modulo operator returns the remainder of N \/ D. However, it is not the case for the negative numbers. Therefore, you should always stick with the above equation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='simple-python-modulo-operator-examples'>Simple Python modulo operator examples <a href=\"#simple-python-modulo-operator-examples\" class=\"anchor\" id=\"simple-python-modulo-operator-examples\" title=\"Anchor for Simple Python modulo operator examples\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to use the modulo operator (%) with positive integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">16<\/span>\nb = <span class=\"hljs-number\">5<\/span>\n\nm = a % b\nf = a \/\/ b\n\n<span class=\"hljs-comment\"># show the result<\/span>\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{a}<\/span> % <span class=\"hljs-subst\">{b}<\/span> = <span class=\"hljs-subst\">{m}<\/span>'<\/span>)  <span class=\"hljs-comment\"># 1<\/span>\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{a}<\/span> \/\/ <span class=\"hljs-subst\">{b}<\/span> = <span class=\"hljs-subst\">{f}<\/span>'<\/span>)  <span class=\"hljs-comment\"># 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YSA9IDE2CmIgPSA1CgptID0gYSAlIGIKZiA9IGEgLy8gYgoKIyBzaG93IHRoZSByZXN1bHQKCnByaW50KGYne2F9ICUge2J9ID0ge219JykgICMgMQpwcmludChmJ3thfSAvLyB7Yn0gPSB7Zn0nKSAgIyAz\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1\n3<\/code><\/span><\/pre>\n\n\n<p>For positive numbers, the result is quite apparent. And you can check the equation quickly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-number\">16<\/span> = <span class=\"hljs-number\">5<\/span> * (<span class=\"hljs-number\">16<\/span> <span class=\"hljs-comment\">\/\/ 5) + 16 % 5<\/span>\n<span class=\"hljs-number\">16<\/span> = <span class=\"hljs-number\">5<\/span> * <span class=\"hljs-number\">3<\/span>         + <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows how to use the modulo operator (<code>%<\/code>) with negative integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">-16<\/span>\nb = <span class=\"hljs-number\">5<\/span>\n\nm = a % b\nf = a \/\/ b\n\n<span class=\"hljs-comment\"># show the result<\/span>\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{a}<\/span> % <span class=\"hljs-subst\">{b}<\/span> = <span class=\"hljs-subst\">{m}<\/span>'<\/span>)  <span class=\"hljs-comment\"># 4<\/span>\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{a}<\/span> \/\/ <span class=\"hljs-subst\">{b}<\/span> = <span class=\"hljs-subst\">{f}<\/span>'<\/span>)  <span class=\"hljs-comment\"># -4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YSA9IC0xNgpiID0gNQoKbSA9IGEgJSBiCmYgPSBhIC8vIGIKCiMgc2hvdyB0aGUgcmVzdWx0CgpwcmludChmJ3thfSAlIHtifSA9IHttfScpICAjIDQKcHJpbnQoZid7YX0gLy8ge2J9ID0ge2Z9JykgICMgLTQ%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>And the equation is satisfied:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">-16 = 5 * (-16 % 5) + (-16) % 5\n-16 = 5 * -4        - 4<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='practical-python-modulo-operator-examples'>Practical Python modulo operator examples <a href=\"#practical-python-modulo-operator-examples\" class=\"anchor\" id=\"practical-python-modulo-operator-examples\" title=\"Anchor for Practical Python modulo operator examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some practical examples of using  the modulo operator (%)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='checking-if-a-number-is-even-or-odd'>Checking if a number is even or odd <a href=\"#checking-if-a-number-is-even-or-odd\" class=\"anchor\" id=\"checking-if-a-number-is-even-or-odd\" title=\"Anchor for Checking if a number is even or odd\">#<\/a><\/h3>\n\n\n\n<p>The following <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">defines a function<\/a> that uses the modulo operator (%) to check if a number is even:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">is_even<\/span><span class=\"hljs-params\">(num)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> num % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the following defines a function that uses the modulo operator to check if a number is odd:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">def is_odd(num):\n    <span class=\"hljs-keyword\">return<\/span> num % <span class=\"hljs-number\">2<\/span> != <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='converting-between-units'>Converting between units <a href=\"#converting-between-units\" class=\"anchor\" id=\"converting-between-units\" title=\"Anchor for Converting between units\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the modulo operator (<code>%<\/code>) to convert seconds to days, hours, minutes, and seconds. It can be handy if you want to develop a countdown program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> floor\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_time<\/span><span class=\"hljs-params\">(total_seconds)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> {\n        <span class=\"hljs-string\">'days'<\/span>: floor(total_seconds \/ <span class=\"hljs-number\">60<\/span> \/ <span class=\"hljs-number\">60<\/span> \/ <span class=\"hljs-number\">24<\/span>),\n        <span class=\"hljs-string\">'hours'<\/span>: floor(total_seconds \/ <span class=\"hljs-number\">60<\/span> \/ <span class=\"hljs-number\">60<\/span>) % <span class=\"hljs-number\">24<\/span>,\n        <span class=\"hljs-string\">'minutes'<\/span>: floor(total_seconds \/ <span class=\"hljs-number\">60<\/span>) % <span class=\"hljs-number\">60<\/span>,\n        <span class=\"hljs-string\">'seconds'<\/span>: total_seconds % <span class=\"hljs-number\">60<\/span>,\n    }\n\n\nprint(get_time(<span class=\"hljs-number\">93750<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBtYXRoIGltcG9ydCBmbG9vcgoKCmRlZiBnZXRfdGltZSh0b3RhbF9zZWNvbmRzKToKICAgIHJldHVybiB7CiAgICAgICAgJ2RheXMnOiBmbG9vcih0b3RhbF9zZWNvbmRzIC8gNjAgLyA2MCAvIDI0KSwKICAgICAgICAnaG91cnMnOiBmbG9vcih0b3RhbF9zZWNvbmRzIC8gNjAgLyA2MCkgJSAyNCwKICAgICAgICAnbWludXRlcyc6IGZsb29yKHRvdGFsX3NlY29uZHMgLyA2MCkgJSA2MCwKICAgICAgICAnc2Vjb25kcyc6IHRvdGFsX3NlY29uZHMgJSA2MCwKICAgIH0KCgpwcmludChnZXRfdGltZSg5Mzc1MCkp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">{<span class=\"hljs-string\">'days'<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'hours'<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'minutes'<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'seconds'<\/span>: <span class=\"hljs-number\">30<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python uses the percent sign (<code>%<\/code>) as the modulo operator.<\/li>\n\n\n\n<li>The modulo operator (%) always satisfies the equation <code>N = D * ( N \/\/ D) + (N % D)<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"920\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-modulo\/\"\n\t\t\t\tdata-post-title=\"Python Modulo\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"920\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-modulo\/\"\n\t\t\t\tdata-post-title=\"Python Modulo\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about the Python modulo operator (%) and how to use it effectively. Introduction to the Python modulo operator # Python uses the percent sign (%) as the modulo operator. The modulo operator always satisfies the following equation: In this equation: If both N and D are positive integers, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-920","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=920"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/920\/revisions"}],"predecessor-version":[{"id":7137,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/920\/revisions\/7137"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}