Lesson 7: Conditional Statements
Give your programs the power to make decisions using if, elif, and else statements.
Lesson 7: Conditional Statements
Until now, our programs have been like a straight train track—they go from start to finish without any choice. Conditional Statements allow us to add "forks in the road." They let our code make decisions based on whether a condition is True or False.
Lesson Overview
In this lesson, we will cover:
- The
ifStatement: The basic decision-maker. - The
elseStatement: The fallback plan. - The
elifStatement: Handling multiple choices. - Indentation: Python’s most important rule.
- Nested Conditionals: Decisions inside decisions.
1. The Basic if Statement
The if statement checks a condition. If it’s true, the code inside runs.
age = 19
if age >= 18:
print("You are an adult.")
Crucial Note: Notice the colon : at the end of the if line and the Indent (empty space) before the print line. This indentation is how Python knows which code belongs to the if statement.
2. Adding a Fallback with else
What if the condition is false? We use else.
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
3. Multiple Choices with elif
If you have more than two options, use elif (short for "else if").
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
How it works: Python checks from top to bottom. As soon as it finds a true condition, it runs that block and skips the rest.
4. Logical Operators in Conditionals
You can combine multiple criteria using and and or.
temperature = 25
is_sunny = True
if temperature > 20 and is_sunny:
print("Perfect day for a walk!")
5. Decisions Inside Decisions (Nested if)
Sometimes, one decision depends on another.
choice = "pizza"
topping = "pineapple"
if choice == "pizza":
print("Italian food selected.")
if topping == "pineapple":
print("Wait... are you sure about that?")
else:
print("Classic choice!")
Practice Exercise: The Adventure Game
Create a file named adventure.py. Write a mini "Choose Your Own Adventure" game:
- Print a starting scenario (e.g., "You are in a dark room. There are two doors, left and right.")
- Ask the user for their choice using
input(). - If they choose
"left", print a outcome. - If they choose
"right", print a different outcome. - If they type anything else, print:
"Invalid choice, you trip and fall!"
Quick Knowledge Check
- What symbol must follow an
ifstatement? - Why is indentation important in Python?
- When does the code inside an
elseblock run? - What does
elifstand for?
Key Takeaways
ifstatements allow for branching logic.elseprovides a default action when no conditions are met.elifhandles multiple specific scenarios.- Correct indentation is required for code blocks to work.
What’s Next?
We can now make programs that choose between paths! But what if we want to do something over and over again? In Lesson 8, we’ll learn about Loops—the key to automation!