Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Python Modulo: Using the % Operator (Summary)
At first glance, the Python modulo operator may not grab your attention. Yet, as you’ve seen, there’s so much to this humble operator. From checking for even numbers to encrypting text with ciphers, you’ve seen many different uses for the modulo operator.
In this course, you’ve learned how to:
- Use themodulo operator with
int,float,math.fmod(),divmod(), anddecimal.Decimal - Calculate the results of amodulo operation
- Solvereal-world problems using the modulo operator
- Override
.__mod__()in your own classes to use them with the modulo operator
Here are additional resources about python expressions, floating point arithmetic, cryptography, and more:
00:00In the previous lesson,I showed you how to use.__mod__() and the% operator in your own classes.In this lesson, I’ll wrap up and summarize the course.
00:11Python’s% operator anddivmod() function are used to calculate division andremainders. 14 divided by 4 is 3 remainder 2. Usingdivmod(),you can do exactly that calculation, getting the 3 remainder 2.
00:26Or using the% operator, you can get just the remainder.The mod operation can be done on bothint andfloat.The precision problems that you can encounter with float numbers are alsopresent when you do mod on floats.
00:44Additionally,themath module has thefmod() function that also does the modulus operation.An important thing to note is that the% operator andfmod() calculatenegative mods differently.8 % -3 is-1,whereasfmod() returns2.
01:06In a compound statement,the% operator has the same precedence as multiplication and division,so it may be important to use brackets to get the result you’re intending.
01:17One of the most common uses of the mod operator in coding is to determinewhether or not a number is even.You could do that by doing% 2 and checking for0. Mod is also veryhelpful when you need to cycle through a list,wanting to loop it back to the beginningwithout having to check whether or not you’ve passed the end.
01:37The appropriate use of the mod operator can make your code more succinct andrequire fewer boundary checks.Mod is often used for unit conversion as well.
01:48This tends to be a good place to usedivmod(). Converting a total number ofminutes into the number of days, hours, and minutes that iscan be done with two quick calls todivmod().
02:00days andextra_minutes are the divisor and the remainder of the1440 minutes ina day,and then taking the result from the previous line and runningdivmod() on thatfor60 will give you the remaining hours and minutes in the calculation.
02:19Like all operators in Python,% maps to a class method.The% method maps to.__mod__().This can be found in Python’s actual source code and is something that you can doyourself when you’re writing your own classesif you wish to support the mod operator.
02:38One of the best places to get answers to your questions about Python is thePython documentation. Here’s the link to the expressions page.The subsection on binary arithmetic operations includes information on the modoperator. To better understand floating-point,you can look at the Python floating-pointdocumentation, or Wikipedia has an article on the IEEE 754 standardthat specifies how floating-point arithmetic is meant to work.
03:09If cryptography is your thing,the course on exploring HTTPS and cryptography shows you how to use mod tocreate Caesar ciphers as well as the generation of RSA keys.
03:23Here’s the link to the Pythondecimal module,if you wish to dig into that a little further.And this page describes all of the dunder methods and what operators map tothem. I hope this course has been useful for you.
Course Contents

