Python Decisions Slides
COMPUTING FOUNDATIONS
GOOGLE COLAB
Making Decisions in Python
Comparisons, Booleans & Conditionals
Learning Target
I can use comparisons, Boolean values, and if, elif, and else statements to make a Python program respond differently based on conditions.
Warm-Up Recall
Quick Vocabulary Refresh
30-Second Check
condition
An expression that evaluates to true or false.
Boolean
A data type with only two values: True or False.
if
Runs its block only when the condition is True.
elif
Short for "else if"—checks another condition next.
else
The fallback block when no earlier conditions are true.
Key Fact
Keywords are case-sensitive in Python!
Turn & Talk
What does a conditional allow a computer to do?
Concept 01
Python Can Ask Questions
Evaluation
question.py
Cell 1
score = 85
print(score >= 70)
Predict
What do you predict Python will print?
Key Rule
Comparisons produce a Boolean value:
True or False
Ready? Prepare your Google Colab tab. We will test this live! →
Reference Guide
Comparison Operators
Python Syntax
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
= assigns a value (score = 85)
== compares two values (score == 85)
Active Code-Along
Boolean Practice in Colab
Type Together
Google Colab • Cell 1
age = 15
print(age >= 16)
print(age == 15)
print(age != 18)
1
Predict Each Line
Predict before running: True or False?
2
Run & Confirm
Press Shift + Enter to run.
3
Modify & Re-run
Change age = 18 or age = 12 and re-run.
Every comparison produces an exact Boolean value: True or False.
Concept 02
From a Question to a Decision
The `if` Statement
score = 85
if score >= 70 :
indent → print("You passed!")
1. The Condition
The question Python tests. Evaluates to True or False.
2. The Colon (:)
Signals the start of the instruction block.
3. Indented Action
Runs only if the condition above is True.
If the condition is True, Python runs the indented code.
Critical Python Rule
Indentation Matters
Structure
Correct Indentation
Runs cleanly
if score >= 70:
print("You passed!")
Indent groups instructions inside the if.
Poor / Missing Indent
IndentationError
if score >= 70:
print("You passed!")
Python expects an indented block after :
The Golden Rule of Indentation
Indented code belongs to the condition above it.
Concept 03
Adding `else`
Two-Way Decision
if_else.py Colab Cell 2
score = 65
if score >= 70:
print("You passed!")
else:
print("Keep working.")
else Meaning
If the previous condition was not true, do this instead.
else: never takes a condition. It catches all other cases.
Colab Challenge
Change score to test both outcomes:
Try 92 → "You passed!" | Try 50 → "Keep working."
Only ONE branch will ever execute: either the if or the else.
Concept 04
More Than Two Choices — `elif`
Multi-Way Decision
temperature.py Colab Cell 3
temperature = 72
if temperature >= 80:
print("It's hot.")
elif temperature >= 60:
print("It's comfortable.")
else:
print("It's chilly.")
What does `elif` mean?
"Otherwise, check this condition."
Top-to-Bottom Evaluation
Python checks conditions from top to bottom and stops when it finds the first True condition.
Remaining elif and else blocks are skipped!
1. Check `if` → 2. If false, check `elif` → 3. If all false, run `else`
Active Code-Along
Test the Logic: Temperature
Live Testing
Test Case 1 Hot
temperature = 85
Condition 1: 85 >= 80
Evaluates → True!
Test Case 2 Comfortable
temperature = 72
Condition 1 is False.
Condition 2: 72 >= 60
Evaluates → True!
Test Case 3 Chilly
temperature = 45
Conditions 1 & 2 False.
Falls to else:
Runs fallback block
Reflection Questions
• Which condition became True?
• Which message appeared?
• Why did Python skip the other choices?
Real Application
Connect Back to Survey Data
Prior Project
survey_analysis.py Automated
yes_percent = 62.5
if yes_percent > 50:
print("Most students answered YES.")
elif yes_percent == 50:
print("The class was evenly divided.")
else:
print("Most students answered NO.")
In Our Previous Class
Your program calculated the raw percentage mathematically: 62.5%.
Now with Conditionals
Python interprets results automatically!
Computers make decisions based on what calculated numbers actually mean.
Notice the equality check on line 4: yes_percent == 50
Brain Check
Predict Before Running
Trace the Code
challenge.py Trace carefully!
battery = 20
if battery > 50:
print("Battery Good")
elif battery > 20:
print("Low Battery")
else:
print("Plug In Now!")
Your Mission
What will Python print to the screen when this runs?
Pay close attention to whether the comparison uses > or >=!
Do Not Call It Out
Type the code into Colab to check your hypothesis!
Hint: Is 20 strictly greater than 20? Test it in Google Colab now!
Build Time • 10 Minutes
Decision Maker Challenge
Independent Build
Step 1: Choose ONE Topic
A Grade Checker
B Temperature Message
C Age Eligibility Checker
D Game Score Result
E Survey Result Interpreter
Step 2: Program Requirements
At least one variable
At least one comparison operator
Use if
Use elif
Use else
Produce at least 3 possible outputs
Test using multiple values
Write your code from scratch in a fresh Colab cell. Test every branch!
Self-Audit
Debugging Check
Before You Finish
Did I use == when comparing values? (Not =)
Does every if, elif, and else line end with a colon (:)?
Is the code underneath each condition indented?
Did I test values that trigger every possible outcome?
Does the program produce the result I expected in every test?
All 5 checked? Give a thumbs-up to your teacher or elbow partner!
Looking Ahead
Next Block: Rock, Paper, Scissors
Preview
Program Pipeline for Next Class
Step 1
Get player choice
Step 2
Generate computer choice
Step 3
Compare choices
Step 4
Decide win/loss/tie
Step 5
Display result
Thinking Ahead Question
If player and computer choices are stored in variables:
player and computer
What comparison could Python use to determine whether the game is a tie?
Keep your answer ready for the exit ticket! Block class preview →
Lesson Closure
Exit Ticket
5-Minute Check
1
What is a Boolean?
2
What is the difference between = and ==?
3
When would you use elif?
4
Why does indentation matter in an if statement?
5
What comparison might be useful for detecting a tie in Rock, Paper, Scissors?
Submit your answers before leaving class. Great work coding today! • See you next block!