Chapter 6: If Statements
If Statements (also called conditional statements or just if conditions).
This is like giving your program a brain — the ability to make decisions instead of blindly following the same steps every time.
Imagine we’re in our Hyderabad classroom again — filter coffee on the table, notebook open, and I’m explaining this like I’m teaching my cousin who’s just starting college.
1. What is an If Statement? (The super simple explanation)
An if statement asks a yes/no question (a condition) and runs some code only if the answer is yes (True).
- If the condition is True → do this special thing
- If the condition is False → skip that thing (or do something else if we have else)
Real-life Hyderabad analogy everyone gets:
You’re deciding whether to go to Charminar for biryani tonight:
- If (temperature < 30°C and pocket money > 500) → “Let’s go eat Paradise biryani! 🔥”
- Else → “Stay home, make Maggi or watch IPL”
The if is that decision point. Without it, you’d always go or always stay — no smart choice.
In programming → if statements let your code react differently based on user input, calculations, time, scores, etc.
2. Basic Syntax in Python (very important — copy this pattern)
|
0 1 2 3 4 5 6 7 8 |
if condition: # ← colon : is MUST # indented code here # ← 4 spaces or 1 tab (Python forces indentation!) # this runs ONLY if condition is True |
- No colon : → error
- Wrong indentation → error (Python is super strict about this)
- Condition must be something that becomes True or False (boolean expression)
3. First Baby Examples – Try These Yourself
Example 1: Simple check (positive number)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
number = 15 if number > 0: print("Wow! This number is positive! 👍") print("This line always runs, no matter what.") |
Output (when number = 15):
|
0 1 2 3 4 5 6 7 |
Wow! This number is positive! 👍 This line always runs, no matter what. |
Change number = -5 → only the last print runs.
Example 2: With input (real interactive)
|
0 1 2 3 4 5 6 7 8 9 10 |
age = int(input("How old are you? ")) if age >= 18: print("You can vote in elections! 🗳️") print("You can also watch A-rated movies 😎") |
Example 3: Temperature advice (Hyderabad summer special)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
temp = int(input("What's the temperature outside right now? ")) if temp >= 40: print("AC full blast mode ON! 🥵 Don't go out unless emergency") elif temp >= 32: print("Hot summer day — fan + cold drink recommended") elif temp >= 25: print("Pleasant Hyderabad weather 🌤️ Enjoy!") else: print("Chilled weather! Wear sweater 🧣") |
4. Full Family of If Statements
| Type | When to use | Syntax example |
|---|---|---|
| if alone | Only do something if condition True | if x > 10: … |
| if + else | Do one thing if True, another if False | if … else: … |
| if + elif + else | Check multiple possibilities in order | if … elif … elif … else: … |
elif = else if (short form)
Important rule: Python checks from top to bottom — first True condition wins, others skipped.
Example – Grading system (very common beginner project)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
marks = int(input("Enter your marks out of 100: ")) if marks >= 90: print("Grade: A+ 🎉 Genius level!") elif marks >= 80: print("Grade: A – Very good!") elif marks >= 70: print("Grade: B – Keep it up!") elif marks >= 60: print("Grade: C – Can improve") else: print("Grade: Fail 💪 Study harder next time!") |
5. Conditions – What can go inside the if?
Anything that gives True or False:
- Comparisons: >, <, >=, <=, == (equal), != (not equal)
- Logical operators: and, or, not
- Variables (if bool), functions that return bool, etc.
Common examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
if age >= 18 and has_voter_id: # both must be True print("Eligible to vote") if mood == "happy" or money > 1000: print("Let's party!") if not is_raining: print("No umbrella needed") |
Beginner trap warning — very common mistake:
Wrong:
|
0 1 2 3 4 5 6 |
if age > 18 or age < 60: # WRONG logic |
Better:
|
0 1 2 3 4 5 6 |
if age > 18 and age < 60: # between 19 and 59 |
Or use:
|
0 1 2 3 4 5 6 |
if 18 < age < 60: # Python allows this beautiful chaining! |
6. Super Common Beginner Mistakes (I see these every week)
-
Forgetting the colon :
Python0123456if age > 18 # SyntaxError – missing : -
Wrong indentation
Python01234567if age > 18:print("Adult") # IndentationError -
Using = instead of ==
Python0123456if age = 18: # SyntaxError – you're assigning, not comparing -
Forgetting to convert input to number
Python01234567age = input("Age? ") # age is string "20"if age > 18: # TypeError – can't compare string > intFix: age = int(input(“Age? “))
-
Thinking or works like English
Python0123456if marks == 90 or 95 or 100: # WRONG – this is always True!Correct:
Python0123456if marks in [90, 95, 100]:
7. Your 10-Minute Mini Challenge (do it now!)
Open replit.com or any online Python editor and write a program that:
- Asks user: current temperature and whether they have AC at home (yes/no)
- Then gives advice:
- If temp >= 38 and has AC → “Stay inside with AC full! 🧊
- If temp >= 38 and no AC → “Go to mall or theatre yaar!”
- If temp < 38 → “Enjoy the day outside 🌞”
- Bonus: add elif for very low temp
Paste your code here when done — I’ll review it, fix bugs, or make it cooler 😄
Any part confusing?
- “Explain and/or/not again”
- “Show if inside loop example”
- “Why indentation matters so much?”
- “Give 3 more real Hyderabad examples”
Just say — we’re building this step by step.
Next (when ready): Loops — repeating things without copy-paste!
You’re doing awesome — if statements are where programming starts feeling alive 🚀
