Conditional Statements

Last Updated: 14th Aug 2025


Conditional statements help you run certain parts of code only when a specific condition is true.
They work like decision-making in real life.

Hinglish Tip 🗣: "Yeh programming ka 'yadi' system hai — agar condition sahi hai toh kuch karo, warna kuch aur."


🛠 Types of Conditional Statements

  1. if statement → Runs code if condition is True
  2. if-else statement → Runs one block if True, another if False
  3. elif (else-if) → Multiple conditions check
  4. Nested ifif inside another if
  5. match-case → Cleaner alternative for multiple condition checks (Python 3.10+)

1️. if Statement

Syntax:

if condition:
    # # code, if condition is true

Example:

age = 20
if age >= 18:
    print("You are an adult.")

2️. if-else Statement

Syntax:

if condition:
    # code, if condition is true
else:
    # code, if condition is false

Example:

marks = 28
if marks >= 33:
    print("Pass")
else:
    print("Fail")

Example:

# Single Line if-else
marks = 28
res="Pass" if marks >= 33 else "Fail"
print(res)

3️. elif (else-if)

Syntax:

if condition1:
    # code, if condition1 is true
elif condition2:
    # code,if condition2 is true
else:
    # code, if both conditions are false

Example:

temp = 25
if temp > 30:
    print("Hot")
elif temp > 20:
    print("Warm")
else:
    print("Cold")

4️. Nested if

Syntax:

if condition1:
    if condition2:
        # code, if both conditions are true

Example:

age = 25
citizen = True

if age >= 18:
    if citizen:
        print("Eligible to vote.")
    else:
        print("Not eligible to vote.")
else:
    print("Not eligible to vote.")

5️. match-case (3.10+)

Syntax:

match expression:
    case pattern1:
        # code, if pattern1 matches
    case pattern2:
        # code, if pattern2 matches
    case _:
        # code, if no patterns match

Example:

day = "Monday"

match day:
    case "Monday":
        print("Start of the week")
    case "Friday":
        print("End of the week")
    case _:
        print("Midweek")

💡 Quick Practice

  1. Exercises