Important Notice:

The if...else Statement

The if...else Statement

14 views 2 min read

The if...else Statement :-

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

यदि condition True (सही) होती है, तो if block के अंदर लिखा code execute होता है।
यदि condition False (गलत) होती है, तो else block के अंदर लिखा code execute होता है।

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

English

The if...else statement is a conditional statement in Python. It is used to check a condition.

If the condition is True, the statements inside the if block are executed.

If the condition is False, the statements inside the else block are executed.

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

Syntax:-
 
if condition:
    statement
else:
    statement
 
Example:
 
age = 15
if age >= 18:
    print("You are an adult")
else:
    print("You are not an adult")
 
Output:
You are not an adult
 
Because:
15 >= 18 → False

इसलिए if block को skip करके else block execute होगा।

Another Example:-
 
number = 10
if number > 0:
    print("Positive Number")
else:
    print("Negative Number")
 
Output:
Positive Number
 
Because:
10 > 0 → True

इसलिए if block execute होगा और else block execute नहीं होगा।
 
Shorthand if...else Statement :-

Python में if...else statement को एक ही line में भी लिखा जा सकता है। इसे Shorthand if...else Statement, One-Line if...else Statement या Conditional Expression कहा जाता है।

English

In Python, an if...else statement can also be written in a single line. This is called a Shorthand if...else Statement, One-Line if...else Statement, or Conditional Expression.

Syntax:
 
value_if_true if condition else value_if_false
 
Example:
age = 20
print("Adult") if age >= 18 else print("Not Adult")
 
Output:
Adult
 
Because:
20 >= 18 → True

इसलिए "Adult" print होगा।

 

Related Notes