Python Conditional Statements
Python में if, elif और else का उपयोग करके Decision Making करना सीखें।
इस lesson में हम सीखेंगे कि Python में Conditional Statements क्या होते हैं और उनका उपयोग किसी condition के आधार पर decision लेने के लिए कैसे किया जाता है।
आज हम सीखेंगे:
if → elif → else
🐍 Conditional Statement क्या है?
Programming में कई बार हमें किसी condition के आधार पर अलग-अलग काम करना होता है।
उदाहरण के लिए:
- अगर Student के Marks 40 या उससे अधिक हैं → Pass
- अगर Marks 40 से कम हैं → Fail
ऐसी स्थिति में हम Python के Conditional Statements का उपयोग करते हैं।
Condition के आधार पर Program को Decision लेने की सुविधा देने के लिए Conditional Statements का उपयोग किया जाता है।
1️⃣ Python में if Statement
if statement का उपयोग तब किया जाता है जब हमें किसी condition के True होने पर कोई काम करना हो।
Syntax:
if condition:
statement
Python में condition के बाद colon (:) लगाना जरूरी है।
इसके बाद आने वाले code को indentation के साथ लिखना होता है।
Example:
age = 20
if age >= 18:
print("You are eligible")
Output:
You are eligible
🔢 if के साथ Comparison Operator
Conditional Statements में Comparison Operators का बहुत अधिक उपयोग किया जाता है।
marks = 75
if marks >= 40:
print("Pass")
Output:
Pass
2️⃣ if...else Statement
जब हमें दो possibilities में से किसी एक को चुनना हो, तब if...else का उपयोग किया जाता है।
Syntax:
if condition:
statement1
else:
statement2
Example:
marks = 35
if marks >= 40:
print("Pass")
else:
print("Fail")
Output:
Fail
🎯 User से Marks लेकर Pass/Fail Check करें
marks = int(input("Enter your marks: "))
if marks >= 40:
print("You are Pass")
else:
print("You are Fail")
Example Output:
Enter your marks: 75
You are Pass
3️⃣ elif Statement
जब हमें दो से अधिक conditions को check करना हो, तब elif का उपयोग किया जाता है।
Syntax:
if condition1:
statement
elif condition2:
statement
else:
statement
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
अब एक practical program बनाते हैं जिसमें Student के Marks के आधार पर Grade निकाली जाएगी।
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Example Output:
Enter your marks: 82
Grade A
🔢 Even और Odd Number Check करना
Modulus Operator % और if statement का उपयोग करके हम पता कर सकते हैं कि Number Even है या Odd।
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Example:
Enter a number: 10
Even Number
➕ Positive, Negative और Zero Check करना
number = int(input("Enter a number: "))
if number > 0:
print("Positive Number")
elif number < 0:
print("Negative Number")
else:
print("Zero")
4️⃣ Nested if Statement
जब एक if statement के अंदर दूसरा if statement लिखा जाता है, उसे Nested if कहा जाता है।
Example:
age = int(input("Enter your age: "))
if age >= 18:
if age >= 60:
print("Senior Citizen")
else:
print("Adult")
else:
print("Minor")
🧠 Practice Questions
Question 1:
Python में condition check करने के लिए कौन-सा statement सबसे basic है?
Answer: if
Question 2:
दो possibilities में से एक को select करने के लिए कौन-सा statement उपयोग होता है?
Answer: if...else
Question 3:
Multiple conditions check करने के लिए किसका उपयोग किया जाता है?
Answer: elif
Question 4:
किस Operator से पता लगाया जा सकता है कि Number Even है?
Answer: %
❓ Frequently Asked Questions
Python में if statement क्या है?
if statement का उपयोग condition के True होने पर किसी code को execute करने के लिए किया जाता है।
Python में else का उपयोग क्यों किया जाता है?
else का उपयोग तब किया जाता है जब if की condition False हो।
Python में elif क्या है?
elif का उपयोग multiple conditions को एक के बाद एक check करने के लिए किया जाता है।
Python में Conditional Statements का उपयोग क्यों होता है?
Conditional Statements का उपयोग program में conditions के आधार पर decision लेने के लिए किया जाता है।
Python में if के बाद colon क्यों लगाया जाता है?
Python में if, elif और else के बाद colon (:) लगाकर उस condition के अंदर आने वाले code block को दर्शाया जाता है।
📖 आज हमने क्या सीखा?
- Conditional Statements क्या होते हैं?
- if statement
- if...else statement
- elif statement
- Nested if
- Even और Odd Number check करना
- Pass और Fail check करना
- Student Grade Program बनाना
🐍 RBSE Python — Python Programming सीखें Step by Step