Comparisons and elif
Last lesson you saw >= and ==. Now let’s meet the whole set.
Comparison operators
Section titled “Comparison operators”| Symbol | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal | 5 != 3 | True |
> | greater than | 5 > 7 | False |
< | less than | 5 < 7 | True |
>= | greater than or equal | 5 >= 5 | True |
<= | less than or equal | 5 <= 4 | False |
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().
elif — “no, but what about this?”
Section titled “elif — “no, but what about this?””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().
Change score and try to hit all four answers. Try 95, 60, 30!
Missions
Section titled “Missions”True or false?
+10 XP ✓ CompletedWrite 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().
Weather advisor
+15 XP ✓ CompletedGive 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().
Odd or even?
+15 XP ✓ CompletedOur 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().
Next lesson: we learn to combine several conditions — and, or, not. Continue →