Learn Without Walls
← Back to Module 4

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

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

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: 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) → andor (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

  1. Combine with and: Replace nested ifs with if A and B:
  2. Guard clauses: Check failure cases first with early returns/messages
  3. 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?

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!

Complete the Practice Problems | Take the Quiz