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.
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.
Six questions
Python knows six comparison signs. Each one is a different question:
| Sign | Question | Example | Answer |
|---|---|---|---|
> | greater? | 7 > 5 | True |
< | less? | 7 < 5 | False |
>= | greater or equal? | 5 >= 5 | True |
<= | less or equal? | 4 <= 3 | False |
== | equal? | 6 == 6 | True |
!= | different? | 6 != 6 | False |
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.
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.
1print(7 > 5)2print(3 > 8)3print(4 == 4)4print(6 != 6)5print(2 <= 9)
โOne sign = or two ==?
This is very important and easy to mix up:
| Code | What it does |
|---|---|
age = 12 | puts 12 into the box age ๐ฆ |
age == 12 | asks: 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.
1age = 122print(age == 12)3print(age == 13)4print(age > 10)
โA ride from 120 cm ๐ฌ
Click Next - you'll see how >= works.
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.
The limit on the scale
The same 12 twice - once with >, once with >=.
Click the arrow - we'll put every comparison on the scale.
1print(12 > 12)2print(12 >= 12)3print(11 <= 12)
โCan you get on the ride? ๐ข
Type your height in cm ๐ (e.g. 135) and press Enter โ. Try 120 too - the limit itself!
Click the arrow - we'll measure the height by the ruler.
1height_str = input()2height = int(height_str)3print("You can ride:", height >= 120)
โ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.
1ola_sweets = 4 + 32kuba_sweets = 2 * 33print(ola_sweets > kuba_sweets)4print(ola_sweets == kuba_sweets + 1)
โ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.
1number_str = input()2number = int(number_str)3print("Got it:", number == 7)4print("Too small:", number < 7)5print("Too big:", number > 7)
โWhat will this program print?
print(10 > 3)
What will this program print?
print(4 >= 4)
What will this program print?
x = 3print(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 >= 120is True, but120 > 120is False.- One
=puts into a box, two==ask whether equal. - Calculating first, then comparing:
2 + 3 == 5is 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.