AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 24

Divisibility - % in conditions

After this lesson: You'll check whether a number divides by another with no remainder, and recognise even numbers.

Video

?

The video for this lesson is coming soon.

Explanation

Remember % from lesson 5? It gives the remainder of a division: 14 % 3 is 2, because 14 = 4 ยท 3 + 2. Today we'll use it for the most important question: does something divide with no remainder? It's the key to lots of contest tasks! ๐Ÿ”‘

Teams in PE class ๐ŸŽฌ

Click Next - you'll see what dividing with no remainder means.

?Bajtek
๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’๐Ÿง’
1

Teams of three

There are 12 children in PE, and the teacher splits them into teams of 3 for 3x3 basketball. We line the children up in rows of 3...

Step 1 / 4

Will the teams be equal?

14 children in rows of 3 - watch what is left at the end.

๐Ÿง’ ๐Ÿง’ ๐Ÿง’ | ๐Ÿง’ ๐Ÿง’ ๐Ÿง’ | ๐Ÿง’

Click the arrow - we'll lay the number out in rows and see whether anything is left.

Code
1children = 14
2if children % 3 == 0:
3 print("Equal teams!")
4else:
5 print("Someone is left without a team")
Screen
โ–
1/4 ยท line 1 runs next

15 children

One more child - and everything fits.

๐Ÿ€1children = 15
if children % 3 == 0
3print("Equal teams!")
โœ…
elsein every other case
5print("Someone is left without a team")
๐Ÿ˜•

Click the arrow - I'll walk the program's path, line by line.

Code
1children = 15
2if children % 3 == 0:
3 print("Equal teams!")
4else:
5 print("Someone is left without a team")
Screen
โ–
1/4 ยท line 1 runs next

Even or odd?

The most common question about divisibility is: is the number even? Even = divides by 2 with no remainder:

ConditionMeans
n % 2 == 0n is even (2, 4, 10, 0, -6...)
n % 2 != 0n is odd (1, 7, 15, -3...)

The pairs of socks ๐Ÿงฆ from lesson 5: an even number of socks - all of them have a pair. Odd - one is left alone.

Socks in pairs ๐Ÿงฆ

Type a number of socks ๐Ÿงฆ (e.g. 7, at most 60) and press Enter โŽ - you'll see whether every one has a pair.

๐Ÿงฆ ๐Ÿงฆ ๐Ÿงฆ | ๐Ÿงฆ ๐Ÿงฆ ๐Ÿงฆ | ๐Ÿงฆ

Click the arrow - we'll lay the number out in rows and see whether anything is left.

Code
1socks_str = input()
2socks = int(socks_str)
3if socks % 2 == 0:
4 print("Even - every sock has a pair")
5else:
6 print("Odd - one is left alone")
Screen
โ–
1/2 ยท line 1 runs next

By any number you like

We check divisibility by any number the same way:

  • n % 5 == 0 - ends in 0 or 5 (5, 10, 35, 100...),
  • n % 10 == 0 - ends in 0 (a round number),
  • n % 7 == 0 - it's in the 7 times table (7, 14, 21...),
  • a % b == 0 - b is a divisor of a (e.g. 3 is a divisor of 12).

Egg boxes ๐Ÿฅš

A box holds 6 eggs. Type a number of eggs ๐Ÿฅš (at most 60) and press Enter โŽ - will all the boxes be full?

๐Ÿฅš ๐Ÿฅš ๐Ÿฅš | ๐Ÿฅš ๐Ÿฅš ๐Ÿฅš | ๐Ÿฅš

Click the arrow - we'll lay the number out in rows and see whether anything is left.

Code
1eggs_str = input()
2eggs = int(eggs_str)
3if eggs % 6 == 0:
4 print("All the boxes are full!")
5else:
6 print("One box isn't full")
Screen
โ–
1/2 ยท line 1 runs next

The Pif-Paf game ๐ŸŽฌ

Click Next - a game where the order of the conditions is a trap.

Bajtek
3 โžก๏ธ Pif5 โžก๏ธ Paf15 โžก๏ธ PifPaf
1

The rules

The children count in turn. Whoever gets a number divisible by 3 says Pif. Divisible by 5 - Paf. And divisible by both 3 and 5 (15, 30...) - PifPaf! Otherwise you just say the number.

Step 1 / 3

Play Pif-Paf ๐ŸŽฒ

Type a number ๐Ÿ”ข (e.g. 9, 10, 30 or 7) and press Enter โŽ - what do you have to say?

๐ŸŽฒ1n_str = input()
2n = int(n_str)
if n % 15 == 0
4print("PifPaf")
๐Ÿ’ฅ
elif n % 3 == 0
6print("Pif")
๐Ÿ”ต
elif n % 5 == 0
8print("Paf")
๐ŸŸ 
elsein every other case
10print(n)
๐Ÿ”ข

Click the arrow - I'll walk the program's path, line by line.

Code
1n_str = input()
2n = int(n_str)
3if n % 15 == 0:
4 print("PifPaf")
5elif n % 3 == 0:
6 print("Pif")
7elif n % 5 == 0:
8 print("Paf")
9else:
10 print(n)
Screen
โ–
1/2 ยท line 1 runs next
?

Which condition checks whether n is odd?

?

What will this program print?

n = 20
if n % 6 == 0:
print("A")
else:
print(n % 6)
?

Which number should be checked first in Pif-Paf?

Remember

  • n % k == 0 - n divides by k with no remainder.
  • n % 2 == 0 - even, n % 2 != 0 - odd.
  • a % b == 0 means b is a divisor of a.
  • When conditions are inside each other (15 โ†’ 3 and 5), check the most demanding one first.

On to the tasks! Each one is a question: does something divide with no remainder?

Tasks from the video

We solve them together in the video - pause it and try first on your own.

Practice tasks

Try to solve them without the video. Stuck? Every task has hints and an editorial.

Sign in to save your progress.