✅ CHAPTER 6 NOTES: Conditionals in Python


🧠 What are Conditionals?

Conditionals allow you to run certain blocks of code only when a specific condition is true.

Think of it as making decisions in code — like a "choose your own adventure" book!


🧱 Basic Structure of if Statement

if condition:
    # Code block runs only if condition is True
else:
    # Code block runs if condition is False

📌 Python uses indentation (spacing) to decide which code belongs where — no {} like in C/C++/Java.


🧱 if-else Ladder

if condition1:
    # Executes if condition1 is True
elif condition2:
    # Executes if condition2 is True (only if condition1 is False)
elif condition3:
    # Executes if condition3 is True (if others are False)
else:
    # Executes if NONE of the above conditions are True

Only one block is executed in an if-elif-else ladder, whichever matches first.


💡 Example 1: Basic Age Checker

age = int(input("Enter your age: "))

if age > 18:
    print("You are allowed!")
else:
    print("Not allowed.")

💡 Example 2: Complex If-Elif-Else Ladder

if age > 18 and age < 110:
    print("You are above 18.")
elif age < 0:
    print("Aliens/time travelers not allowed.")
elif age >= 110:
    print("Ghosts not allowed.")
elif age == 0:
    print("Babies not born yet!")
else:
    print("Invalid entry.")

🔎 Logic Order Matters