If/Else Decisions
Make your code choose — if, elif, else, and comparison operators.
So I've got functions, I've got f-strings, I can take input and produce formatted output. But every function we've written does the same thing every time. In my spreadsheets I use IF formulas all the time — "if this cell is above 90, show 'A', otherwise show 'B'." Can Python do that?
Absolutely. That's what if statements are for. An if checks a condition, and if it's True, the indented code below it runs. If it's False, Python skips it:
def check_age(age):
if age >= 18:
return "You can vote"
return "Too young to vote"
When age is 20, the condition age >= 18 is True, so Python returns "You can vote" and never reaches the second line. When age is 15, the condition is False, Python skips the indented block, and hits the fallback return.
OK that's basically my IF formula but vertical instead of horizontal. What about when I have more than two outcomes? Like letter grades — A, B, C, D, F. In Excel I end up with nested IFs and it's a nightmare.
Python handles this much more cleanly with elif — short for "else if." You just stack the conditions top to bottom, and Python runs the first one that matches:
def letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
The else at the bottom is the catch-all — it runs only if nothing above it matched. For a score of 85, Python asks: is 85 >= 90? No. Is 85 >= 80? Yes. Returns "B" and stops. It never even checks the remaining conditions.
Oh that's so much better than nesting five IF statements inside each other. What comparison operators can I use besides >=?
Python gives you six:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Each one produces a boolean — True or False. You can also combine conditions with and and or:
def can_ride(height, age):
if height >= 120 and age >= 8:
return "Welcome aboard!"
else:
return "Sorry, not this time."
With and, both conditions must be True. With or, at least one must be True.
Wait — I just realized something. Yesterday we used = to assign variables, like name = "Alice". But up there you used == to check equality. What's the difference?
This is one of the most common beginner mistakes, so good catch.
=is assignment:x = 5stores the value 5 inx==is comparison:x == 5asks "is x equal to 5?" and returnsTrueorFalse
x = 10 # assignment — x now holds 10
x == 10 # comparison — True
x == 5 # comparison — False
If you accidentally write if x = 5: instead of if x == 5:, Python raises a SyntaxError. It's protecting you from the mistake.
Good, so Python won't let me shoot myself in the foot there. One more thing — does the order of the elif blocks actually matter?
It matters a lot. Python stops at the first match. If you put score >= 60 first, every passing score would get a "D" — because 90 is also >= 60. Always put the most specific condition (highest threshold) first and work your way down.
Your turn now. Write a grading function yourself.
Practice your skills
Sign up to write and run code in this lesson.
If/Else Decisions
Make your code choose — if, elif, else, and comparison operators.
So I've got functions, I've got f-strings, I can take input and produce formatted output. But every function we've written does the same thing every time. In my spreadsheets I use IF formulas all the time — "if this cell is above 90, show 'A', otherwise show 'B'." Can Python do that?
Absolutely. That's what if statements are for. An if checks a condition, and if it's True, the indented code below it runs. If it's False, Python skips it:
def check_age(age):
if age >= 18:
return "You can vote"
return "Too young to vote"
When age is 20, the condition age >= 18 is True, so Python returns "You can vote" and never reaches the second line. When age is 15, the condition is False, Python skips the indented block, and hits the fallback return.
OK that's basically my IF formula but vertical instead of horizontal. What about when I have more than two outcomes? Like letter grades — A, B, C, D, F. In Excel I end up with nested IFs and it's a nightmare.
Python handles this much more cleanly with elif — short for "else if." You just stack the conditions top to bottom, and Python runs the first one that matches:
def letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
The else at the bottom is the catch-all — it runs only if nothing above it matched. For a score of 85, Python asks: is 85 >= 90? No. Is 85 >= 80? Yes. Returns "B" and stops. It never even checks the remaining conditions.
Oh that's so much better than nesting five IF statements inside each other. What comparison operators can I use besides >=?
Python gives you six:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Each one produces a boolean — True or False. You can also combine conditions with and and or:
def can_ride(height, age):
if height >= 120 and age >= 8:
return "Welcome aboard!"
else:
return "Sorry, not this time."
With and, both conditions must be True. With or, at least one must be True.
Wait — I just realized something. Yesterday we used = to assign variables, like name = "Alice". But up there you used == to check equality. What's the difference?
This is one of the most common beginner mistakes, so good catch.
=is assignment:x = 5stores the value 5 inx==is comparison:x == 5asks "is x equal to 5?" and returnsTrueorFalse
x = 10 # assignment — x now holds 10
x == 10 # comparison — True
x == 5 # comparison — False
If you accidentally write if x = 5: instead of if x == 5:, Python raises a SyntaxError. It's protecting you from the mistake.
Good, so Python won't let me shoot myself in the foot there. One more thing — does the order of the elif blocks actually matter?
It matters a lot. Python stops at the first match. If you put score >= 60 first, every passing score would get a "D" — because 90 is also >= 60. Always put the most specific condition (highest threshold) first and work your way down.
Your turn now. Write a grading function yourself.
Practice your skills
Sign up to write and run code in this lesson.