Conditionals
A program that always does the same thing is not very useful. Conditionals let a program branch โ doing different things depending on the data.
A condition splits execution down one of two paths.
1. Learning Objective
By the end of this session you will be able to:
- Write an if statement and explain that its body only runs when the condition is True
- Add elif and else to handle multiple, mutually exclusive branches
- Combine conditions with and, or, and not
- Explain the difference between = (assignment) and == (comparison), a very common typo
- Trace, by hand, exactly which branch of an if/elif/else will run for a given value
2. Pre-Coding Quiz
Answer these before reading the concept explanation or writing any code. It is fine to get these wrong โ that is the point. You need 4/5 to proceed to the lab.
Given population = 54 (population in millions) and the following code, what happens?
if population > 50:
print("Large")
population > 50 evaluates to True (54 is indeed greater than 50), so the indented block underneath the if runs.Given the following, with population = 54, what prints?
if population > 100:
print("Huge")
elif population > 50:
print("Large")
else:
print("Small")
Given region = "Africa" and population = 54, which correctly checks "is this an African country with over 50 million people"?
and requires BOTH conditions to be True. or (option a) would match if EITHER were true, which is not what "African country AND over 50 million" means. Option c uses a single = (assignment), which is invalid inside a condition.What is wrong with if population = 54:?
= assigns a value; it does not compare. Python actually catches this specific mistake as a SyntaxError rather than silently doing the wrong thing โ but it is still a very common typo worth recognising immediately.Given is_independent = True, which correctly checks "is NOT independent"?
not flips a boolean: not True is False, and not False is True. if not is_independent: is the idiomatic way to check "this is False" rather than writing if is_independent == False:.3. The Concept โ Branching with if / elif / else
The indented block only executes when the condition is True โ otherwise Python skips straight past it.
if, elif, and else are keywords โ reserved words built into Python itself; you can never use them as a variable name. and, or, and not are technically keywords too, but they have a second, more specific name: they are logical operators (also called boolean operators) โ words that combine or invert True/False values, playing the same functional role that + or > play for numbers. So: every one of these words is a keyword, but only and/or/not also count as operators.
if โ running code only when a condition is True
An if statement's indented block only runs when its condition evaluates to True. If the condition is False, Python skips straight past it. (Population figures below are in millions, to keep the numbers easy to read.)
population = 54
if population > 50:
print("Large country")
print("This line always runs, regardless of the condition")
elif and else โ multiple, mutually exclusive branches
Python checks each condition top to bottom and runs only the FIRST branch whose condition is True โ every remaining branch, including else, is skipped once one has matched.
population = 54
if population > 100:
print("Huge country")
elif population > 50:
print("Large country")
elif population > 10:
print("Medium country")
else:
print("Small country")
# Prints "Large country" โ the first True condition wins, nothing else runs
Combining conditions with and, or, not
Real conditions often depend on more than one thing. and requires every part to be True; or requires at least one; not flips a boolean.
region = "Africa"
population = 54
if region == "Africa" and population > 50:
print("Large African country")
if region == "Africa" or region == "Asia":
print("Africa or Asia")
is_independent = True
if not is_independent:
print("Not independent")
else:
print("Independent")
The = vs == typo
A single = assigns; a double == compares. Python treats = inside a condition as a SyntaxError rather than silently reassigning the variable โ a deliberate safety net, since this exact typo is extremely common when writing conditionals quickly.
# if population = 54: # SyntaxError โ = cannot be used as a condition
if population == 54: # correct โ == compares
print("Exactly 54 million")
4. Lab
What you will build
A file called conditionals.py.
Step-by-step instructions
Create the file and classify population size with if/elif/else
# conditionals.py
population = 54
if population > 100:
size = "Huge"
elif population > 50:
size = "Large"
elif population > 10:
size = "Medium"
else:
size = "Small"
print(f"Population size category: {size}")
Combine two conditions with and
region = "Africa"
if region == "Africa" and population > 50:
print("This is a large African country")
else:
print("Does not match: large African country")
Combine two conditions with or
if region == "Africa" or region == "Americas":
print("This country is in Africa or the Americas")
Use not to check a boolean is False
is_independent = True
if not is_independent:
print("Not an independent nation")
else:
print("An independent nation")
Trigger the = vs == typo deliberately, read the error, then fix it
Comment the broken line out after reading the SyntaxError.
# if population = 54: # uncomment to see the SyntaxError
if population == 54:
print("Exactly 54 million")
else:
print("Not exactly 54 million")
5. Expected Files Changed
| File | Action | Why |
|---|---|---|
conditionals.py |
Created | Demonstrates if/elif/else, and/or/not, and the = vs == distinction. |
docs/sessions/session-03/index.html |
Created | This session document. |
6. Commit Checkpoint
Once the lab is complete and you can explain every line, make this exact commit:
git add conditionals.py docs/sessions/session-03/index.html
git commit -m "session-03: branch program flow with if/elif/else and combined conditions"
7. Code Review Checklist
Go through your code line by line and check each item:
8. Post-Coding Quiz
Same 5 questions. Take it again now that you have written and run the code. You need 4/5 to mark this session complete.
Given population = 54 (population in millions) and the following code, what happens?
if population > 50:
print("Large")
population > 50 evaluates to True (54 is indeed greater than 50), so the indented block underneath the if runs.Given the following, with population = 54, what prints?
if population > 100:
print("Huge")
elif population > 50:
print("Large")
else:
print("Small")
Given region = "Africa" and population = 54, which correctly checks "is this an African country with over 50 million people"?
and requires BOTH conditions to be True. or (option a) would match if EITHER were true, which is not what "African country AND over 50 million" means. Option c uses a single = (assignment), which is invalid inside a condition.What is wrong with if population = 54:?
= assigns a value; it does not compare. Python actually catches this specific mistake as a SyntaxError rather than silently doing the wrong thing โ but it is still a very common typo worth recognising immediately.Given is_independent = True, which correctly checks "is NOT independent"?
not flips a boolean: not True is False, and not False is True. if not is_independent: is the idiomatic way to check "this is False" rather than writing if is_independent == False:.9. Reflection Questions
Think through these after the post-quiz. No right answer โ they are for discussion.
- In the population-classification chain, what would happen if you reordered the branches so the "Medium" check came before "Large"? Would the result change? Why or why not?
- Why does Python stop checking further elif branches once it finds one that is True, instead of checking all of them?
- Can you think of a real condition that would need and, one that would need or, and one that would need not, from your own experience (not this lab)?
- Why do you think Python makes = inside a condition a hard error instead of just a warning?
10. What Breaks If This Knowledge Is Missing?
- Every data-filtering session ahead: From the very next Layer forward, every "find only the countries that match X" operation is built on the exact if-condition logic from this session, just applied inside a loop or comprehension instead of standalone.
- The assignment/comparison typo: Writing = instead of == is one of the most common typos in any C-like or Python-like language. Python is one of the few languages that turns this into an immediate, loud SyntaxError instead of a silent, hard-to-find bug โ recognising that error message on sight will save you real debugging time.
11. What We Learned
Python concept mastered: Branching program flow with if/elif/else, combining conditions with and/or/not, and the assignment-vs-comparison distinction.
Unlocks: Your programs can now make decisions based on data instead of always doing the same thing โ the foundation for filtering and validating data in every layer ahead.
Next session: Session 04 โ Loops. We repeat an action multiple times instead of writing it out by hand โ for and while loops.