TL ; DR:
The modulo operator (%
) calculates the remainder of dividing two values:
print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2
It can also be used with variables:
a = 2
b = 4
print(b % a) # Output: 0
print(11 % b) # Output: 3
Full lesson:
Remember how we first learn about the division of two integer numbers in primary school?
The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.
Here are some examples:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
Floor division
Floor division (//
) is a normal division operation except that it returns the integral part of the result (the quotient):
print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6
It can also be used with variables:
a = 3
b = 7
print(20 // a) # Output: 6
print(b // a) # Output: 2
Modulo
The modulo operator (%
) calculates the remainder of dividing two values:
print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2
# Can be used with variables:
a = 2
b = 4
print(b % a) # Output: 0
print(11 % b) # Output: 3
Quotient and remainder
In programming, we combine both these concepts to get the quotient and remainder of some divison:
# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3
print(quotient) # Output: 8
print(remainder) # Output: 2
Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!
Hint
Look at the examples above if you get stuck.
The modulo operator is a fundamental concept in programming that calculates the remainder of a division operation. It is widely used in various applications, such as determining if a number is even or odd, cycling through array indices, and implementing algorithms that require periodicity. Understanding the modulo operator is essential for solving many programming problems efficiently.
The modulo operator (%) returns the remainder of a division between two numbers. For example, 15 % 4
equals 3 because 15 divided by 4 leaves a remainder of 3. This operator is particularly useful when you need to wrap around values, such as in circular buffers or when working with cyclic data structures.
Here are some simple examples to illustrate the concept:
print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2
Understanding these basics is crucial before moving on to more complex applications of the modulo operator.
The key concept behind the modulo operator is its ability to determine the remainder of a division operation. This can be applied in various ways:
number % 2 == 0
).Let's see how to apply these concepts with clear examples:
# Check if a number is even or odd
number = 7
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
# Wrapping around an array
array = [1, 2, 3, 4, 5]
index = 7
wrapped_index = index % len(array)
print(array[wrapped_index]) # Output: 3
Let's explore some real-world use cases where the modulo operator is beneficial:
# Example 1: Determine if a year is a leap year
year = 2024
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
# Example 2: Cycling through a list
colors = ["red", "green", "blue"]
for i in range(10):
print(colors[i % len(colors)]) # Output: red, green, blue, red, green, blue, ...
When using the modulo operator, there are some common mistakes to avoid:
Best practices include writing clear and maintainable code, using descriptive variable names, and adding comments to explain the logic.
Advanced techniques involving the modulo operator include:
Here's an example combining advanced techniques with basics:
# Simple hash function using modulo
def simple_hash(key, size):
return key % size
print(simple_hash(12345, 100)) # Output: 45
Below is a well-commented code snippet demonstrating the correct use of the modulo operator:
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
# Test the function
print(is_prime(11)) # Output: True
print(is_prime(15)) # Output: False
When debugging code involving the modulo operator, consider the following tips:
Writing tests for functions using the modulo operator is crucial. Here are some test cases:
import unittest
class TestModulo(unittest.TestCase):
def test_is_prime(self):
self.assertTrue(is_prime(11))
self.assertFalse(is_prime(15))
if __name__ == "__main__":
unittest.main()
When approaching problems related to the modulo operator, consider the following strategies:
In this lesson, we covered the modulo operator in Python, its significance, and various applications. Mastering the modulo operator is essential for solving many programming problems efficiently. Practice and explore further applications to deepen your understanding.
For further reading and practice problems, consider the following resources: