Python Mastery – From Basics to Brilliance / Control Flow & Decision Making

if, elif, else Statements

Conditional statements let your program make decisions. It checks if a certain condition is true — and then runs some code based on that. If the condition is not true, it can check another condition, or just run a default block of code.

It’s like saying:

“If it's raining, take an umbrella. Else if it's cloudy, take a jacket. Else, wear whatever you want.”


🔹 Python Syntax Breakdown

if condition:
    # Block A
elif another_condition:
    # Block B
else:
    # Block C

🔸 if block:

  • The program checks this first condition.

  • If it's True, it runs Block A, and skips the rest.

if temperature > 30:
    print("It's hot!")

🔸 elif block:

  • Only checked if the first if was False.

  • You can have multiple elif blocks.

  • If its condition is True, it runs Block B and skips the rest.

elif temperature > 20:
    print("Nice weather!")

🔸 else block:

  • Runs if none of the above conditions were True.

  • Acts as a “default”.

else:
    print("It's cold!")

🔁 Example Combined:

temperature = 25

if temperature > 30:
    print("It's hot!")              # Not printed
elif temperature > 20:
    print("Nice weather!")          # ✅ This is printed
else:
    print("It's cold!")