Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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 the modulo operator with int, float, math.fmod(), divmod(), and decimal.Decimal
  • Calculate the results of a modulo operation
  • Solve real-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:


Sample Code (.zip)

2.4 KB

Course Slides (.pdf)

696.4 KB

00:00 In 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:11 Python’s % operator and divmod() function are used to calculate division and remainders. 14 divided by 4 is 3 remainder 2. Using divmod(), you can do exactly that calculation, getting the 3 remainder 2.

00:26 Or using the % operator, you can get just the remainder. The mod operation can be done on both int and float. The precision problems that you can encounter with float numbers are also present when you do mod on floats.

00:44 Additionally, the math module has the fmod() function that also does the modulus operation. An important thing to note is that the % operator and fmod() calculate negative mods differently. 8 % -3 is -1, whereas fmod() returns 2.

01:06 In 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:17 One of the most common uses of the mod operator in coding is to determine whether or not a number is even. You could do that by doing % 2 and checking for 0. Mod is also very helpful when you need to cycle through a list, wanting to loop it back to the beginning without having to check whether or not you’ve passed the end.

01:37 The appropriate use of the mod operator can make your code more succinct and require fewer boundary checks. Mod is often used for unit conversion as well.

01:48 This tends to be a good place to use divmod(). Converting a total number of minutes into the number of days, hours, and minutes that is can be done with two quick calls to divmod().

02:00 days and extra_minutes are the divisor and the remainder of the 1440 minutes in a day, and then taking the result from the previous line and running divmod() on that for 60 will give you the remaining hours and minutes in the calculation.

02:19 Like 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 do yourself when you’re writing your own classes if you wish to support the mod operator.

02:38 One of the best places to get answers to your questions about Python is the Python documentation. Here’s the link to the expressions page. The subsection on binary arithmetic operations includes information on the mod operator. To better understand floating-point, you can look at the Python floating-point documentation, or Wikipedia has an article on the IEEE 754 standard that specifies how floating-point arithmetic is meant to work.

03:09 If cryptography is your thing, the course on exploring HTTPS and cryptography shows you how to use mod to create Caesar ciphers as well as the generation of RSA keys.

03:23 Here’s the link to the Python decimal module, if you wish to dig into that a little further. And this page describes all of the dunder methods and what operators map to them. I hope this course has been useful for you.

03:38 Thanks for your attention.

Become a Member to join the conversation.