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)

Python
  • 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)

Python

Output (when number = 15):

text

Change number = -5 → only the last print runs.

Example 2: With input (real interactive)

Python

Example 3: Temperature advice (Hyderabad summer special)

Python

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)

Python

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:

Python

Beginner trap warning — very common mistake:

Wrong:

Python

Better:

Python

Or use:

Python

6. Super Common Beginner Mistakes (I see these every week)

  1. Forgetting the colon :

    Python
  2. Wrong indentation

    Python
  3. Using = instead of ==

    Python
  4. Forgetting to convert input to number

    Python

    Fix: age = int(input(“Age? “))

  5. Thinking or works like English

    Python

    Correct:

    Python

7. Your 10-Minute Mini Challenge (do it now!)

Open replit.com or any online Python editor and write a program that:

  1. Asks user: current temperature and whether they have AC at home (yes/no)
  2. 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 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *