Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔁 Unit 2 – Flow of Control और Functions  ·  Lesson 15

if, elif, else (Selection)

Selection क्या है?

अब तक हमारे programs हर line ऊपर से नीचे चलाते थे (sequence)। असली programs को चुनना पड़ता है: "Pass" या "Fail" दिखाना, ₹1000 से ऊपर ही discount देना, password match होने पर ही login। Condition के आधार पर रास्ता चुनना selection या decision making कहलाता है, और Python इसके लिए if, elif और else keywords use करता है।

StartInput nn % 2 == 0 ?YesNoPrint "odd"Print "even"Stop
एक if-else decision

Indentation

Python indentation (line की शुरुआत में spaces) से बताता है कि कौन से statements एक block के हैं। if वाली line colon : पर ख़त्म होती है, और अंदर की lines बराबर indent होती हैं (आमतौर पर 4 spaces)। दूसरी languages इसके लिए { } use करती हैं।

Python
if 10 > 5:
print('yes')
IndentationError: expected an indented block after 'if' statement on line 1

Simple if

Syntax
if condition:
    statement(s)      # सिर्फ़ condition True होने पर
Python
age = int(input('Enter your age: '))
if age >= 18:
    print('You can vote.')
print('Thank you!')            # not indented: always runs
Enter your age: 19 You can vote. Thank you!

if-else

दोनों में से ठीक एक block चलता है।

Python
n = int(input('Enter a number: '))
if n % 2 == 0:
    print(n, 'is even')
else:
    print(n, 'is odd')
Enter a number: 37 37 is odd

if-elif-else ladder

कई choices हों तो elif ("else if") use करें। Python ऊपर से नीचे conditions check करता है और सिर्फ़ पहला True condition वाला block चलाता है।

Python
marks = float(input('Enter percentage: '))
if marks >= 90:
    grade = 'A'
elif marks >= 75:
    grade = 'B'
elif marks >= 60:
    grade = 'C'
elif marks >= 33:
    grade = 'D'
else:
    grade = 'E (needs improvement)'
print('Grade:', grade)
Enter percentage: 78.5 Grade: B
marks >= 90 ?False → अगली checkmarks >= 75 ?True → grade = B, बाकी छोड़ोmarks >= 60 ?check नहीं हुईmarks >= 33 ?check नहीं हुई
78.5 के लिए Python दूसरी condition पर रुक जाता है
⚠️ क्रम ज़रूरी है

marks >= 33 पहले लिख दें तो हर pass student को grade D मिलेगा। सबसे सख़्त condition पहले रखें।

Nested if

एक if के अंदर दूसरा if nested if है। Example: तीन numbers में सबसे बड़ा।

Python
a, b, c = 12, 45, 29
if a >= b:
    if a >= c:
        print('Largest is', a)
    else:
        print('Largest is', c)
else:
    if b >= c:
        print('Largest is', b)
    else:
        print('Largest is', c)
Largest is 45

यही and से आसान लिख सकते हैं: if a >= b and a >= c: … या सीधे max(a, b, c)।

Solved programs

1. Leap year

Python
year = int(input('Enter year: '))
if year % 400 == 0:
    print(year, 'is a leap year')
elif year % 100 == 0:
    print(year, 'is not a leap year')
elif year % 4 == 0:
    print(year, 'is a leap year')
else:
    print(year, 'is not a leap year')
Enter year: 1900 1900 is not a leap year

2. Slabs वाला बिजली bill

पहले 100 units ₹3/unit, अगले 100 units ₹5/unit, 200 से ऊपर ₹7/unit।

Python
units = int(input('Units consumed: '))
if units <= 100:
    bill = units * 3
elif units <= 200:
    bill = 100 * 3 + (units - 100) * 5
else:
    bill = 100 * 3 + 100 * 5 + (units - 200) * 7
print('Electricity bill = Rs.', bill)
Units consumed: 260 Electricity bill = Rs. 1220
260 units का bill = 300 + 500 + 4200200400600₹0–100101–200201–260300500420
Slab bill कैसे जुड़ता है

3. Menu-driven calculator

Python
print('1. Add  2. Subtract  3. Multiply  4. Divide')
choice = int(input('Your choice: '))
x = float(input('First number: '))
y = float(input('Second number: '))
if choice == 1:
    print('Result =', x + y)
elif choice == 2:
    print('Result =', x - y)
elif choice == 3:
    print('Result =', x * y)
elif choice == 4:
    if y == 0:
        print('Cannot divide by zero')
    else:
        print('Result =', x / y)
else:
    print('Invalid choice')
1. Add 2. Subtract 3. Multiply 4. Divide Your choice: 4 First number: 22 Second number: 8 Result = 2.75

एक line वाला if (conditional expression)

Python
temp = 31
status = 'Hot' if temp > 30 else 'Pleasant'    # one-line if-else
print(status)
Hot

सारांश और practice

  • if condition True होने पर block चलाता है; else False होने पर।
  • elif कई conditions check करता है; सिर्फ़ पहला true block चलता है।
  • आख़िर में colon : और consistent indentation ज़रूरी है।
  • Nested if = if के अंदर if।
✅ Practice

Programs लिखें: (1) character vowel है या नहीं, (2) abs() के बिना number की absolute value, (3) bill ₹2000 से ऊपर हो तो 10% discount, नहीं तो 5%।

Hint: (1) if ch.lower() in 'aeiou': (2) if n < 0: n = -n

अक्सर पूछे जाने वाले प्रश्न (FAQ)

if-elif और कई अलग if statements में क्या फ़र्क है?
if-elif ladder में Python पहली true condition पर रुककर बाकी छोड़ देता है। अलग-अलग if में हर condition check होती है, इसलिए एक से ज़्यादा block चल सकते हैं।
Indentation के लिए कितने spaces use करें?
चार spaces standard है। कोई भी consistent संख्या चलेगी, पर एक block की हर line का indentation बिल्कुल same होना चाहिए।
क्या if के बाद else ज़रूरी है?
नहीं। else optional है। तभी use करें जब condition false होने पर कुछ करना हो।
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

अगला topicfor और while Loops, range() आपकी प्रगति: 15 / 29 (52%) · Lesson 16 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें