if Statements
Making your program make decisions — the most important concept in all of programming, explained simply with everyday examples.
"An if statement is just your program saying: 'If this is true — do this. Otherwise — do something else.' You already think this way. Python just lets you write it down."
— ShurAIYou Already Think in if-statements
Before you ever touch code, you make hundreds of if-decisions every day. Your brain runs them automatically:
→ take an umbrella
else
→ leave it at home
→ plug in charger
else
→ keep using phone
→ allowed in
else
→ entry denied
Python's if statement lets you write exactly this kind of logic in code.
The Basic Shape of an if Statement
Here is the structure. Three things matter: the keyword if, a condition that is either true or false, and a colon : at the end. The indented code below runs only if the condition is True.
keyword
any True/False expression
required colon!
Example 1 — Is it Hot Outside?
The simplest possible if statement. If the temperature is over 35, print a message. Otherwise, do nothing:
temperature = 38
if temperature > 35:
print("It's too hot! Stay indoors.")
print("Drink lots of water.")
print("Have a nice day!")
It's too hot! Stay indoors.
Drink lots of water.
Have a nice day!
Now change temperature = 28 and run it again. The two indented lines are skipped — but "Have a nice day!" always prints because it is not indented under the if block.
Indentation — The Most Important Rule
In Python, indentation is not just style — it is the rule. The indented lines belong to the if block and only run when the condition is True. Lines that go back to the left edge always run.
Python standard is 4 spaces per indent level. Never mix tabs and spaces — Python will give you an IndentationError. Most code editors handle this automatically when you press Tab.
if...else — One or the Other
Add an else block to handle the case when the condition is False. Exactly one of the two blocks will always run:
age = 16
if age >= 18:
print("You can vote!")
else:
print("You are too young to vote.")
print(f"Wait {18 - age} more year(s).")
You are too young to vote.
Wait 2 more year(s).
More Everyday Examples
password = input("Enter password: ")
if password == "shurai123":
print("✅ Access granted! Welcome.")
else:
print("❌ Wrong password. Try again.")
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is an EVEN number")
else:
print(f"{number} is an ODD number")
total = float(input("Cart total (Rs.): "))
if total >= 1000:
discount = total * 0.10 # 10% off
print(f"🎉 You get Rs.{discount:.0f} off!")
print(f"You pay: Rs.{total - discount:.0f}")
else:
needed = 1000 - total
print(f"Add Rs.{needed:.0f} more to get 10% off!")
if...elif...else — Multiple Choices
elif means "else if" — it adds more conditions to check. Python tests them top to bottom and runs the first one that is True. Think of it like a flowchart:
marks = int(input("Your marks (0-100): "))
if marks >= 90:
print("Grade A+ — Outstanding! 🏆")
elif marks >= 80:
print("Grade A — Excellent! 🌟")
elif marks >= 70:
print("Grade B — Good work! 👍")
elif marks >= 60:
print("Grade C — Satisfactory")
elif marks >= 35:
print("Grade D — Just passed")
else:
print("Grade F — Failed. Keep trying! 💪")
Common Mistakes to Avoid
# ❌ SyntaxError — colon is missing
if age >= 18
print("adult")
# ✅ Correct
if age >= 18:
print("adult")
# ❌ SyntaxError — = is assignment, not comparison
if name = "Riya":
print("hello")
# ✅ Correct — use == to compare
if name == "Riya":
print("hello")
# ❌ IndentationError — no indent inside if
if score >= 50:
print("pass")
# ✅ Correct — 4 spaces inside the if block
if score >= 50:
print("pass")
Real Example — Movie Ticket Pricing
A ticket booking system that uses age and day of week to calculate the price:
print("=== 🎬 Movie Ticket Booking ===")
age = int(input("Your age : "))
day = input("Day (Mon-Sun) : ").lower()
# Base price by age group
if age < 5:
price = 0
category = "Child (free)"
elif age < 12:
price = 80
category = "Child"
elif age < 60:
price = 200
category = "Adult"
else:
price = 120
category = "Senior"
# Tuesday discount — cheapest day
if day == "tuesday":
price = price * 0.5 # 50% off on Tuesdays
print()
print(f"Category : {category}")
print(f"Day : {day.title()}")
print(f"Ticket : Rs.{price:.0f}")
=== 🎬 Movie Ticket Booking ===
Your age : 28
Day (Mon-Sun) : Tuesday
Category : Adult
Day : Tuesday
Ticket : Rs.100
You have three choices: just if (runs or skips), if...else (runs one of two options), and if...elif...else (runs one of many options). You will use all three constantly — knowing which one to reach for is the skill.
"Every useful program has if statements. They are not just a feature — they are the heartbeat of all logic."
— ShurAI🧠 Quiz — Question 1
What happens if you forget the colon : at the end of an if statement?
🧠 Quiz — Question 2
What is the standard number of spaces used for indentation inside an if block?
🧠 Quiz — Question 3
score = 75. Which message does this print?
if score >= 90: print("A")
elif score >= 70: print("B")
elif score >= 60: print("C")
else: print("F")
🧠 Quiz — Question 4
In an if...elif...else chain, how many blocks will actually run?