AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 19

Comparisons: greater, less, equal

After this lesson: You'll ask Python a question about two numbers and get the answer True or False.

Video

?

The video for this lesson is coming soon.

Explanation

Welcome to module 2! ๐ŸŽ‰ So far your programs always did the same thing - line by line, from top to bottom. Now we'll teach them to decide: one thing for a 10-year-old, another for a 15-year-old. But to decide, you first need to be able to ask a question. For example: is 7 greater than 5?

Who has more sweets? ๐ŸŽฌ

Click Next and see how Python answers questions.

?Bajtek
I have the same!๐Ÿง’
๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐ŸฌOla: 7โš–๏ธ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐ŸฌKuba: 5
1

An argument about sweets

Ola has 7 sweets ๐Ÿฌ, and Kuba 5. Kuba says: "I have the same!". Ola: "Not true, I have more!". Bajtek takes a scale to check.

Step 1 / 6

Six questions

Python knows six comparison signs. Each one is a different question:

SignQuestionExampleAnswer
>greater?7 > 5True
<less?7 < 5False
>=greater or equal?5 >= 5True
<=less or equal?4 <= 3False
==equal?6 == 6True
!=different?6 != 6False

The signs >=, <=, == and != are two characters written together, with no space. The equals sign always comes second: >=, not =>. The exclamation mark ! in != means "not": not equal.

Play with the scale โš–๏ธ

Set two numbers with the buttons - and see which of the six questions are answered True.

a = 7
b = 5
๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ๐ŸŽ
๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ
a < blessFalse
a > bgreaterTrue
a <= bless or equalFalse
a >= bgreater or equalTrue
a == bequalFalse
a != bnot equalTrue

Change a and b with the buttons - and watch which comparisons are true. Try setting two equal numbers!

The scale answers questions

We put every comparison on the scale: the bigger number weighs more, and the lamp shows True or False.

?

Click the arrow - we'll put every comparison on the scale.

Code
1print(7 > 5)
2print(3 > 8)
3print(4 == 4)
4print(6 != 6)
5print(2 <= 9)
Screen
โ–
1/6 ยท line 1 runs next

One sign = or two ==?

This is very important and easy to mix up:

CodeWhat it does
age = 12puts 12 into the box age ๐Ÿ“ฆ
age == 12asks: is there 12 in the box age? โ“

One = sign is putting in, two signs == are a question. We can compare variables too - Python first looks into the box, then compares what's inside.

Putting in and asking

Line 1 puts 12 into the box. Then three questions about what's inside.

?

Click the arrow - we'll put every comparison on the scale.

Code
1age = 12
2print(age == 12)
3print(age == 13)
4print(age > 10)
Screen
โ–
1/5 ยท line 1 runs next

A ride from 120 cm ๐ŸŽฌ

Click Next - you'll see how >= works.

Bajtek
๐ŸŽขentry from 120 cm๐Ÿ“
1

The sign by the ride

By the ride ๐ŸŽข there's a sign: entry from 120 cm. Bajtek has to measure the children and let in only those who may ride.

Step 1 / 3

The limit on the scale

The same 12 twice - once with >, once with >=.

?

Click the arrow - we'll put every comparison on the scale.

Code
1print(12 > 12)
2print(12 >= 12)
3print(11 <= 12)
Screen
โ–
1/4 ยท line 1 runs next

Can you get on the ride? ๐ŸŽข

Type your height in cm ๐Ÿ“ (e.g. 135) and press Enter โŽ. Try 120 too - the limit itself!

0
20
40
60
80
100
120
140
160
120 cm

Click the arrow - we'll measure the height by the ruler.

Code
1height_str = input()
2height = int(height_str)
3print("You can ride:", height >= 120)
Screen
โ–
1/2 ยท line 1 runs next

First calculating, then the question

There can be calculations on both sides of a comparison. Python calculates first, and only then compares. 2 + 3 == 5 is: first 2 + 3 gives 5, then the question 5 == 5 - True.

Calculate and weigh

The results go on the scale already calculated.

?

Click the arrow - we'll put every comparison on the scale.

Code
1ola_sweets = 4 + 3
2kuba_sweets = 2 * 3
3print(ola_sweets > kuba_sweets)
4print(ola_sweets == kuba_sweets + 1)
Screen
โ–
1/5 ยท line 1 runs next

Guess Bajtek's number ๐Ÿ”ข

Bajtek is thinking of the number 7. Type a number from 1 to 10 ๐Ÿ”ข and press Enter โŽ - the program says whether you guessed it.

?

Click the arrow - we'll put every comparison on the scale.

Code
1number_str = input()
2number = int(number_str)
3print("Got it:", number == 7)
4print("Too small:", number < 7)
5print("Too big:", number > 7)
Screen
โ–
1/2 ยท line 1 runs next
?

What will this program print?

print(10 > 3)
?

What will this program print?

print(4 >= 4)
?

What will this program print?

x = 3
print(x != 3)
?

Which line ASKS whether the box holds 5?

Remember

  • A comparison is a question - Python answers True or False.
  • Six signs: > < >= <= == !=.
  • >= and <= count the limit too: 120 >= 120 is True, but 120 > 120 is False.
  • One = puts into a box, two == ask whether equal.
  • Calculating first, then comparing: 2 + 3 == 5 is True.

Now it's your turn! In the tasks you'll read numbers and print Python's answer: True or False.

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.