Important Notice:

The if...elif...else Statement

The if...elif...else Statement

17 views 3 min read

The if...elif...else Statement :-

if...elif...else statement Python का एक Conditional Statement (शर्तीय कथन) है। इसका उपयोग multiple conditions (एक से अधिक conditions) को check करने के लिए किया जाता है।

सबसे पहले if की condition check होती है। यदि if condition True (सही) होती है, तो उसका code execute होता है और बाकी conditions को check नहीं किया जाता।

यदि if condition False (गलत) होती है, तो Python elif की condition को check करता है। आवश्यकता के अनुसार एक से अधिक elif statements का उपयोग किया जा सकता है।

यदि सभी if और elif conditions False होती हैं, तो else block का code execute होता है।

Python में if, elif और else के अंदर लिखे code को Indentation (रिक्त स्थान) देना आवश्यक होता है। Indentation यह बताता है कि कौन-सा statement किस block के अंदर है। सामान्यतः Python में 4 spaces का indentation प्रयोग किया जाता है।

English

The if...elif...else statement is a conditional statement in Python. It is used to check multiple conditions.

First, the if condition is checked. If the if condition is True, its block is executed and the remaining conditions are not checked.

If the if condition is False, Python checks the elif condition. Multiple elif statements can be used when required.

If all the if and elif conditions are False, the else block is executed.

In Python, the code written inside the if, elif, and else blocks must be given Indentation (spaces). Indentation indicates which statements belong to each block. Generally, 4 spaces are used for indentation in Python.

Syntax:-
 
if condition1:
    statement

elif condition2:
    statement

elif condition3:
    statement

else:
    statement
 
Example:
 
marks = 75
if marks >= 80:
    print("Grade A")
 
elif marks >= 60:
    print("Grade B")

elif marks >= 40:
    print("Grade C")

else:
    print("Fail")
 
Output:
 
Grade B
 
Because:
75 >= 80 → False
75 >= 60 → True

इसलिए elif marks >= 60 का block execute होगा और output Grade B आएगा।

Another Example:-
 
number = -5

if number > 0:
    print("Positive Number")

elif number < 0:
    print("Negative Number")

else:
    print("Zero")
 
Output:
 
Negative Number
 
Because:
-5 > 0 → False
-5 < 0 → True

इसलिए elif block execute होगा।

Important Points :-
  • if statement first condition को check करता है।
  • elif का अर्थ else if होता है।
  • एक if के साथ multiple elif statements हो सकते हैं।
  • else optional होता है।
  • एक condition True होने के बाद बाकी conditions check नहीं की जाती हैं।
  • else तब execute होता है जब सभी if और elif conditions False हों।
  • if, elif और else के अंदर indentation आवश्यक है।
  • सामान्यतः 4 spaces का indentation प्रयोग किया जाता है।
Shorthand if...elif...else Statement :-

Python में if...elif...else को सामान्य if...elif...else की तरह सीधे एक ही line में लिखने का standard shorthand syntax नहीं है। लेकिन Conditional Expression का उपयोग करके simple conditions को short form में लिखा जा सकता है।

English

A normal if...elif...else statement does not have a direct one-line shorthand syntax. However, a Conditional Expression can be used to write simple conditions in a shorter form.

Syntax:
 
value1 if condition1 else value2

For multiple conditions:

value1 if condition1 else value2 if condition2 else value3
 
Example:
 
marks = 75
result = "A" if marks >= 80 else "B" if marks >= 60 else "C" if marks >= 40 else "Fail"
print(result)
 
Output:
B
 
Because:
75 >= 80 → False
75 >= 60 → True

इसलिए B assign होगा।

Related Notes