Programming · Grade 7 · Chapter 6
Python Conditionals
Teaching code how to make decisions using `if`, `elif`, and `else` statements.
Chapter Goals
What We Will Master Today
- Comparison Operators: Master `==`, `!=`, `<`, `>`, `<=`, and `>=`.
- Conditional Blocks: Structure `if`, `elif`, and `else` decision branches.
- Indentation Rules: Understand why Python uses 4-space indentation.
- Nested Logic: Combine nested conditions with logical `and`/`or` operators.
The Decision Tree
How Video Games Choose What Happens Next
"If player touches lava → lose health. If player collects 100 coins → gain extra life. Else → keep playing."
Concept: Conditionals allow code to take different paths depending on real-time data!
Core Concept 1
The 6 Comparison Operators
== Equal to
(5 == 5 ➔ True)
!= Not Equal to
(5 != 3 ➔ True)
> Greater than
(10 > 2 ➔ True)
< Less than
(3 < 1 ➔ False)
>= Greater or Equal
(5 >= 5 ➔ True)
<= Less or Equal
(4 <= 10 ➔ True)
Difference Alert
Assignment (=) vs Comparison (==)
Single Equals (=)
score = 100
Action: Stores value 100 inside variable score.
Double Equals (==)
if score == 100:
Question: Checks if score is equal to 100 (Returns True/False).
Core Concept 2
The if Statement Syntax
age = 14
if age >= 13:
print("Access Granted: Teen Account")
- Must start with keyword
if.
- Condition line MUST end with a colon
:.
- The code block below MUST be indented (4 spaces or 1 Tab).
Core Concept 3
Adding the Catch-All else Branch
user_pass = "secret"
if user_pass == "secret123":
print("Login successful!")
else:
print("Incorrect password. Try again.")
Visual Flow
Flowchart of if / else Execution
Core Concept 4
Multi-Branching with elif
When you have 3 or more potential outcomes, use elif (short for Else-If).
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or below")
Execution Rule
How Python Evaluates elif Chains
Python checks conditions top to bottom. As soon as ONE condition evaluates to True, Python runs that block and SKIPS the entire rest of the chain!
Spot the Mistake 1
Can You Spot the Syntax Error?
# Broken line:
if temperature > 30
print("It's a hot day!")
SyntaxError: expected ':'
Fix: Every if, elif, and else header MUST end with a colon :!
Spot the Mistake 2
IndentationError Trap!
# Broken line:
if score == 100:
print("Perfect Score!")
IndentationError: expected an indented block after 'if' statement
Fix: Press Tab or 4 spaces on lines inside conditional blocks!
Smartboard Voting
What Will This Code Print for x = 15?
if x > 20:
print("Alpha")
elif x > 10:
print("Beta")
elif x > 5:
print("Gamma")
else:
print("Delta")
Core Concept 5
Nested Conditionals (if inside if)
has_ticket = True
age = 14
if has_ticket == True:
if age >= 12:
print("Welcome to the PG-13 Movie!")
else:
print("Parent supervision required.")
Code Refactoring
Cleaning Up Nested Code with and
Nested Structure
if has_ticket:
if age >= 12:
print("Enter")
Clean Single Line
if has_ticket and age >= 12:
print("Enter")
Common Confusion
Multiple if Statements vs elif Chain
Independent ifs
Checks EVERY condition independently. Multiple blocks can execute!
if / elif / else
Checks conditions sequentially until ONE is True, then stops.
Quick Check · Quiz 1
Which operator tests whether two values are NOT equal in Python?
Click to reveal answer
Quick Check · Quiz 2
What happens if no condition in an if/elif chain is True, but there is an else block?
AThe code crashes with an Error
BPython skips everything
CThe else block executes automatically
DThe first if block runs anyway
Click to reveal answer
Quick Check · Quiz 3
What character MUST be placed at the end of an if condition line?
ASemicolon ;
BColon :
CPeriod .
DComma ,
Click to reveal answer
Quick Check · Quiz 4
What is the output of 10 >= 10?
Click to reveal answer
Guided Practice
Theme Park Roller Coaster Checker
height = 135 # cm
age = 12
if height >= 140:
print("Eligible for Mega Coaster!")
elif height >= 120 and age >= 10:
print("Eligible for Family Coaster.")
else:
print("Eligible for Kiddie Ride only.")
Coding Challenge
Build a Text-Based Adventure Game Choice
choice = input("Do you open door 'left' or 'right'? ")
if choice == "left":
print("You found a treasure chest! 🪙")
elif choice == "right":
print("A sleeping dragon awakes! 🐉")
else:
print("Invalid direction. You fell into a trap!")
Common Trap
Misconception: if age >= 12 or 13:
Remember: Comparison operators require explicit values on both sides of or!
Summary & Takeaways
What We Learned in Chapter 6
- Conditionals control program execution flow using Boolean test expressions.
- Use
== to check equality, never single = (assignment).
- Headers end with
: and code blocks require 4-space indentation.
- Chain multiple conditions with
elif and catch remaining cases with else.
Cheat Sheet
Conditional Syntax Summary
if condition_1:
# Run if condition 1 is True
elif condition_2:
# Run if condition 1 is False and condition 2 is True
else:
# Run if all above conditions are False
Exit Ticket
Predict the Output Challenge
Evaluate for score = 75:
if score > 80: print("Gold")
elif score >= 75: print("Silver")
else: print("Bronze")
Next Chapter: Python Loops (for & while)!