Session 4 of the Python Fundamentals series, covered in depth. It makes the program choose using if, elif, and else, starting with the colon and the indentation that mark a block. It covers the comparison operators, else and elif chains checked in order so that the first true branch wins, the difference between == and =, comparing strings and converted input, nested ifs, and boundary conditions. It ends with the classic traps: separate ifs that all fire, using = where == was meant, and indentation errors. Every snippet and error message was copied verbatim from CPython 3.12.
Subject: Python Fundamentals · 98 slides · code lesson
Open the interactive version of this deck · Homework for this lesson
Title
Python Fundamentals - Session 4
if / elif / else - choosing what to do
Objectives
Until now every line always ran. Now the program will pick a path. By the end you can:
if block that runs only when a condition is true.else for the fallback and elif for extra cases.elif chain is checked in order - first true branch wins.== (not =) to compare, and compare text and converted input.= vs ==, and indentation errors.Warm-up
Discussion prompt
Before we open Session 4 - Conditionals (if / elif / else): without looking back, what was the main idea of Session 3 - Input & Output, and what could you do by the end of it that you could not do before?
Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.
Answer:
That session covers why input() returns text, how to convert it with int() before doing math, and how to produce clean output with commas and f-strings.
Section
Part 1
Concept
if condition: runs the indented block below it only when the condition is true. If it is false, that block is skipped.
Two pieces of punctuation carry the meaning: the colon ends the if line, and the indentation marks which lines are inside.
block — The indented lines under an if/elif/else. The indentation - not braces - is how Python knows which lines belong to that branch.
Counterexample
Discussion prompt
Two pieces of punctuation carry the meaning: the colon ends the if line, and the indentation marks which lines are inside.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Intuition
Picture the program walking down a path and reaching a fork. The condition is the sign: true goes one way, false the other.
if alone is a side trail that only some walkers take; if / else is a two-way fork that everyone takes one side of.
Analogy
Discussion prompt
Explain A fork in the road by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
Picture the program walking down a path and reaching a fork. The condition is the sign: true goes one way, false the other.
Worked example
score = 75
if score >= 60:
print("You passed!")Line 2 checks: is 75 >= 60?
Why: 75 is at least 60, so the condition is true.
The indented line runs
Why: Verified by execution: prints You passed! If score were below 60, nothing would print.
| score | score >= 60 | prints |
|---|---|---|
| 75 | True | You passed! |
| 40 | False | (nothing) |
Comparison
Comparison matrix
From A single if: refill the score >= 60 column from what you know. The rest of the table is as it appeared.
| score | score >= 60 | prints |
|---|---|---|
| 75 | True | You passed! |
| 40 | False | (nothing) |
Concept
Every line indented under the if is part of the block, and they all run together when the condition is true.
Line them up at the same indentation - Python uses that alignment to group them.
Explain it
Discussion prompt
Explain A block can be several lines to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
Every line indented under the if is part of the block, and they all run together when the condition is true.
Worked example
temp = 95
if temp > 90:
print("It is hot.")
print("Drink water.")Both indented lines belong to the if
Why: They share the same indentation, so both run when temp > 90 is true.
Read the output
Why: Verified by execution: two lines print because the condition held.
| condition | lines that run |
|---|---|
| temp > 90 = True | It is hot. |
| Drink water. |
Trade off
Comparison matrix
From Two lines inside one if: every row here is a choice with a cost. Fill the lines that run column, then say which row you would actually pick and what you give up for it.
| condition | lines that run |
|---|---|
| temp > 90 = True | It is hot. |
| Drink water. |
Concept
Conditions are built from comparisons: == equal, != not equal, < less, > greater, <= at most, >= at least.
Each one produces a bool - True or False - which the if then acts on.
Pattern
Predict first
The table runs: 5 == 5 | True · 5 != 3 | True · 4 < 7 | True
In What each comparison gives, given the rows so far: what is the next one — the row where expression is 8 >= 10?
Correct: 8 >= 10 | False
| expression | value |
|---|---|
| 5 == 5 | True |
| 5 != 3 | True |
| 4 < 7 | True |
| 8 >= 10 | False |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. The comparison is computed just like arithmetic, but the result is a bool.
Worked example
print(5 == 5)
print(5 != 3)
print(4 < 7)
print(8 >= 10)Each line evaluates to True or False
Why: The comparison is computed just like arithmetic, but the result is a bool.
Read the output
Why: Verified by execution.
| expression | value |
|---|---|
| 5 == 5 | True |
| 5 != 3 | True |
| 4 < 7 | True |
| 8 >= 10 | False |
Discrimination
Sort into buckets
Sort these by value, from memory, without looking back at What each comparison gives. Telling them apart on the spot is the skill; the table is only where the answer happens to be written down.
Section
Part 2
Concept
else: runs when the if condition was false. Together they guarantee exactly one branch runs.
else has no condition of its own - it is the catch-all for everything the if did not match.
Worked example
score = 55
if score >= 60:
print("Pass")
else:
print("Try again")55 >= 60 is false, so the if block is skipped
Why: The condition fails, so Python looks to else.
The else block runs instead
Why: Verified by execution: prints Try again.
| score | score >= 60 | branch | prints |
|---|---|---|---|
| 55 | False | else | Try again |
Error analysis
Annotate
Walk the callouts on Pass or try again. Each one is a place this is easy to get subtly wrong.
Concept
For several categories, chain with elif ('else if'). Python checks each condition top to bottom and runs the block of the first true one.
Once a branch runs, the rest are skipped. A final else catches whatever is left.
Ranking
Put in order
Put the moves of A letter grade into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. First true condition wins; the chain ends here.
Worked example
score = 83
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")83 >= 90? No - skip A
Why: First condition false, move on.
83 >= 80? Yes - print B and stop
Why: First true condition wins; the chain ends here.
The rest never run
Why: Verified by execution: prints B.
| condition | true? | action |
|---|---|---|
| score >= 90 | False | skip |
| score >= 80 | True | print B, stop |
| score >= 70 | - | not checked |
| else | - | not reached |
Discrimination
Sort into buckets
Sort these by true?, from memory, without looking back at A letter grade. Telling them apart on the spot is the skill; the table is only where the answer happens to be written down.
Concept
Because the first true condition wins, the most specific / strictest condition must come first.
Put >= 90 before >= 80. If the looser one comes first, it grabs high scores before the stricter one is ever checked.
Anomaly
Predict first
A student writes this, and it looks reasonable:
Grades with the cutoffs in the wrong order. score is 95.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: The loose condition matches every high score, so the >= 90 branch is never reached.
Strictest cutoff first.
Why: The loose condition matches every high score, so the >= 90 branch is never reached. A 95 wrongly gets a C.
Trap
Grades with the cutoffs in the wrong order. score is 95.
if score >= 70:
print("C")
elif score >= 90:
print("A")95 >= 70 is true first, so it prints C
Why: The loose condition matches every high score, so the >= 90 branch is never reached. A 95 wrongly gets a C.
| score | prints | wanted |
|---|---|---|
| 95 | C | A |
Strictest cutoff first.
if score >= 90:
print("A")
elif score >= 70:
print("C")Now 95 hits >= 90 first
Why: The strict condition is checked before the loose one, so a 95 correctly prints A.
| score | prints |
|---|---|
| 95 | A |
Break the constraint
Discussion prompt
The rule this trap just fixed:
The strict condition is checked before the loose one, so a 95 correctly prints A.
Now break it on purpose. Build a case that violates it and follow the consequences until something visibly fails. Where does the failure first show up — and would you have noticed it if you had not been looking?
Hint: The dangerous rules are the ones whose violation still produces an answer. If yours fails loudly, try to find one that fails quietly.
Answer:
The loose condition matches every high score, so the >= 90 branch is never reached. A 95 wrongly gets a C.
Concept
An if / elif / ... / else picks exactly one block. After it runs, execution jumps past the whole chain.
This is the key difference from separate if statements, which each decide on their own (and can all run).
Worked example
temp = 5
if temp < 10:
print("cold")
elif temp < 20:
print("mild")
else:
print("warm")temp < 10 is true, so print cold
Why: The first true branch runs.
The mild and warm branches are skipped
Why: Verified by execution: exactly one label, cold - even though temp < 20 is also true.
| branch | true? | runs? |
|---|---|---|
| temp < 10 | True | yes |
| temp < 20 | True | no - already done |
| else | - | no |
Blank canvas
Draw it
Draw what Sorting into one bucket just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Section
Part 3
Concept
= assigns a value to a name. == asks whether two values are equal. They are completely different jobs.
In a condition you always want ==. Mixing them up is the single most common beginner bug.
Anomaly
Predict first
A student writes this, and it looks reasonable:
Using a single = inside an if condition.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: = means assign, which is not allowed inside an if test.
= means assign, which is not allowed inside an if test. Python stops before running - and even suggests the fix.
Why: = means assign, which is not allowed inside an if test. Python stops before running - and even suggests the fix.
Trap
Using a single = inside an if condition.
x = 5
if x = 5:
print("yes")A single = cannot be a condition
Why: = means assign, which is not allowed inside an if test. Python stops before running - and even suggests the fix.
| you write | result |
|---|---|
| if x = 5: | SyntaxError: invalid syntax. Maybe you meant '==' ... instead of '='? |
Use == to compare.
x = 5
if x == 5:
print("yes")== asks 'are these equal?'
Why: x == 5 is True, so the block runs. Real output: yes.
| expression | value |
|---|---|
| x == 5 | True |
Concept
== works on strings too: answer == "yes" is True only if the text matches exactly.
It is case-sensitive: "Yes" == "yes" is False. (Session 11 shows how .lower() smooths this over.)
Worked example
print("yes" == "yes")
print("Yes" == "yes")Identical text is equal
Why: "yes" == "yes" is True.
Different capitalization is not
Why: Verified by execution: "Yes" == "yes" is False - the Y differs.
| expression | value |
|---|---|
| "yes" == "yes" | True |
| "Yes" == "yes" | False |
Sorting
Sort into buckets
These are the pieces of Session 4 - Conditionals (if / elif / else), out of order. Put each one back under the part of the lesson it belongs to.
Fill the middle
Fill in the blanks
From Deciding on an answer — one line has had its right-hand side removed. Put it back.
answer = input("Continue? ")
if answer == "yes":
print("Great, moving on.")
else:
print("Okay, stopping.")
Why: answer is what everything below it consumes, so the wrong expression here fails later and somewhere else. answer is the str "yes", which matches, so the if runs.
Worked example
The user types yes:
answer = input("Continue? ")
if answer == "yes":
print("Great, moving on.")
else:
print("Okay, stopping.")Compare the text answer with "yes"
Why: answer is the str "yes", which matches, so the if runs.
Read the output
Why: Verified by execution: prints Great, moving on. Typing Yes or Y would fall to else, since the match is exact.
| user types | answer == "yes" | prints |
|---|---|---|
| yes | True | Great, moving on. |
| Yes | False | Okay, stopping. |
Concept
Input arrives as text, and comparing text to a number is a mismatch. Convert first: age = int(input(...)), then compare age >= 18.
Worked example
The user types 20:
age = int(input("Age? "))
if age >= 18:
print("Welcome in.")
else:
print("Too young.")int() makes age a number so >= works
Why: age is 20; 20 >= 18 is true.
Read the output
Why: Verified by execution: prints Welcome in.
| age | age >= 18 | prints |
|---|---|---|
| 20 | True | Welcome in. |
| 15 | False | Too young. |
Concept
An if block can contain another if. The inner one is only reached when the outer condition is true.
Indent the inner block one more level so Python knows it lives inside the outer one.
Fill the middle
Fill in the blanks
From Two conditions in sequence — one line has had its right-hand side removed. Put it back.
age = 20
ticket = "yes"
if age >= 18:
if ticket == "yes":
print("Enter")
else:
print("Need a ticket")
else:
print("Too young")
Why: ticket is what everything below it consumes, so the wrong expression here fails later and somewhere else. Verified by execution: prints Enter.
Worked example
age = 20
ticket = "yes"
if age >= 18:
if ticket == "yes":
print("Enter")
else:
print("Need a ticket")
else:
print("Too young")Outer check: age >= 18 is true
Why: So we go inside and check the ticket.
Inner check: ticket == "yes" is true
Why: Verified by execution: prints Enter. Change ticket to "no" and it prints Need a ticket instead.
| age | ticket | prints |
|---|---|---|
| 20 | yes | Enter |
| 20 | no | Need a ticket |
| 15 | yes | Too young |
Pattern
Step through it
Step through Two conditions in sequence one row at a time. What is driving the change, and what would the row after the last one be?
Section
Part 4
Concept
At the exact cutoff, >= includes the value and > excludes it. A pass mark of exactly 60 needs >= 60 to count as a pass.
Off-by-one at the boundary is a classic bug. Always ask: should the exact value be in or out?
Worked example
score = 60
if score >= 60:
print("Pass")
else:
print("Fail")60 >= 60 is true
Why: The equals part of >= includes the boundary value.
Read the output
Why: Verified by execution: 60 prints Pass; 59 would print Fail. With > 60 instead, even 60 would fail.
| score | score >= 60 | prints |
|---|---|---|
| 60 | True | Pass |
| 59 | False | Fail |
Anomaly
Predict first
A student writes this, and it looks reasonable:
Sorting a temperature but using separate ifs. temp is 5.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: 5 < 10 and 5 < 20 are both true, and these are independent statements - so you get two labels.
Chain with elif so only the first match runs.
Why: 5 < 10 and 5 < 20 are both true, and these are independent statements - so you get two labels.
Trap
Sorting a temperature but using separate ifs. temp is 5.
temp = 5
if temp < 10:
print("cold")
if temp < 20:
print("mild")Each if is checked on its own, so both run
Why: 5 < 10 and 5 < 20 are both true, and these are independent statements - so you get two labels.
| statement | true? | prints |
|---|---|---|
| if temp < 10 | True | cold |
| if temp < 20 | True | mild |
Chain with elif so only the first match runs.
temp = 5
if temp < 10:
print("cold")
elif temp < 20:
print("mild")elif ties the checks together
Why: 5 < 10 is true, prints cold, and the elif is skipped. Real output: just cold.
| branch | runs? |
|---|---|
| if temp < 10 | yes -> cold |
| elif temp < 20 | skipped |
Comparison
Comparison matrix
From Trap: separate ifs all fire: refill the runs? column from what you know. The rest of the table is as it appeared.
| branch | runs? |
|---|---|
| if temp < 10 | yes -> cold |
| elif temp < 20 | skipped |
Anomaly
Predict first
A student writes this, and it looks reasonable:
The if needs an indented block
It is wrong. Say what breaks — and say it before you turn the page.
Correct: Python reaches the next line expecting indentation and finds none, so it stops before running.
Indent the block (4 spaces is standard).
Why: Python reaches the next line expecting indentation and finds none, so it stops before running.
Trap
No indentation under the if.
if True:
print("hi")The if needs an indented block
Why: Python reaches the next line expecting indentation and finds none, so it stops before running.
| you write | result |
|---|---|
| print not indented | IndentationError: expected an indented block after 'if' statement on line 1 |
Indent the block (4 spaces is standard).
if True:
print("hi")The indentation marks the block
Why: Now the print clearly belongs to the if. Real output: hi. Keep indentation consistent throughout a block.
| you write | shows |
|---|---|
| indented print | hi |
Section
Part 5
Concept
Three outcomes need if / elif / else: test the first case, the second, then fall through to the last.
A classic is the sign of a number: positive, negative, or zero.
Worked example
n = -3
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")n > 0? No; n < 0? Yes
Why: -3 is not above 0, but it is below 0, so the elif wins.
else catches exactly zero
Why: Verified by execution: -3 prints negative; 0 would reach else and print zero.
| n | prints |
|---|---|
| 7 | positive |
| -3 | negative |
| 0 | zero |
Pattern
Step through it
Step through Positive, negative, or zero one row at a time. What is driving the change, and what would the row after the last one be?
Concept
To sort a value into ranges, list the cutoffs in order. Each elif covers 'not the ones above, but below this next line'.
You only test one edge per branch, because the earlier branches already ruled out everything below them.
Pattern
Predict first
The table runs: 0.5 | 3 · 3 | 6
In Shipping cost by weight, given the rows so far: what is the next one — the row where weight is 9?
Correct: 9 | 10
| weight | cost |
|---|---|
| 0.5 | 3 |
| 3 | 6 |
| 9 | 10 |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. Verified by execution: 3 is under 5, so it prints 6 and stops.
Worked example
weight = 3
if weight < 1:
print(3)
elif weight < 5:
print(6)
else:
print(10)weight < 1? No - skip
Why: 3 is not under 1.
weight < 5? Yes - cost is 6
Why: Verified by execution: 3 is under 5, so it prints 6 and stops. The 5-and-up case is handled by else.
| weight | cost |
|---|---|
| 0.5 | 3 |
| 3 | 6 |
| 9 | 10 |
Pattern
Step through it
Step through Shipping cost by weight one row at a time. What is driving the change, and what would the row after the last one be?
Intuition
Picture the conditions as steps. The value walks down and stops on the first step it fits, never checking the ones below.
That is why you only need one comparison per step: standing on a step already means you were not on the ones above.
Concept
You do not have to write an else. Sometimes you only want to act in one case and do nothing otherwise.
Without an else, a false condition simply skips the block and the program continues.
Worked example
battery = 80
if battery < 20:
print("Low battery!")
print("Done.")battery < 20 is false, so the warning is skipped
Why: 80 is not under 20, so nothing inside the if runs.
The line after the if still runs
Why: Verified by execution: prints only Done. With battery = 10, it would print Low battery! then Done.
| battery | prints |
|---|---|
| 80 | Done. |
| 10 | Low battery! then Done. |
Concept
Conditions are not only for printing. Use them to decide a value - like whether a discount applies.
Fill the middle
Fill in the blanks
From Discount over $100 — one line has had its right-hand side removed. Put it back.
total = 120
if total >= 100:
total = **total * 0.9**
print(total)
Why: total is what everything below it consumes, so the wrong expression here fails later and somewhere else. 120 qualifies, so total becomes 120 * 0.9.
Worked example
total = 120
if total >= 100:
total = total * 0.9
print(total)total >= 100 is true, so apply 10% off
Why: 120 qualifies, so total becomes 120 * 0.9.
Read the output
Why: Verified by execution: prints 108.0. An 80 total would skip the discount and print 80.
| total in | qualifies? | total out |
|---|---|---|
| 120 | yes | 108.0 |
| 80 | no | 80 |
Concept
if name: is true when name is 'truthy' - a non-empty string, a non-zero number. It is false for an empty string or 0.
It reads as 'if there is a name'. A handy shortcut for 'did the user actually type something?'
Explain it
Discussion prompt
Explain A value on its own is a condition to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
if name: is true when name is 'truthy' - a non-empty string, a non-zero number. It is false for an empty string or 0.
Estimation
Predict first
The user just presses Enter (types nothing):
Commit before you compute: what does Did they enter a name? come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Read the output
Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. Verified by execution: with nothing typed, prints No name given; typing Sam prints Hi Sam.
Worked example
The user just presses Enter (types nothing):
name = input("Name: ")
if name:
print("Hi", name)
else:
print("No name given")An empty string is falsy
Why: Typing nothing makes name the empty string "", so if name: is false.
Read the output
Why: Verified by execution: with nothing typed, prints No name given; typing Sam prints Hi Sam.
| user types | if name: | prints |
|---|---|---|
| Sam | True | Hi Sam |
| (nothing) | False | No name given |
Error analysis
Annotate
Walk the callouts on Did they enter a name?. Each one is a place this is easy to get subtly wrong.
Anomaly
Predict first
A student writes this, and it looks reasonable:
Comparing raw input (text) to a number. The user types 5.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: "5" == 5 is False - a string never equals a number, even one that looks identical.
Convert the input first, then compare numbers to numbers.
Why: "5" == 5 is False - a string never equals a number, even one that looks identical. So it always says Nope.
Trap
Comparing raw input (text) to a number. The user types 5.
guess = input("Guess: ")
if guess == 5:
print("Correct!")
else:
print("Nope")guess is the TEXT "5", not the number 5
Why: "5" == 5 is False - a string never equals a number, even one that looks identical. So it always says Nope.
| expression | value |
|---|---|
| "5" == 5 | False |
Convert the input first, then compare numbers to numbers.
guess = int(input("Guess: "))
if guess == 5:
print("Correct!")
else:
print("Nope")int() makes guess the number 5
Why: Now 5 == 5 is True. Real output with 5 typed: Correct! Compare like with like.
| expression | value |
|---|---|
| int("5") == 5 | True |
Two truths and a lie
Sort into buckets
Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.
if line, and the indentation marks which lines are inside.; Picture the program walking down a path and reaching a fork. The condition is the sign: true goes one way, false the other.; Every line indented under the if is part of the block, and they all run together when the condition is true.Concept
Keep each condition simple and readable. If you need two tests at once (age AND ticket), that is what and / or are for - the very next session.
Analogy
Discussion prompt
Explain One comparison per condition by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
Keep each condition simple and readable. If you need two tests at once (age AND ticket), that is what and / or are for - the very next session.
Worked example
The secret is 7; the user types 7:
secret = 7
guess = int(input("Guess 1-10: "))
if guess == secret:
print("You got it!")
else:
print("Not this time")Convert, then compare to the secret
Why: guess is 7, which equals secret.
Read the output
Why: Verified by execution: prints You got it! Next session's loops turn this into a full guessing game.
| guess | guess == secret | prints |
|---|---|---|
| 7 | True | You got it! |
| 4 | False | Not this time |
Blank canvas
Draw it
Draw what A tiny guessing check just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Concept
A good habit: read each condition as an English sentence. if score >= 60: becomes 'if the score is at least 60'.
If it does not read like a clear yes/no question, it is probably the wrong condition - or two conditions that need and / or.
Counterexample
Discussion prompt
A good habit: read each condition as an English sentence. if score >= 60: becomes 'if the score is at least 60'.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Answer:
If it does not read like a clear yes/no question, it is probably the wrong condition - or two conditions that need and / or.
Section
Part 6
Pattern
1. Start with if and a condition; end the line with a colon
Why: if score >= 60: - the colon says the block starts here.
2. Indent the lines that belong to that branch
Why: The indentation is how Python groups the block. Keep it consistent.
3. Add else for the fallback, elif for more cases
Why: One chain runs exactly one branch; separate ifs each decide alone.
4. Order elif from strictest to loosest
Why: First true wins, so put >= 90 before >= 80, or the loose one grabs the case first.
Pattern
Compare with ==, not =
Why: == asks equal; = assigns. Using = in an if is a SyntaxError.
Convert input before numeric comparisons
Why: int(input(...)) >= 18. Comparing text to a number does not do what you want.
Mind the boundary and case
Why: >= includes the cutoff, > excludes it; "Yes" != "yes". Decide what should count.
Real world
Discussion prompt
Outside this lesson: where does Session 4 - Conditionals (if / elif / else) actually turn up? Name one concrete situation — a job, a piece of software someone ships, a decision somebody has to make — and say which part of Writing correct conditions is doing the work in it.
Hint: Vague is the failure mode here. "Engineering" is not a situation; "deciding whether this build is fast enough to ship" is.
Answer:
Session 4 of the Python Fundamentals series, in depth. Making the program choose with if / elif / else: the colon and indentation that mark a block; comparison operators; else and elif chains checked in order with the first true branch winning; == vs = ; comparing strings and converted input; nested ifs; boundary conditions; and the classic traps (separate ifs that all fire, using = instead of ==, indentation errors).
Check
Walk the chain for score 85.
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")| condition | true? |
|---|---|
| score >= 90 | ? |
| score >= 80 | ? |
Check your understanding
What does this print?
Answer: A
Why: 85 >= 90 is false (skip A); 85 >= 80 is true, so it prints B and the chain stops. Verified by execution.
Trade off
Comparison matrix
From Check: which grade?: every row here is a choice with a cost. Fill the true? column, then say which row you would actually pick and what you give up for it.
| condition | true? |
|---|---|
| score >= 90 | ? |
| score >= 80 | ? |
Check
n is 3. These are two separate ifs.
n = 3
if n > 0:
print("positive")
if n < 5:
print("small")| statement | true? |
|---|---|
| if n > 0 | ? |
| if n < 5 | ? |
Check your understanding
How many lines does this print?
Answer: A
Why: Two independent ifs: 3 > 0 is true (positive) and 3 < 5 is true (small), so both blocks run. Verified by execution.
Comparison
Comparison matrix
From Check: separate ifs: refill the true? column from what you know. The rest of the table is as it appeared.
| statement | true? |
|---|---|
| if n > 0 | ? |
| if n < 5 | ? |
Check
Spot the bug.
x = 5
if x = 5:
print("match")| line | result |
|---|---|
| 2 | ? |
Check your understanding
What happens?
Answer: B
Why: A single = means assign and is not allowed as an if condition, so Python raises a SyntaxError before running. You need ==. Verified by execution.
Check
score is exactly 70.
score = 70
if score > 70:
print("above")
else:
print("not above")| score > 70 | value |
|---|---|
| 70 > 70 | ? |
Check your understanding
What does this print?
Answer: A
Why: 70 > 70 is false because > excludes the exact value. So the else runs, printing not above. Using >= would make it print above. Verified by execution.
Check
The user types Yes (capital Y).
a = input("Go? ")
if a == "yes":
print("going")
else:
print("stopping")| a | a == "yes" |
|---|---|
| "Yes" | ? |
Check your understanding
What does this print?
Answer: A
Why: String comparison is case-sensitive, so "Yes" == "yes" is False. The else runs, printing stopping. Lowercasing the input first would fix it. Verified by execution.
Check
age is 16, ticket is yes.
age = 16
ticket = "yes"
if age >= 18:
if ticket == "yes":
print("Enter")
else:
print("Need a ticket")
else:
print("Too young")| age >= 18 | value |
|---|---|
| 16 >= 18 | ? |
Check your understanding
What does this print?
Answer: A
Why: The outer condition 16 >= 18 is false, so Python jumps straight to the outer else and prints Too young - the inner ticket check is never reached. Verified by execution.
Connect it up
Draw it
One page, no notation unless you need it: draw how these connect — The if Statement · else and elif · Comparing Values · Edges & Pitfalls · More Decisions in Practice · Patterns & Checks. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.
Recap
if condition: runs its indented block only when true; else handles otherwise; elif adds cases and the first true branch wins.
| You want | Use |
|---|---|
| one thing, only when true | if |
| a fallback for false | if / else |
| several categories, pick one | if / elif / ... / else |
| to compare equality | == (never =) |
| a numeric input comparison | int(input(...)) first |
Mind the colon, the indentation, the elif order, and the boundary. Next session we make conditions smarter by combining them with and, or, and not.
Want this taught 1-on-1? Alexander tutors Python Fundamentals — $55/session, free consultation.