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 करता है।
Indentation
Python indentation (line की शुरुआत में spaces) से बताता है कि कौन से statements एक block के हैं। if वाली line colon : पर ख़त्म होती है, और अंदर की lines बराबर indent होती हैं (आमतौर पर 4 spaces)। दूसरी languages इसके लिए { } use करती हैं।
if 10 > 5:
print('yes')Simple if
if condition:
statement(s) # सिर्फ़ condition True होने परage = int(input('Enter your age: '))
if age >= 18:
print('You can vote.')
print('Thank you!') # not indented: always runsif-else
दोनों में से ठीक एक block चलता है।
n = int(input('Enter a number: '))
if n % 2 == 0:
print(n, 'is even')
else:
print(n, 'is odd')if-elif-else ladder
कई choices हों तो elif ("else if") use करें। Python ऊपर से नीचे conditions check करता है और सिर्फ़ पहला True condition वाला block चलाता है।
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)marks >= 33 पहले लिख दें तो हर pass student को grade D मिलेगा। सबसे सख़्त condition पहले रखें।
Nested if
एक if के अंदर दूसरा if nested if है। Example: तीन numbers में सबसे बड़ा।
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)यही and से आसान लिख सकते हैं: if a >= b and a >= c: … या सीधे max(a, b, c)।
Solved programs
1. Leap year
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')2. Slabs वाला बिजली bill
पहले 100 units ₹3/unit, अगले 100 units ₹5/unit, 200 से ऊपर ₹7/unit।
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)3. Menu-driven calculator
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')एक line वाला if (conditional expression)
temp = 31 status = 'Hot' if temp > 30 else 'Pleasant' # one-line if-else print(status)
सारांश और practice
ifcondition True होने पर block चलाता है;elseFalse होने पर।elifकई conditions check करता है; सिर्फ़ पहला true block चलता है।- आख़िर में colon
:और consistent indentation ज़रूरी है। - Nested if = if के अंदर if।
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
