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!
if
Statementif 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 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.
age = int(input("Enter your age: "))
if age > 18:
print("You are allowed!")
else:
print("Not allowed.")
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