🧠 Mental Model: Programs as Decision Trees
The Fork in the Road
Every conditional statement is like a fork in the road. Your program examines a condition (asks a yes/no question), then chooses which path to take based on the answer.
- Condition - The question being asked (True or False?)
- If path - What to do when the answer is True
- Else path - What to do when the answer is False
Decision Tree Example
Take umbrella ☂️
Wear sunglasses 🕶️
✅❌ Boolean Logic: The Language of Decisions
Boolean logic is the foundation of all computer decision-making. Every condition ultimately evaluates to either True or False - there's no "maybe" in computer logic!
Interactive Boolean Explorer
Try different comparisons and see their boolean results:
Result:
⚠️ Critical Pitfall: Python's "Truthiness"
Dangerous Assumption: Everything is True or False
Beginner trap: Python considers some non-boolean values as "falsy" and others as "truthy" when used in conditions. This can lead to unexpected behavior!
❌ "Falsy" Values
False
- obviously false0
- zero is false""
- empty string is false[]
- empty list is falseNone
- nothing is false
✅ "Truthy" Values
True
- obviously true1, 5, -3
- any non-zero number"hello"
- any non-empty string[1, 2]
- any non-empty list
🧠 Safe Practice
As a beginner, always be explicit! Use comparison operators like == 0 instead of relying on truthiness. It makes your intent crystal clear.
🔀 if-elif-else: The Decision Making Structure
The Complete Decision Framework
- if - "If this condition is true, do this"
- elif - "Otherwise, if this other condition is true, do this"
- else - "If none of the above were true, do this as a fallback"
Interactive Grade Calculator
Let's build a grade calculator that demonstrates if-elif-else chains:
score = 85 # Try changing this!
if score >= 90:
grade = "A"
print("Excellent work!")
elif score >= 80:
grade = "B"
print("Good job!")
elif score >= 70:
grade = "C"
print("You passed!")
else:
grade = "F"
print("Need to study more!")
print(f"Your grade is: {grade}")
🔗 Logical Operators: Combining Conditions
Sometimes you need to combine multiple conditions. Python provides three logical operators that work like their English counterparts:
and
Both conditions must be True
age >= 18 and has_license
Can drive only if BOTH are true
or
At least one condition must be True
is_weekend or is_holiday
Don't work on weekends OR holidays
not
Flips True to False, False to True
not is_raining
Go outside if it's NOT raining
Logical Operator Playground
Try different combinations and see how logical operators work:
🎯 Mastery Check: Boolean Logic & Conditionals
Question 1: Boolean Evaluation
What does this expression evaluate to: 5 > 3 and 2 < 4
?
True (both conditions are true)
False
Error
Question 2: Conditional Flow
What gets printed when score = 75
?
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")
C
B
All of them (A, B, C)
🎯 Ready for Repetition!
What You've Mastered
- ✅ Boolean logic (True/False) as the foundation of decisions
- ✅ if-elif-else chains for complex decision making
- ✅ Comparison operators (==, !=, <, >, <=, >=)
- ✅ Logical operators (and, or, not)
- ✅ Python's truthiness concept and its pitfalls
Now that your programs can make decisions, let's learn how to make them repeat actions efficiently with loops!
Continue to Repeating Actions →