Programming में कई बार ऐसी परिस्थितियाँ आती हैं जहाँ Program को किसी शर्त (Condition) के आधार पर निर्णय (Decision) लेना पड़ता है। उदाहरण के लिए—
- यदि विद्यार्थी के अंक 33 या उससे अधिक हैं, तो उसे Pass घोषित करना।
- यदि बैंक खाते में पर्याप्त राशि है, तभी पैसा निकालना।
- यदि User का Username और Password सही है, तभी Login की अनुमति देना।
- यदि किसी व्यक्ति की आयु 18 वर्ष या उससे अधिक है, तो उसे मतदान (Voting) की अनुमति देना।
ऐसी परिस्थितियों में Program को अलग-अलग परिस्थितियों के अनुसार अलग-अलग कार्य करना होता है। Python में इस प्रक्रिया को Decision Making या Conditional Statements कहा जाता है।
Python में Decision Making के लिए मुख्य रूप से निम्न Statements का उपयोग किया जाता है—
- if
- if...else
- if...elif...else
- Nested if
- Match-Case (Python 3.10+)
Decision Making वह प्रक्रिया है जिसमें Program किसी Condition को जाँचकर यह तय करता है कि कौन-सा Code Execute करना है।
यदि Condition True होती है, तो एक Block Execute होता है और यदि False होती है, तो दूसरा Block Execute हो सकता है।
Condition एक ऐसा Expression होता है जिसका परिणाम केवल दो में से एक होता है—
- True
- False
उदाहरण—
age = 20
print(age >= 18)
Output
True
Condition
│
┌──────┴──────┐
True False
│ │
Execute Block Skip/Else Block
यदि कोई Condition True होती है, तो if Block के अंदर का Code Execute होता है।
यदि Condition False है, तो Program सीधे अगले Statement पर चला जाता है।
if condition:
statement
age = 20
if age >= 18:
print("You are eligible for voting.")
Output
You are eligible for voting.
marks = 75
if marks >= 33:
print("Pass")
Output
Pass
यदि Condition True होगी—
Condition → True → Execute Block
यदि Condition False होगी—
Condition → False → Block Skip
जब Program को दो अलग-अलग परिस्थितियों में अलग-अलग कार्य करना हो, तब if...else का उपयोग किया जाता है।
if condition:
statement1
else:
statement2
age = 16
if age >= 18:
print("Eligible for Voting")
else:
print("Not Eligible")
Output
Not Eligible
num = int(input("Enter Number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
जब दो से अधिक Conditions की जाँच करनी हो, तब if...elif...else का उपयोग किया जाता है।
if condition1:
statement
elif condition2:
statement
elif condition3:
statement
else:
statement
marks = int(input("Enter Marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 33:
print("Grade C")
else:
print("Fail")
Condition 1
│
True → Execute
│
False
│
Condition 2
│
True → Execute
│
False
│
Condition 3
│
Else
जब एक if के अंदर दूसरा if लिखा जाता है, तो उसे Nested if कहते हैं।
if condition1:
if condition2:
statement
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible for Voting")
Output
Eligible for Voting
username = input("Username: ")
password = input("Password: ")
if username == "admin":
if password == "12345":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Invalid Username")
Python 3.10 में Match-Case Statement जोड़ा गया, जो कई विकल्पों (Multiple Choices) में से किसी एक को चुनने के लिए उपयोग किया जाता है।
यह कुछ हद तक अन्य भाषाओं के Switch Case जैसा कार्य करता है।
match variable:
case value1:
statement
case value2:
statement
case _:
default_statement
day = int(input("Enter Day Number: "))
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Invalid Day")
|
if-elif |
Match-Case |
|
Conditions के लिए |
निश्चित Values के लिए |
|
Complex Logic |
Multiple Fixed Options |
|
सभी Versions |
Python 3.10+ |
Conditional (Ternary) Operator
यदि केवल एक Condition हो और छोटा Code लिखना हो, तो Ternary Operator का उपयोग किया जा सकता है।
value_if_true if condition else value_if_false
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)
Output
Adult
Program 1 – Positive, Negative या Zero
num = int(input("Enter Number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
a = int(input("First Number: "))
b = int(input("Second Number: "))
if a > b:
print("Largest =", a)
else:
print("Largest =", b)
year = int(input("Enter Year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Program 4 – Voting Eligibility
age = int(input("Enter Age: "))
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
a = int(input("First Number: "))
b = int(input("Second Number: "))
op = input("Enter Operator (+,-,*,/): ")
if op == "+":
print(a + b)
elif op == "-":
print(a - b)
elif op == "*":
print(a * b)
elif op == "/":
if b != 0:
print(a / b)
else:
print("Division by zero is not allowed.")
else:
print("Invalid Operator")
if age >= 18
❌ Error
✔ सही
if age >= 18:
if age >= 18:
print("Adult")
❌ IndentationError
❌ गलत
if age = 18:
✔ सही
if age == 18:
- हमेशा Conditions को स्पष्ट रखें।
- Complex Conditions में Parentheses () का उपयोग करें।
- अनावश्यक Nested if से बचें।
- Fixed Values के लिए Match-Case उपयोग करें।
- Meaningful Variable Names रखें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- if केवल True होने पर Execute होता है।
- if...else दो परिस्थितियों के लिए उपयोग होता है।
- if...elif...else अनेक Conditions के लिए उपयोग होता है।
- Nested if में एक if के अंदर दूसरा if होता है।
- match-case Python 3.10 और बाद के Versions में उपलब्ध है।
- Comparison Operators (==, >, <, !=) का उपयोग Conditions में किया जाता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python में Decision Making क्या है?
Conditions के आधार पर Program का अलग-अलग Code Execute करना।
प्रश्न 2. if और if...else में क्या अंतर है?
if केवल True होने पर चलता है, जबकि if...else में True और False दोनों स्थितियों के लिए अलग Block होता है।
प्रश्न 3. elif का उपयोग कब किया जाता है?
जब दो से अधिक Conditions की जाँच करनी हो।
एक if Statement के अंदर दूसरा if Statement लिखना।
प्रश्न 5. Match-Case किस Version में आया?
Python 3.10 में।
- if = एक Condition
- if...else = दो विकल्प
- if...elif...else = अनेक Conditions
- Nested if = एक if के अंदर दूसरा if
- match-case = Multiple Fixed Values
- Ternary Operator = एक लाइन में Condition
Python Loops (for Loop, while Loop, Nested Loop, break, continue, pass और range() Function) — इसमें Loop की आवश्यकता, for और while Loop, Nested Loops, Infinite Loop, break, continue, pass, range() Function तथा 30+ Practical Programs को विस्तार से सीखेंगे।