TalentJR

Lesson 9

The Modulo Operator

Find remainders and detect patterns

1What is the Modulo Operator?✨

The modulo operator (%) is a special tool in Python that helps us find the remainder after dividing two numbers. It’s useful for various tasks like checking if a number is even or odd!

2Why Use the Modulo Operator?🎯

  • ✓ Determine remainders in division
  • ✓ Check if numbers are divisible
  • ✓ Identify patterns in numbers
  • ✓ Useful in games and simulations

3Basic Usage💡

To use the modulo operator, simply write the two numbers with the % symbol in between. For example:

💻 code
result = 10 % 3
print(result)

This code will output 1 because when you divide 10 by 3, the remainder is 1.

4Finding Even and Odd Numbers🚀

You can use the modulo operator to find out if a number is even or odd. If a number modulo 2 equals 0, it’s even; otherwise, it’s odd. Here’s how:

💻 code
number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

This program checks if the number 7 is even or odd and will print "Odd".

5Detecting Patterns🔍

The modulo operator can help us find patterns in sequences. For instance, let’s print numbers from 1 to 20 and mark multiples of 5:

💻 code
for i in range(1, 21):
    if i % 5 == 0:
        print(i, "is a multiple of 5")
    else:
        print(i)

This code checks each number from 1 to 20 and tells us which ones are multiples of 5!

6Practice Challenge⭐

Now it’s your turn! Write a program that prints numbers from 1 to 30 and indicates whether they are divisible by 3, 5, or both. Use the modulo operator and if-else statements to complete this challenge. Happy coding!

🎯 Practice Challenges

Master The Modulo Operator by completing all 3 difficulty levels

🟢 EASY

The Modulo Operator - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

The Modulo Operator - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

The Modulo Operator - Hard Challenge

Master this concept. Prove your skills!

🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →

Ready to Master The Modulo Operator? 🚀

Complete the challenges, take the quiz and keep your learning streak going!