Module 4 Study Guide
Complete Review: Conditionals
Lesson 4.1: Comparison Operators
The Six Operators
Comparison operators compare two values and return a Boolean (True or False).
print(10 == 10) # True - equal to print(10 != 5) # True - not equal to print(5 < 10) # True - less than print(10 > 5) # True - greater than print(5 <= 5) # True - less than or equal to print(5 >= 10) # False - greater than or equal to
Key Points
=assigns a value;==checks equality- String comparison is case-sensitive:
"Hello" != "hello" - Comparing different types:
"5" == 5isFalse - Integers and floats can be compared:
5 == 5.0isTrue - Results can be stored in variables:
is_adult = age >= 18
Lesson 4.2: if, elif, and else
Basic Structure
# Simple if if temperature > 30: print("It's hot!") # if-else: two paths if age >= 18: print("Adult") else: print("Minor") # if-elif-else: multiple paths if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" else: grade = "F"
Key Points
- Always end
if,elif,elsewith a colon: - Indent the body with 4 spaces
- In an elif chain, only the first True branch executes
- Multiple separate
ifstatements each check independently elseis optional and catches everything not matched above
Critical Distinction: Multiple
if statements are independent checks (any/all can run). An if-elif chain is mutually exclusive (only one runs).
Lesson 4.3: Logical Operators
The Three Operators
and— True only when both sides are Trueor— True when at least one side is Truenot— Reverses: True becomes False, False becomes True
# and: both must be true if age >= 18 and has_id: print("Can enter") # or: at least one must be true if day == "Sat" or day == "Sun": print("Weekend!") # not: reverses the boolean if not is_raining: print("Go outside")
Precedence and Short-Circuit
Order: not (first) → and → or (last). Use parentheses for clarity.
Short-circuit: and stops at the first False; or stops at the first True. Useful for guarding against errors (e.g., checking x != 0 before dividing by x).
Lesson 4.4: Nested Conditionals
Nesting Basics
if has_ticket: if age >= 18: print("Welcome!") else: print("Too young.") else: print("Need a ticket.")
Refactoring Strategies
- Combine with
and: Replace nested ifs withif A and B: - Guard clauses: Check failure cases first with early returns/messages
- Limit nesting: Keep to 3 levels or fewer
When to nest vs. flatten: Nest when the inner decision genuinely depends on the outer one and the logic is clearer hierarchically. Flatten when you can combine conditions with
and/or without losing clarity.
Self-Check: Can You Do This?
- Use all six comparison operators correctly
- Explain the difference between
=and== - Write an if-elif-else chain for multiple conditions
- Explain why indentation matters in Python
- Use
and,or,notto combine conditions - Read a truth table for
andandor - Explain short-circuit evaluation with an example
- Write nested conditionals for complex decisions
- Refactor deep nesting into flatter code
- Identify and fix common conditional mistakes
Next Steps
Once you are comfortable with conditionals, you are ready for Module 5: Loops, where you will learn to repeat actions automatically using for and while loops. Conditionals and loops together form the core of programming logic!