Learning outcomes
- Write conditional statements
- Understand decision-making in code
Core concepts
Lessons
Module goal
Learner understands how programs make decisions, and can write code that behaves differently depending on the situation.
Lesson 1 of 5 · lecture
Why programs need to make decisions
Step 1 of 2
So far every script runs the exact same way, top to bottom, every time. Real software doesn't. A login screen behaves differently depending on whether the password is correct. A repair-tracking app behaves differently depending on whether a device is in stock. That is control flow: the program checks a condition and, depending on True or False, takes a different path.
Human analogy
“If it's raining, take an umbrella. Otherwise, don't.” Python's if statement works on exactly this logic.
Guided labs
Lab part 1 — age checker
- 1.Create control_flow.py
- 2.Ask for the user's age with input() — note input() always returns a string, so convert with int()
- 3.Use if/elif/else to print teenager (13–19), adult (20–64) or senior (65+)
age = int(input("Enter your age: "))
if age >= 65:
print("You are a senior.")
elif age >= 20:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")Lab part 2 — simple login checker
- 1.Hardcode a correct username and password
- 2.Ask the user to input a username and password
- 3.Use and to check both match before granting access
correct_username = "nextdoortech"
correct_password = "1234"
entered_username = input("Enter username: ")
entered_password = input("Enter password: ")
if entered_username == correct_username and entered_password == correct_password:
print("Access granted.")
else:
print("Access denied.")Success criteria
Both scripts run without errors and branch correctly — test each several times with different values to see the branching in action.
Knowledge Check
Question 1 of 6
What's the key difference between = and ==?
What you read
- How programs branch based on conditions
- Comparison operators (==, !=, >, <)
- Combining conditions with logical operators
What you understand
Key takeaways
- — Control flow lets code react to different inputs
- — Real programs are not purely linear
Hands-on lab
Lab tasks
- 1.Build an age-checker (teen/adult/senior)
- 2.Build a simple login checker (username + password match)
Knowledge check
- Predict which branch executes given a scenario
- Write if-elif-else blocks
Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.