Skip to content

Comparisons and elif

Last lesson you saw >= and ==. Now let’s meet the whole set.

SymbolMeaningExampleResult
==equal to5 == 5True
!=not equal5 != 3True
>greater than5 > 7False
<less than5 < 7True
>=greater than or equal5 >= 5True
<=less than or equal5 <= 4False

True and False are Python’s “yes” and “no”. Put a comparison inside print and you’ll see the answer directly:

Inputs (for input())

One value per line — read in order by input().

Output
 

When two paths aren’t enough, elif (else if) comes to the rescue. Python checks the conditions top to bottom and stops at the first true one:

Inputs (for input())

One value per line — read in order by input().

Output
 

Change score and try to hit all four answers. Try 95, 60, 30!

Mission

True or false?

+10 XP

Write three comparisons and print their results — at least one True and one False.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Weather advisor

+15 XP

Give advice based on the temperature: 30°+ hot, 15–29° mild, below 15° cold. (The input is 22.)

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Odd or even?

+15 XP

Our old friend % is back: if the number is even print even, if it’s odd print odd. (The input is 7.)

Inputs (for input())

One value per line — read in order by input().

Output
 

Next lesson: we learn to combine several conditions — and, or, not. Continue →