The Python Modulo Operator - What Does the % Symbol Mean in Python? | Python Modulo in Practice|#20
Programming Guru
The Python Modulo Operator - What Does the % Symbol Mean in Python?
When you see the % symbol, you may think "percent". But in Python, as well as most other programming languages, it means something different.
The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
The modulo operator is considered an arithmetic operation, along with +, -, /, *, **, //.
The basic syntax is:
a % b
In the previous example a is divided by b, and the remainder is returned. Let's see an example with numbers. the previous example a is divided by b, and the remainder is returned. Let's see an example with numbers.
7 % 2
The result of the previous example is one. Two goes into seven three times and there is one left over.
The diagram below shows a visual representation of 7 / 2 and 7 % 2 (The "R" stands for "remainder"). The single logo on the right side (with the green arrow pointing at it) is the remainder from the division problem. It is also the answer to 7 % 2.
This will result in three. Four does not go into three any times so the original three is still left over. The diagram below shows what is happening. Remember, the modulo operator returns the remainder after performing division. The remainder is three.


Python Modulo in Practice: How to Use the % Operator
by Jason Van Schooneveld 8 Comments basics python
Mark as Completed
Tweet Share Email
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


Python Modulo in Practice: How to Use the % Operator
by Jason Van Schooneveld 8 Comments basics python
Mark as Completed
Tweet Share Email
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.
Example using the Modulo Operator
One common use for the Modulo Operator is to find even or odd numbers. The code below uses the modulo operator to print all odd numbers between 0 and 10. ... https://www.youtube.com/watch?v=_R_Yi-4sp0M
7799799 Bytes