Python If Else in Hindi | if, elif aur else Conditions Explained 2026
अगर आप Python अभी-अभी सीखना शुरू कर रहे हैं, तो एक स्टेज ऐसी जरूर आता है जहाँ program को खुद फैसला लेना पड़ता है — कि आगे कौन सा रास्ता चुनना है। जैसे किसी student के marks 33 से ऊपर हैं तो उसे Pass दिखाना है, वरना Fail। या फिर age 18 से ज्यादा है तो वह व्यक्ति vote कर सकता है, या नहीं तो नहीं।
यही "फैसला लेने" वाला काम Python में if, elif और else की मदद से होता है। इन्हें Python में Conditional statements या decision making statements कहा जाता है।
इस पोस्ट में हम Python If Else को शुरुआत से, एकदम practical examples के साथ समझेंगे — ताकि आप सिर्फ syntax याद न करें, बल्कि यह समझें कि real programs में इसका इस्तेमाल कैसे होता है।
अगर आपने अभी तक Python Tutorial in Hindi वाली पार्ट नहीं देखा , तो पहले basics पर एक नज़र डाल लें — इससे आगे का कॉन्सेप्ट समझना और आसान हो जाएगा।
Python में Conditional Statement आखिर होता क्या है?
Simple शब्दों में — conditional statement का मतलब है कि program पहले एक condition check करे, और उसके result (True या False) के हिसाब से decide करे कि आगे क्या करना है।
age = 20
if age >= 18:
print("आप vote कर सकते हैं")
यहाँ Python सबसे पहले age>= 18 वाली condition check करेगा। अगर यह True निकली, तभी नीचे वाली line execute होगी।
Output:
आप vote कर सकते हैं
और अगर condition False होती, तो print() बिल्कुल भी execute नहीं होता — Python बस उस block को skip कर जाता।
Python If Statement — सबसे पहला कदम
if, Python की सबसे basic conditional statement है। इसका इस्तेमाल तब होता है जब हमें सिर्फ एक ही condition के true होने पर कुछ काम करवाना हो।
Syntax
if condition:
statement
ध्यान रहे — condition के बाद colon (:) लगाना जरूरी है, और अंदर लिखा गया code proper indentation के साथ होना चाहिए।
Example
marks = 75
if marks >= 33:
print("Student Pass है")
Output:
Student Pass है
यहाँ marks >= 33 हमारी condition है। अगर यह True है तो message print होगा, वरना कुछ नहीं होगा — क्योंकि अभी हमने else नहीं लगाया।
Python में If Else — जब दूसरा रास्ता भी चाहिए
सिर्फ if की एक limitation है — अगर condition false हो जाए, तो कोई दूसरा काम कैसे करवाएँ? इसी के लिए else आता है। if का condition true होने पर पहला block चलता है, और false होने पर else वाला block।
Syntax
if condition:
statement_if_true
else:
statement_if_false
Example
marks = 25
if marks >= 33:
print("Pass")
else:
print("Fail")
Output:
Fail
25 >= 33 false है, इसलिए if वाला code skip हो गया और else वाला block चल गया।
एक Real-Life Example — Login System
password = "python123"
if password == "python123":
print("Login Successful")
else:
print("Wrong Password")
Output:
Login Successful
यही pattern लगभग हर login form, हर form-validation और हर छोटे-बड़े web app में कहीं न कहीं इस्तेमाल होता है।
Comparison Operators — Conditions की नींव
Condition बनाने के लिए comparison operators सबसे ज्यादा काम आते हैं:
| Operator | मतलब |
|---|---|
| == | बराबर |
| != | बराबर नहीं |
| > | बड़ा |
| < | छोटा |
| >= | बड़ा या बराबर |
| <= | छोटा या बराबर |
इन operators की पूरी डिटेल आप Python Operators in Hindi वाली पोस्ट में देख सकते हैं (यहाँ अपनी existing Operators पोस्ट का लिंक लगाएँ)।
Multiple Conditions — जब एक Condition काफी नहीं है
कई बार सिर्फ एक condition से काम नहीं चलता, और, or, not जैसे logical operators तब मदद करते हैं।
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry Allowed")
else:
print("Entry Not Allowed")
Output:
Entry Allowed
यहाँ दोनों conditions एक साथ true होनी जरूरी हैं, क्योंकि and का इस्तेमाल हुआ है।
Python Elif — जब Options दो से ज्यादा हों
असली programs में अक्सर सिर्फ दो नहीं, कई possibilities होती हैं। जैसे marks के आधार पर grade तय करना:
- 90 या ज्यादा → Excellent
- 75 या ज्यादा → Very Good
- 60 या ज्यादा → Good
- 33 या ज्यादा → Pass
- इससे कम → Fail
बार-बार अलग-अलग if लिखने की बजाय, यहाँ elif (यानी "else if") काम आता है।
marks = 82
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
elif marks >= 60:
print("Good")
elif marks >= 33:
print("Pass")
else:
print("Fail")
Output:
Very Good
Python ऊपर से नीचे की तरफ conditions check करता जाता है, और जैसे ही कोई condition true मिलती है, उसका block चलाकर बाकी सारी conditions skip कर देता है।
If Elif Else — तीनों को एक साथ इस्तेमाल करना
if condition1:
# code
elif condition2:
# code
elif condition3:
# code
else:
# code
Example — Weather Check
temperature = 35
if temperature >= 40:
print("बहुत ज्यादा गर्मी है")
elif temperature >= 30:
print("गर्मी है")
elif temperature >= 20:
print("मौसम ठीक है")
else:
print("ठंड है")
Output:
गर्मी है
Practical Example — Even या Odd Number Check
number = 8
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output:
Even Number
यहाँ % modulus operator है — अगर number को 2 से divide करने पर remainder 0 बचे, तो number even होता है।
User Input के साथ If Else
अब तक हमने fixed values के साथ conditions देखीं। लेकिन real programs में value अक्सर user से ही ली जाती है।
age = int(input("अपनी age enter करें: "))
if age >= 18:
print("आप Adult हैं")
else:
print("आप Minor हैं")
अगर user 20 enter करता है, तो output आएगा — आप Adult हैं।
अगर आपने अभी तक input() function ठीक से नहीं समझा, तो Python Input and Output वाली पोस्ट (यहाँ अपनी existing Input/Output पोस्ट का लिंक डालें) एक बार जरूर पढ़ लें।
Conditional Statements कहाँ-कहाँ इस्तेमाल होते हैं?
ये सिर्फ किताबी examples नहीं हैं — असली projects में इनका इस्तेमाल हर जगह होता है:
1. Login System
if password == correct_password:
print("Login")
else:
print("Invalid Password")
2. Age Verification
if age >= 18:
print("Allowed")
else:
print("Not Allowed")
3. Result System
if marks >= 33:
print("Pass")
else:
print("Fail")
4. Discount System
if amount >= 5000:
print("20% Discount")
elif amount >= 2000:
print("10% Discount")
else:
print("No Discount")
Nested If — एक If के अंदर दूसरा If
कभी-कभी एक if के अंदर दूसरा if भी लिखना पड़ता है, इसे nested if कहते हैं।
age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry Allowed")
else:
print("ID Required")
else:
print("Age is too low")
Output:
Entry Allowed
Indentation क्यों इतनी जरूरी है?
Python में indentation सिर्फ style नहीं, बल्कि syntax का हिस्सा है। Python इसी से पहचानता है कि कौन सा code किस block के अंदर आता है।
गलत तरीका:
if age >= 18:
print("Adult")
सही तरीका:
if age >= 18:
print("Adult")
Beginners जो Common गलतियाँ करते हैं
1. Colon भूल जाना — if age >= 18: में colon लगाना न भूलें।
2. = और == को confuse करना — = value assign करता है, == दो values compare करता है।
age = 18 # assignment
if age == 18: # comparison
print("Age is 18")
3. गलत Indentation — Python में हर block properly indent होना चाहिए, वरना IndentationError आ जाता है।
4. Condition उल्टी लिखना
# गलत
if marks < 33:
print("Pass")
# सही
if marks >= 33:
print("Pass")
and, or, not — Multiple Conditions को जोड़ना
and — दोनों conditions true होनी चाहिए:
if age >= 18 and marks >= 50:
print("Eligible")
or — कम से कम एक condition true होनी चाहिए:
if day == "Sunday" or day == "Saturday":
print("Weekend")
not — condition के result को उल्टा कर देता है:
if not logged_in:
print("Please Login")
खुद Practice करें — कुछ Questions
- एक program बनाइए जो check करे कि number positive है या negative।
- एक program बनाइए जो check करे कि number even है या odd।
- Student के marks लेकर Pass या Fail print करें।
- Age के आधार पर Minor या Adult print करें।
- Marks के आधार पर Grade निकालें (90+ → A, 75+ → B, 60+ → C, 33+ → D, बाकी → F)
- Shopping amount के आधार पर discount calculate करें।
📥 इस पूरे गाइड को PDF में डाउनलोड करें
If, Elif aur Else Conditions पूरा नोट्स अपने पास सुरक्षित रखें और बिना इंटरनेट के भी पढ़ें।
PDF डाउनलोड करेंइन questions को खुद code करके देखें — सिर्फ पढ़ने से नहीं, practice से ही if-else पक्का होता है।
आगे क्या पढ़ें?
अगर आप Python शुरुआत से सीख रहे हैं, तो concepts को एक sequence में पढ़ना ज्यादा आसान रहता है — पहले basic syntax और variables, फिर data types और operators, और उसके बाद conditional statements और loops।
पूरी learning path के लिए Python Tutorial in Hindi देख सकते हैं। इसके अलावा Python Data Types , Python Type Casting और Python Strings वाली पोस्ट्स भी इसी क्रम में पढ़ने लायक हैं।
(FAQs)
Python If Else क्या है?
Python If Else एक conditional structure है, जिसका इस्तेमाल किसी condition के true या false होने के आधार पर अलग-अलग code execute करने के लिए किया जाता है।
Python में if statement क्या है?
if statement किसी एक condition को check करता है। अगर वह condition true होती है, तभी उसके अंदर लिखा code execute होता है।
Python में elif क्या है?
elif का मतलब है "else if"। इसका इस्तेमाल तब होता है जब हमें एक साथ कई conditions check करनी हों।
Python में else का क्या काम है?
जब if और सभी elif conditions false हो जाती हैं, तभी else वाला block execute होता है।
क्या Python में एक साथ कई elif इस्तेमाल कर सकते हैं?
हाँ, जरूरत के अनुसार एक if के बाद जितनी चाहें उतनी elif conditions लगाई जा सकती हैं।
Python में if और elif में क्या फर्क है?
if सबसे पहली condition check करता है, जबकि elif तभी अगली condition check करता है जब उससे पहले वाली condition false हो चुकी हो।
Python में = और == में क्या अंतर है?
= का इस्तेमाल किसी variable में value assign करने के लिए होता है, जबकि == दो values की equality check करने के लिए इस्तेमाल होता है।
Related Posts
- MySQL Tutorial in Hindi – Beginner to Advanced (Full Roadmap)
- MySQL Lesson 1 – What is MySQL in Hindi
- MySQL Lesson 1-B – Database Introduction in Hindi
- MySQL Lesson 1-A – MySQL Installation in Hindi
- MySQL Lesson 1-C – SQL Commands: DDL, DML aur DQL in Hindi
- Python Tutorial in Hindi – Full Roadmap
- Python Type Casting in Hindi – int(), float(), str() se Data Type Convert karna seekhein
- Python me Multiline Comments kaise likhein
- Python Cheat Sheet in Hindi
- AI Applications kya hain? Healthcare, Finance, Education aur Gaming mein use
- AI kaise kaam karta hai? Step-by-step guide
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें