🟢 Beginner  ·  Lesson 08

if, elif and else Statements

if, elif और else Statements

Decision Making

Programs often need to choose what to do based on a condition. The if statement runs a block only when its condition is True.

if / elif / else Syntax

Python – syntax
if condition1:
    # runs if condition1 is True
elif condition2:
    # runs if condition1 False and condition2 True
else:
    # runs if all above are False
  • Each condition ends with a colon :.
  • The block under it is indented (4 spaces).
  • elif = "else if"; you can have many. else is optional.

Program 1: Pass or Fail

Python – passfail.py
marks = int(input("Enter marks: "))
if marks >= 33:
    print("Result: PASS")
else:
    print("Result: FAIL")
Enter marks: 45 Result: PASS

If marks is 33 or more, the first block runs; otherwise the else block runs.

Program 2: Largest of Three Numbers

Python – largest.py
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))

if a >= b and a >= c:
    print("Largest is", a)
elif b >= c:
    print("Largest is", b)
else:
    print("Largest is", c)
a: 5 b: 9 c: 2 Largest is 9
  • First check if a beats both others.
  • If not, elif b >= c decides between b and c.
  • Otherwise c is largest.

Program 3: Grade Calculator

Python – grade.py
pct = float(input("Enter percentage: "))

if pct >= 90:
    grade = "A+"
elif pct >= 75:
    grade = "A"
elif pct >= 60:
    grade = "B"
elif pct >= 33:
    grade = "C"
else:
    grade = "Fail"

print("Grade:", grade)
Enter percentage: 82 Grade: A

Python checks each elif top to bottom and stops at the first true one — so order matters (highest first).

Program 4: Positive, Negative or Zero

Python – signcheck.py
n = int(input("Enter a number: "))
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")
Enter a number: -4 Negative

Nested if

An if can sit inside another if for two-level decisions.

Python – nested.py
age = int(input("Age: "))
if age >= 18:
    has_id = input("Have ID? (yes/no): ")
    if has_id == "yes":
        print("Allowed to vote")
    else:
        print("Bring your ID")
else:
    print("Too young to vote")

Common Mistakes

  • Using = instead of == in the condition.
  • Forgetting the colon :.
  • Wrong indentation inside the block.
  • Putting elif conditions in the wrong order.

Practice Tasks

  1. Check whether a number is even or odd.
  2. Take a year and check if it is a leap year.
  3. Ask a person's age and print child / teen / adult / senior.
  4. Take three sides and check if they can form a triangle.

Summary

  • if runs a block when its condition is True.
  • elif adds more choices; else is the fallback.
  • Conditions need a colon and an indented block.
  • elif order matters — check from highest/most-specific first.

Decision Making

Programs को अक्सर condition के आधार पर चुनना पड़ता है कि क्या करें। if statement block तभी चलाता है जब उसकी condition True हो।

if / elif / else Syntax

Python – syntax
if condition1:
    # condition1 True होने पर चलता है
elif condition2:
    # condition1 False और condition2 True पर
else:
    # ऊपर सब False होने पर
  • हर condition colon : से खत्म होती है।
  • उसके नीचे का block indent (4 spaces) होता है।
  • elif = "else if"; कई हो सकते हैं। else optional है।

Program 1: Pass या Fail

Python – passfail.py
marks = int(input("Enter marks: "))
if marks >= 33:
    print("Result: PASS")
else:
    print("Result: FAIL")
Enter marks: 45 Result: PASS

अगर marks 33 या ज़्यादा है तो पहला block चलता है; वरना else block।

Program 2: तीन Numbers में सबसे बड़ा

Python – largest.py
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))

if a >= b and a >= c:
    print("Largest is", a)
elif b >= c:
    print("Largest is", b)
else:
    print("Largest is", c)
a: 5 b: 9 c: 2 Largest is 9
  • पहले check करें कि a दोनों से बड़ा है या नहीं।
  • नहीं तो elif b >= c b और c में से तय करता है।
  • वरना c सबसे बड़ा।

Program 3: Grade Calculator

Python – grade.py
pct = float(input("Enter percentage: "))

if pct >= 90:
    grade = "A+"
elif pct >= 75:
    grade = "A"
elif pct >= 60:
    grade = "B"
elif pct >= 33:
    grade = "C"
else:
    grade = "Fail"

print("Grade:", grade)
Enter percentage: 82 Grade: A

Python हर elif को ऊपर से नीचे check करता है और पहले true पर रुक जाता है — इसलिए order ज़रूरी है (सबसे ऊँचा पहले)।

Program 4: Positive, Negative या Zero

Python – signcheck.py
n = int(input("Enter a number: "))
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")
Enter a number: -4 Negative

Nested if

दो-level decision के लिए एक if दूसरे if के अंदर रह सकता है।

Python – nested.py
age = int(input("Age: "))
if age >= 18:
    has_id = input("Have ID? (yes/no): ")
    if has_id == "yes":
        print("Allowed to vote")
    else:
        print("Bring your ID")
else:
    print("Too young to vote")

सामान्य गलतियाँ

  • condition में == की जगह = लगाना।
  • colon : भूलना।
  • block के अंदर गलत indentation।
  • elif conditions गलत order में रखना।

Practice Tasks

  1. Check करें कि number even है या odd।
  2. एक year लेकर leap year check करें।
  3. Age पूछकर child / teen / adult / senior print करें।
  4. तीन sides लेकर check करें कि triangle बन सकता है या नहीं।

सारांश

  • if condition True होने पर block चलाता है।
  • elif और choices जोड़ता है; else fallback है।
  • Conditions को colon और indented block चाहिए।
  • elif order ज़रूरी — सबसे ऊँचा/specific पहले check करें।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp