Python If Else क्या है? if, elif, else Statement in Hindi | Conditional Statements | RBSE Class 12
Python Conditional Statement क्या है?
Programming में कई बार हमें किसी condition के आधार पर अलग-अलग काम करना होता है। ऐसी स्थिति में Python में Conditional Statements का उपयोग किया जाता है।
उदाहरण के लिए:
- यदि student के marks 40 या उससे अधिक हैं तो Pass दिखाना।
- यदि number 2 से divide होता है तो Even बताना।
- यदि age 18 या उससे अधिक है तो eligible बताना।
इन सभी situations में condition check करने के लिए if, elif और else का उपयोग किया जाता है।
Python में Conditional Statements के प्रकार
| Statement | उपयोग |
|---|---|
| if | एक condition check करने के लिए |
| if-else | दो possible situations के लिए |
| if-elif-else | Multiple conditions के लिए |
| Nested if | एक if के अंदर दूसरा if |
1. Python if Statement
if statement का उपयोग किसी condition को check करने के लिए किया जाता है।
Syntax
if condition:
statement
यदि condition True होती है तो indented block के अंदर लिखा code execute होता है।
Example
age = 20
if age >= 18:
print("You are eligible")
Output:
You are eligible
Python में Indentation क्या है?
Python में code block को define करने के लिए indentation का उपयोग किया जाता है।
age = 20
if age >= 18:
print("Adult")
यहाँ print() statement if block के अंदर है क्योंकि उसमें
indentation है।
2. Python if-else Statement
जब condition True होने पर एक काम और False होने पर दूसरा काम करना हो, तब if-else का उपयोग किया जाता है।
Syntax
if condition:
statement1
else:
statement2
Example
age = 16
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Output:
Not Eligible
Even और Odd Number का Program
Number even है या odd, यह check करने के लिए modulus operator % का उपयोग किया जा सकता है।
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Example Output
Enter a number: 10
Even Number
Positive और Negative Number
num = int(input("Enter a number: "))
if num >= 0:
print("Positive Number")
else:
print("Negative Number")
num >= 0 है। यदि zero को अलग category में रखना हो
तो तीन conditions की आवश्यकता होगी।
Positive, Negative और Zero
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
3. Python if-elif-else Statement
जब दो से अधिक conditions को check करना हो तब if-elif-else statement का उपयोग किया जाता है।
Syntax
if condition1:
statement1
elif condition2:
statement2
elif condition3:
statement3
else:
statement4
Example
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Need Improvement")
Output:
Grade A
Student Grade Program
marks = float(input("Enter your marks: "))
if marks >= 90:
grade = "A+"
elif marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 40:
grade = "D"
else:
grade = "F"
print("Grade:", grade)
4. Nested if Statement
जब एक if statement के अंदर दूसरा if statement लिखा जाता है, उसे Nested if कहा जाता है।
Example
age = 20
has_id = True
if age >= 18:
if has_id:
print("Allowed")
else:
print("ID required")
else:
print("Not eligible")
Comparison Operators के साथ if
Conditional statements में comparison operators का बहुत अधिक उपयोग होता है।
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
a = 10
b = 20
if a < b:
print("a is smaller than b")
Logical Operators के साथ Conditions
Multiple conditions को combine करने के लिए and, or, not का उपयोग किया जा सकता है।
and Example
age = 25
if age >= 18 and age <= 60:
print("Age is between 18 and 60")
or Example
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
not Example
is_raining = False
if not is_raining:
print("You can go outside")
Python में Multiple Conditions
marks = 85
attendance = 80
if marks >= 40 and attendance >= 75:
print("Student is eligible")
else:
print("Student is not eligible")
Largest of Two Numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Largest =", a)
elif b > a:
print("Largest =", b)
else:
print("Both numbers are equal")
Largest of Three Numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest =", a)
elif b >= a and b >= c:
print("Largest =", b)
else:
print("Largest =", c)
Pass और Fail Program
marks = float(input("Enter marks: "))
if marks >= 40:
print("Pass")
else:
print("Fail")
Voting Age Check Program
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible according to the given age condition")
else:
print("Not eligible according to the given age condition")
Divisible by 5 का Program
num = int(input("Enter a number: "))
if num % 5 == 0:
print("Number is divisible by 5")
else:
print("Number is not divisible by 5")
Number 5 और 10 दोनों से Divisible है या नहीं?
num = int(input("Enter a number: "))
if num % 5 == 0 and num % 10 == 0:
print("Divisible by both 5 and 10")
else:
print("Condition not satisfied")
if और if-else में अंतर
| if | if-else |
|---|---|
| एक condition check करता है। | True और False दोनों cases handle करता है। |
| Condition False होने पर कोई else block नहीं होता। | Condition False होने पर else block execute हो सकता है। |
if-elif-else कैसे काम करता है?
Python में conditions को ऊपर से नीचे की ओर check किया जाता है। जैसे ही कोई condition True होती है, उसका corresponding block execute होता है और बाकी elif conditions को उस chain में आगे check नहीं किया जाता।
marks = 85
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
elif marks >= 70:
print("B")
else:
print("C")
यहाँ पहली condition False है, दूसरी condition True है, इसलिए A print होगा।
Common Mistakes in Python if-else
1. Colon भूल जाना
गलत:
if age >= 18
print("Adult")
सही:
if age >= 18:
print("Adult")
2. Indentation की समस्या
if या else के अंदर लिखे statements को सही indentation के साथ लिखना जरूरी है।
3. = और == को confuse करना
Condition में equality check करने के लिए सामान्यतः == का
उपयोग किया जाता है।
age = 18
if age == 18:
print("Age is 18")
Conditional Statements का Quick Revision
| Statement | मुख्य उपयोग |
|---|---|
| if | एक condition check करना |
| if-else | दो possible cases |
| if-elif-else | Multiple conditions |
| Nested if | if के अंदर if |
| and | Multiple conditions में सभी conditions True |
| or | कम से कम एक condition True |
| not | Boolean result को reverse करना |
RBSE Class 12 Important Questions
- Conditional Statement क्या है?
- Python में if statement क्या है?
- if-else statement का syntax लिखिए।
- if और if-else में अंतर लिखिए।
- if-elif-else statement क्या है?
- Nested if क्या है?
- Python में indentation क्यों महत्वपूर्ण है?
- if statement का एक उदाहरण लिखिए।
- दो numbers में से largest number निकालने का program लिखिए।
- Number even या odd check करने का program लिखिए।
- Number positive, negative या zero है यह check करने का program लिखिए।
- Student के marks के आधार पर grade निकालने का Python program लिखिए।
- Comparison operators का conditional statements में क्या उपयोग है?
- Logical operators का conditional statements में क्या उपयोग है?
Frequently Asked Questions (FAQs)
Python में if statement क्या है?
if statement का उपयोग किसी condition को check करने के लिए किया जाता है। यदि condition True होती है तो संबंधित code block execute होता है।
Python में else का क्या उपयोग है?
जब if की condition False होती है, तब else block का उपयोग alternative code execute करने के लिए किया जाता है।
Python में elif क्या है?
elif का उपयोग multiple conditions को क्रम से check करने के लिए किया जाता है।
Nested if क्या है?
एक if statement के अंदर दूसरे if statement का उपयोग करना Nested if कहलाता है।
Python में indentation क्यों जरूरी है?
Python indentation का उपयोग code blocks को identify करने के लिए करता है, इसलिए conditional statements में सही indentation महत्वपूर्ण है।
अब अगला Topic पढ़ें
Python conditional statements और control flow की विस्तृत जानकारी के लिए Python की official documentation देखें।