Python Modulo In Practice: How To Use The % Operator

Table of Contents

  • Modulo in Mathematics
  • Python Modulo Operator Basics
    • Modulo Operator With int
    • Modulo Operator With float
    • Modulo Operator With a Negative Operand
    • Modulo Operator and divmod()
    • Modulo Operator Precedence
  • Python Modulo Operator in Practice
    • How to Check if a Number Is Even or Odd
    • How to Run Code at Specific Intervals in a Loop
    • How to Create Cyclic Iteration
    • How to Convert Units
    • How to Determine if a Number Is a Prime Number
    • How to Implement Ciphers
  • Python Modulo Operator Advanced Uses
    • Using the Python Modulo Operator With decimal.Decimal
    • Using the Python Modulo Operator With Custom Classes
  • Conclusion
Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Modulo: Using the % Operator

Python supports a wide range of arithmetic operators that you can use when working with numbers in your code. One of these operators is the modulo operator (%), which returns the remainder of dividing two numbers.

In this tutorial, you’ll learn:

  • How modulo works in mathematics
  • How to use the Python modulo operator with different numeric types
  • How Python calculates the results of a modulo operation
  • How to override .__mod__() in your classes to use them with the modulo operator
  • How to use the Python modulo operator to solve real-world problems

The Python modulo operator can sometimes be overlooked. But having a good understanding of this operator will give you an invaluable tool in your Python tool belt.

Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Modulo in Mathematics

The term modulo comes from a branch of mathematics called modular arithmetic. Modular arithmetic deals with integer arithmetic on a circular number line that has a fixed set of numbers. All arithmetic operations performed on this number line will wrap around when they reach a certain number called the modulus.

A classic example of modulo in modular arithmetic is the twelve-hour clock. A twelve-hour clock has a fixed set of values, from 1 to 12. When counting on a twelve-hour clock, you count up to the modulus 12 and then wrap back to 1. A twelve-hour clock can be classified as “modulo 12,” sometimes shortened to “mod 12.”

The modulo operator is used when you want to compare a number with the modulus and get the equivalent number constrained to the range of the modulus.

For example, say you want to determine what time it would be nine hours after 8:00 a.m. On a twelve-hour clock, you can’t simply add 9 to 8 because you would get 17. You need to take the result, 17, and use mod to get its equivalent value in a twelve-hour context:

Text 8 o'clock + 9 = 17 o'clock 17 mod 12 = 5

Từ khóa » Hàm Mod Trong Python