Python Conditional Statements (if, if-else, if-elif-else, Nested if) in Hindi | Decision Making का Complete Guide | Python Tutorial Part 6
Introduction
कल्पना कीजिए कि आप ATM से पैसे निकालने जाते हैं।
आप कार्ड डालते हैं।
ATM सबसे पहले क्या करता है?
वह एक निर्णय (Decision) लेता है।
क्या कार्ड वैध है?
क्या PIN सही है?
क्या खाते में पर्याप्त बैलेंस है?
क्या निकाली जाने वाली राशि उपलब्ध है?
यदि हर उत्तर "हाँ" है, तभी पैसे निकलते हैं।
यदि किसी भी चरण पर उत्तर "नहीं" हो, तो प्रक्रिया रुक जाती है।
यही प्रक्रिया Programming में Decision Making कहलाती है।
Python में Decision लेने के लिए Conditional Statements का उपयोग किया जाता है।
यदि आपने Variables और Operators सीख लिए हैं, तो अब Programming वास्तव में शुरू होती है।
Conditional Statement क्या होता है?
Conditional Statement ऐसा Code होता है जो किसी Condition के आधार पर निर्णय लेता है।
सरल भाषा में—
यदि (If) कोई शर्त सही है, तो एक कार्य करो। अन्यथा दूसरा कार्य करो।
उदाहरण:
यदि बारिश हो रही है तो छाता लेकर जाओ।
यदि परीक्षा में 33 से अधिक अंक हैं तो पास।
यदि उम्र 18 वर्ष या अधिक है तो वोट दे सकते हैं।
यदि बैंक बैलेंस पर्याप्त है तो भुगतान सफल।
यही Logic Computer भी अपनाता है।
Python में Conditional Statements के प्रकार
Python मुख्य रूप से चार प्रकार के Conditional Statements प्रदान करता है:
ifif...elseif...elif...elseNested if
इन चारों को समझने के बाद आप लगभग हर प्रकार का Decision Making Program लिख पाएँगे।
1. if Statement
यदि केवल एक Condition Check करनी हो, तो if का उपयोग किया जाता है।
Syntax
if condition:
statement
यदि Condition True है तो Code चलेगा।
यदि False है तो Python उस Block को छोड़ देगा।
Example 1 – Voting Eligibility
age = 20
if age >= 18:
print("You are eligible to vote.")
Output
You are eligible to vote.
Example 2 – Temperature Alert
temperature = 42
if temperature > 40:
print("Heat Wave Alert")
Indentation क्या होती है?
Python की सबसे विशेष बात यह है कि इसमें Curly Braces ({}) का उपयोग नहीं होता।
Code Block दिखाने के लिए Indentation (Space) का उपयोग किया जाता है।
सही उदाहरण:
if age >= 18:
print("Eligible")
गलत उदाहरण:
if age >= 18:
print("Eligible")
इससे IndentationError आएगा।
2. if...else Statement
जब दो संभावित परिणाम हों, तब if...else उपयोग किया जाता है।
Syntax
if condition:
statement
else:
statement
Example – Pass या Fail
marks = 72
if marks >= 33:
print("Pass")
else:
print("Fail")
Real Life Example – ATM Balance
balance = 5000
withdraw = 6000
if withdraw <= balance:
print("Transaction Successful")
else:
print("Insufficient Balance")
यही Logic लगभग हर Banking Application में उपयोग होता है।
3. if...elif...else Statement
जब कई Conditions हों, तब elif का उपयोग किया जाता है।
Example – Grade System
marks = 84
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")
यह कैसे काम करता है?
Python ऊपर से नीचे तक Conditions Check करता है।
पहली True Condition मिलते ही बाकी Conditions Check नहीं होतीं।
इसी कारण Conditions का क्रम (Order) बहुत महत्वपूर्ण होता है।
Common Mistake
गलत
if marks >= 33:
print("Pass")
elif marks >= 90:
print("A+")
यहाँ 95 अंक वाला छात्र भी केवल "Pass" पाएगा, क्योंकि पहली Condition पहले ही True हो गई।
हमेशा बड़ी Condition पहले लिखें।
4. Nested if Statement
जब एक Condition के अंदर दूसरी Condition Check करनी हो।
इसे Nested if कहते हैं।
Example – College Admission
age = 19
marks = 82
if age >= 17:
if marks >= 60:
print("Admission Approved")
else:
print("Marks Too Low")
else:
print("Age Criteria Not Met")
Real Life Example – Login System
username = input("Username: ")
password = input("Password: ")
if username == "admin":
if password == "12345":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Invalid Username")
Mini Project 1 – Even or Odd
number = int(input("Enter Number: "))
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Mini Project 2 – Largest Number
a = int(input("First Number: "))
b = int(input("Second Number: "))
if a > b:
print("Largest:", a)
else:
print("Largest:", b)
Mini Project 3 – BMI Category
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
bmi = weight / (height * height)
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal Weight")
elif bmi < 30:
print("Overweight")
else:
print("Obese")
Best Practices
हमेशा Meaningful Variable Names रखें।
Conditions को सरल रखें।
बड़े Logic को छोटे Blocks में बाँटें।
Indentation पर विशेष ध्यान दें।
पहले आसान Conditions, फिर जटिल Logic लिखें।
सामान्य गलतियाँ
❌ = की जगह == भूल जाना
if age = 18:
✔ सही
if age == 18:
❌ Indentation गलत होना
Python में Indentation केवल Style नहीं, बल्कि Syntax का हिस्सा है।
Practice Questions
User से उम्र लेकर Voting Eligibility बताइए।
Student का Grade निकालिए।
तीन Numbers में सबसे बड़ा Number खोजिए।
Leap Year Checker बनाइए।
Electricity Bill Calculator बनाइए।
Income Tax Slab Program बनाइए।
School Result Management Program बनाइए।
Interview Questions
if Statement क्या है?
if और if...else में क्या अंतर है?
elif कब उपयोग किया जाता है?
Nested if क्या होता है?
Python में Indentation क्यों आवश्यक है?
Decision Making Programming क्या होती है?
Chapter Summary
इस अध्याय में आपने Python के सबसे महत्वपूर्ण Control Structure को सीखा।
अब आप ऐसे Programs लिख सकते हैं जो केवल Input नहीं लेते, बल्कि परिस्थिति के अनुसार सही निर्णय भी लेते हैं।
यही Concept आगे चलकर Banking Software, Hospital Management System, E-commerce Websites, School ERP, Online Examination System और Artificial Intelligence तक में उपयोग होता है।

0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें