|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-mod-operator/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/python-mod-operator/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fpython-mod-operator%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-modulo-operator |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-modulo-operator |
| https://realpython.com/courses/python-modulo-operator/#team |
| Python Modulo: Using the % Operator | https://realpython.com/courses/python-modulo-operator/ |
| Christopher Trudeau | https://realpython.com/courses/python-modulo-operator/#team |
| Recommended Tutorial | https://realpython.com/python-modulo-operator/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-modulo-operator/downloads/modulo-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-modulo-operator/downloads/modulo-sample-code/ |
| Ask a Question | https://realpython.com/lessons/python-mod-operator/#discussion |
| https://realpython.com/feedback/survey/course/python-modulo-operator/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-modulo-operator/disliked/?from=lesson-title |
| Transcript | https://realpython.com/lessons/python-mod-operator/#transcript |
| Discussion | https://realpython.com/lessons/python-mod-operator/#discussion |
| 00:00 | https://realpython.com/lessons/python-mod-operator/#t=0.51 |
| In the previous lesson, I introduced you to modular arithmetic. In this lesson, | https://realpython.com/lessons/python-mod-operator/#t=0.51 |
| I’ll show you how to use the % operator to accomplish the same thing in | https://realpython.com/lessons/python-mod-operator/#t=4.8 |
| Python. | https://realpython.com/lessons/python-mod-operator/#t=8.61 |
| 00:12 | https://realpython.com/lessons/python-mod-operator/#t=12.53 |
| The % operator is the modulus operator. | https://realpython.com/lessons/python-mod-operator/#t=12.53 |
| Just like in the previous lesson, | https://realpython.com/lessons/python-mod-operator/#t=15.44 |
| 13 mod 12 is 1. 13 divided by 12 has a remainder of 1, | https://realpython.com/lessons/python-mod-operator/#t=17.18 |
| hence the result. | https://realpython.com/lessons/python-mod-operator/#t=22.31 |
| 00:25 | https://realpython.com/lessons/python-mod-operator/#t=25.46 |
| Here’s another one for 15 mod 4. And a bigger value, | https://realpython.com/lessons/python-mod-operator/#t=25.46 |
| 240 mod 13. Because the mod operator involves division, | https://realpython.com/lessons/python-mod-operator/#t=29.27 |
| you have the same problem if you do a mod by 0, | https://realpython.com/lessons/python-mod-operator/#t=34.61 |
| as if you did a divide by 0. | https://realpython.com/lessons/python-mod-operator/#t=37.64 |
| 00:40 | https://realpython.com/lessons/python-mod-operator/#t=40.97 |
| You’ll get a ZeroDivisionError raised in this case. | https://realpython.com/lessons/python-mod-operator/#t=40.97 |
| In the previous lesson, I showed you that negatives were possible. | https://realpython.com/lessons/python-mod-operator/#t=45.62 |
| 8 mod -3 is -1. | https://realpython.com/lessons/python-mod-operator/#t=49.34 |
| You have to be a little careful with negative numbers. | https://realpython.com/lessons/python-mod-operator/#t=52.73 |
| 00:54 | https://realpython.com/lessons/python-mod-operator/#t=54.62 |
| There’s two different ways of calculating mod, and you can get two different | https://realpython.com/lessons/python-mod-operator/#t=54.62 |
| results depending on which mechanism you’re using. | https://realpython.com/lessons/python-mod-operator/#t=57.8 |
| I’ll show you more about that a little later. | https://realpython.com/lessons/python-mod-operator/#t=60.38 |
| 01:03 | https://realpython.com/lessons/python-mod-operator/#t=63.32 |
| The % operator also supports floating-point. | https://realpython.com/lessons/python-mod-operator/#t=63.32 |
| Floating-point values going in result in floating-point values coming out. | https://realpython.com/lessons/python-mod-operator/#t=67.52 |
| If you haven’t played much with floating-point before, | https://realpython.com/lessons/python-mod-operator/#t=73.49 |
| you might be a little surprised at the following little piece of math. | https://realpython.com/lessons/python-mod-operator/#t=75.53 |
| 01:18 | https://realpython.com/lessons/python-mod-operator/#t=78.58 |
| .1 + .1 + .1 isn’t 0.3. | https://realpython.com/lessons/python-mod-operator/#t=78.58 |
| This has to do with how your computer stores | https://realpython.com/lessons/python-mod-operator/#t=82.97 |
| these kinds of numbers and the floating-point standard. Mod on floating-point has | https://realpython.com/lessons/python-mod-operator/#t=84.86 |
| the same kind of precision problem as this addition example I just showed you. | https://realpython.com/lessons/python-mod-operator/#t=89.6 |
| 01:35 | https://realpython.com/lessons/python-mod-operator/#t=95.09 |
| Little inaccuracies sneak in. | https://realpython.com/lessons/python-mod-operator/#t=95.09 |
| 01:39 | https://realpython.com/lessons/python-mod-operator/#t=99.98 |
| You should always be cautious with floating-point, | https://realpython.com/lessons/python-mod-operator/#t=99.98 |
| and this is why you should never use floating-point values to store money. | https://realpython.com/lessons/python-mod-operator/#t=102.56 |
| These kinds of errors accumulate over time. | https://realpython.com/lessons/python-mod-operator/#t=106.4 |
| 01:49 | https://realpython.com/lessons/python-mod-operator/#t=109.52 |
| Python has a class called Decimal, capital D, inside of the decimal module. | https://realpython.com/lessons/python-mod-operator/#t=109.52 |
| It has arbitrary precision and doesn’t have this problem. | https://realpython.com/lessons/python-mod-operator/#t=115.04 |
| 01:58 | https://realpython.com/lessons/python-mod-operator/#t=118.31 |
| I’ll show you more about these later. | https://realpython.com/lessons/python-mod-operator/#t=118.31 |
| There are two ways of doing modular arithmetic in Python: the % operator | https://realpython.com/lessons/python-mod-operator/#t=121.37 |
| and inside of the math library, fmod(). | https://realpython.com/lessons/python-mod-operator/#t=125.72 |
| 02:14 | https://realpython.com/lessons/python-mod-operator/#t=134.84 |
| Generally, it’s recommended to use the % operator when dealing with | https://realpython.com/lessons/python-mod-operator/#t=134.84 |
| integers and the fmod() function when dealing with floating-point. | https://realpython.com/lessons/python-mod-operator/#t=138.41 |
| 02:22 | https://realpython.com/lessons/python-mod-operator/#t=142.73 |
| Remember how I mentioned earlier that negatives are a problem? | https://realpython.com/lessons/python-mod-operator/#t=142.73 |
| It turns out that % for integers and floats and fmod() calculate negative | https://realpython.com/lessons/python-mod-operator/#t=145.49 |
| modulus differently, and so you’ll get inconsistent results. | https://realpython.com/lessons/python-mod-operator/#t=151.04 |
| 02:36 | https://realpython.com/lessons/python-mod-operator/#t=156.95 |
| Here’s a large exponent, and here’s the same using the % operator. | https://realpython.com/lessons/python-mod-operator/#t=156.95 |
| You have a significantly different value showing up there. | https://realpython.com/lessons/python-mod-operator/#t=162.32 |
| This problem with negatives that I’ve shown you isn’t just within Python. | https://realpython.com/lessons/python-mod-operator/#t=166.91 |
| 02:49 | https://realpython.com/lessons/python-mod-operator/#t=169.97 |
| It’s also between different languages. | https://realpython.com/lessons/python-mod-operator/#t=169.97 |
| Not all programming languages calculate the negative modulus the same way. | https://realpython.com/lessons/python-mod-operator/#t=172.43 |
| 8 % -3 in JavaScript is 2. In Python, | https://realpython.com/lessons/python-mod-operator/#t=176.42 |
| the same calculation is -1. | https://realpython.com/lessons/python-mod-operator/#t=180.67 |
| 03:04 | https://realpython.com/lessons/python-mod-operator/#t=184.18 |
| There are two common ways of calculating mod. | https://realpython.com/lessons/python-mod-operator/#t=184.18 |
| Here’s how Python’s % operator does it. | https://realpython.com/lessons/python-mod-operator/#t=187.09 |
| The remainder is calculated as a minus n times the floor of a divided by n. | https://realpython.com/lessons/python-mod-operator/#t=190.15 |
| 03:18 | https://realpython.com/lessons/python-mod-operator/#t=198.37 |
| Here’s an example. | https://realpython.com/lessons/python-mod-operator/#t=198.37 |
| 13 mod 12 is 13 minus 12 times the floor of 13 | https://realpython.com/lessons/python-mod-operator/#t=199.9 |
| over 12. | https://realpython.com/lessons/python-mod-operator/#t=204.94 |
| Or 13 minus 12 times the floor of 1.083. | https://realpython.com/lessons/python-mod-operator/#t=207.28 |
| The floor of 1.083 is 1, | https://realpython.com/lessons/python-mod-operator/#t=212.68 |
| so you end up with 13 minus 12 times 1, | https://realpython.com/lessons/python-mod-operator/#t=214.93 |
| which is a remainder of 1. Now let’s do that again | https://realpython.com/lessons/python-mod-operator/#t=218.8 |
| with a negative number. 8 mod -3 gives you 8 minus -3 | https://realpython.com/lessons/python-mod-operator/#t=222.58 |
| times the floor of 8 divided by -3. | https://realpython.com/lessons/python-mod-operator/#t=228.64 |
| 03:52 | https://realpython.com/lessons/python-mod-operator/#t=232.75 |
| 8 divided by -3 gives you minus 2.6 repeated, | https://realpython.com/lessons/python-mod-operator/#t=232.75 |
| the floor of which is -3, | https://realpython.com/lessons/python-mod-operator/#t=237.0 |
| giving you 9 on the right-hand side and remainder of -1. | https://realpython.com/lessons/python-mod-operator/#t=239.35 |
| JavaScript’s % operator calculates things slightly differently. For the | https://realpython.com/lessons/python-mod-operator/#t=246.25 |
| same operation, instead of using floor(), | https://realpython.com/lessons/python-mod-operator/#t=250.99 |
| it uses trunc(). For whole numbers, this isn’t a problem. | https://realpython.com/lessons/python-mod-operator/#t=253.69 |
| 04:18 | https://realpython.com/lessons/python-mod-operator/#t=258.579 |
| The end result is the same calculation as you saw before. | https://realpython.com/lessons/python-mod-operator/#t=258.579 |
| Unfortunately, for negative numbers | https://realpython.com/lessons/python-mod-operator/#t=263.83 |
| it is a problem. When you get to the trunc() step of 8 divided by -3, | https://realpython.com/lessons/python-mod-operator/#t=265.66 |
| the truncation of -2.6 repeated is -2. | https://realpython.com/lessons/python-mod-operator/#t=272.08 |
| 04:36 | https://realpython.com/lessons/python-mod-operator/#t=276.49 |
| This changes the value on the right-hand side and thus changes the result. | https://realpython.com/lessons/python-mod-operator/#t=276.49 |
| 04:42 | https://realpython.com/lessons/python-mod-operator/#t=282.67 |
| In Python, the % operator uses floor(), | https://realpython.com/lessons/python-mod-operator/#t=282.67 |
| but the math.fmod() uses trunc(). | https://realpython.com/lessons/python-mod-operator/#t=285.88 |
| This is what causes the difference that I showed you earlier between negative | https://realpython.com/lessons/python-mod-operator/#t=289.24 |
| values for % and fmod(). You need to be aware of this | https://realpython.com/lessons/python-mod-operator/#t=293.59 |
| if you’re doing modulus math on negative values. | https://realpython.com/lessons/python-mod-operator/#t=297.7 |
| 05:01 | https://realpython.com/lessons/python-mod-operator/#t=301.54 |
| 8 % -3 is -1, | https://realpython.com/lessons/python-mod-operator/#t=301.54 |
| whereas the fmod() on the same value is 2. | https://realpython.com/lessons/python-mod-operator/#t=304.27 |
| This is the same as the JavaScript versus Python example I just showed you. | https://realpython.com/lessons/python-mod-operator/#t=307.69 |
| 05:13 | https://realpython.com/lessons/python-mod-operator/#t=313.15 |
| In addition to the mod methods I’ve shown you so far, | https://realpython.com/lessons/python-mod-operator/#t=313.15 |
| Python also has a function called divmod(). | https://realpython.com/lessons/python-mod-operator/#t=315.55 |
| 05:20 | https://realpython.com/lessons/python-mod-operator/#t=320.2 |
| divmod() returns two values—both the result of the division and the remainder. | https://realpython.com/lessons/python-mod-operator/#t=320.2 |
| 12 goes into 13 once, | https://realpython.com/lessons/python-mod-operator/#t=325.51 |
| so you get the first part of the tuple, and has a remainder of 1, giving you the | https://realpython.com/lessons/python-mod-operator/#t=327.82 |
| second part of the tuple. | https://realpython.com/lessons/python-mod-operator/#t=331.54 |
| 05:34 | https://realpython.com/lessons/python-mod-operator/#t=334.9 |
| 5 goes into 37 7 times and has a remainder of 2 | https://realpython.com/lessons/python-mod-operator/#t=334.9 |
| when it’s done. The divmod() function is the equivalent of running two different | https://realpython.com/lessons/python-mod-operator/#t=339.43 |
| Python operations at the same time. | https://realpython.com/lessons/python-mod-operator/#t=344.5 |
| 05:47 | https://realpython.com/lessons/python-mod-operator/#t=347.26 |
| First off, strict integer division, expressed by the double slashes (//). | https://realpython.com/lessons/python-mod-operator/#t=347.26 |
| 37 // 5 is 7. And secondly, % operator. 37 % 5 is 2, | https://realpython.com/lessons/python-mod-operator/#t=353.03 |
| divmod(37, 5) giving you the same as these two results | https://realpython.com/lessons/python-mod-operator/#t=361.89 |
| but in one function call. Note that divmod() uses the same mechanism as | https://realpython.com/lessons/python-mod-operator/#t=365.99 |
| %. So if you’re dealing with negative values, | https://realpython.com/lessons/python-mod-operator/#t=371.36 |
| you’ll get the kind of calculation that the % operator does—not the kind | https://realpython.com/lessons/python-mod-operator/#t=374.21 |
| of calculation that fmod() uses. In Python, | https://realpython.com/lessons/python-mod-operator/#t=378.32 |
| the % operator is considered to have the same operator precedence as | https://realpython.com/lessons/python-mod-operator/#t=382.49 |
| division and multiplication. | https://realpython.com/lessons/python-mod-operator/#t=386.96 |
| 06:31 | https://realpython.com/lessons/python-mod-operator/#t=391.1 |
| 4 times 10 is 40, 12 | https://realpython.com/lessons/python-mod-operator/#t=391.1 |
| goes into 40 3 times with a remainder of 4, | https://realpython.com/lessons/python-mod-operator/#t=393.5 |
| 4 minus 9 gives you -5. | https://realpython.com/lessons/python-mod-operator/#t=396.44 |
| Adding some brackets to the same statement changes the value. | https://realpython.com/lessons/python-mod-operator/#t=400.58 |
| Now you’re doing 40 mod 3 instead. | https://realpython.com/lessons/python-mod-operator/#t=404.93 |
| 06:49 | https://realpython.com/lessons/python-mod-operator/#t=409.88 |
| That’s the basic usage of % and fmod() | https://realpython.com/lessons/python-mod-operator/#t=409.88 |
| in Python. In the next lesson, | https://realpython.com/lessons/python-mod-operator/#t=412.88 |
| I’ll show you some common uses of these operators inside of code. | https://realpython.com/lessons/python-mod-operator/#t=414.98 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/modular-arithmetic/ |
| Overview | https://realpython.com/courses/python-modulo-operator/ |
| https://realpython.com/lessons/common-uses-mod/ |
|
Python Modulo: Using the % Operator (Overview) 01:44
| https://realpython.com/videos/python-modulo-overview/ |
|
Modular Arithmetic 03:36
| https://realpython.com/videos/modular-arithmetic/ |
|
Python % Operator 07:00
| https://realpython.com/lessons/python-mod-operator/ |
|
Common Uses of % 08:57
| https://realpython.com/lessons/common-uses-mod/ |
|
Advanced Uses and Overriding .__mod__ 09:00
| https://realpython.com/lessons/overriding-dunder-mod/ |
|
Python Modulo: Using the % Operator (Summary) 03:40
| https://realpython.com/lessons/python-modulo-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover