modulo operator
In Python, the modulo operator (%) returns the remainder left over after dividing one number by another. For example, 17 % 5 gives you 2 because five fits into seventeen three times with two to spare. You’ll use it whenever your code needs to wrap around a cycle, like checking whether a number is even or finding the weekday that falls some number of days from today.
Python’s modulo pairs with floor division (//), and the two always satisfy the identity x == (x // y) * y + x % y. Because floor division rounds toward negative infinity, any nonzero result of % takes the sign of the divisor. That’s why -7 % 3 is 2 rather than -1, and 7 % -3 is -2.
Drag the sliders below to move x and y and watch where floor division lands and how far the remainder reaches from there to x. Flip y negative and the remainder flips sign with it:
The operator works with floats too, so 7.5 % 2 returns 1.5. A zero divisor raises ZeroDivisionError, and complex numbers don’t support modulo at all. The built-in divmod() function returns the quotient and the remainder together as a tuple.
Strings overload % for printf-style string formatting, so "%d items" % 3 builds a string instead of computing a remainder. Your own classes can support the operator by defining the .__mod__() special method.
Example
Say you’re building a calendar feature and need to know which day of the week a deadline lands on, counting from a Friday:
>>> days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
>>> today = 4 # Fri
>>> days[(today + 10) % 7]
'Mon'
>>> days[(today - 12) % 7]
'Sun'
Adding ten days pushes the index past the end of the list, and % 7 wraps it back into the range 0 to 6. Going backward by twelve days produces a negative number, but since the result takes the sign of the positive divisor, modulo still returns a valid index.
Related Resources
Tutorial
Python Modulo Operator (%): How to Use Mod in Python
Learn how to use the Python modulo operator (%) for remainders, modular arithmetic, and more. Covers mod with ints, floats, and practical examples.
For additional information on related topics, take a look at the following resources:
- Python Modulo: Using the % Operator (Course)
- Python Operators and Expressions (Course)
- Modulo String Formatting in Python (Tutorial)
- How to Round Numbers in Python (Tutorial)
- Rounding Numbers in Python (Course)
- Rounding Numbers in Python (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 30, 2026