← Back

Home › Hindi › if else Hindi में

Python if else Hindi में: condition और elif कैसे लिखें?

🌐 यह page Hindi में है · Python basics in English →

Beginner conditionsExamples + outputFree bug-fix practice

if else क्या है?

if किसी expression को जाँचता है: True हो तो उसके नीचे indented block चलता है; False हो तो optional else block। दोनों एक साथ कभी नहीं चलते। जैसे marks से pass/fail तय करना:

marks = 42
if marks >= 33:
    print("Pass")
else:
    print("Try again")
# output: Pass

हर condition के बाद : लगाओ और अगले statement को चार spaces indent करो। Python में braces नहीं होते: whitespace ही तय करता है कि कौन-सी line किस branch में है।

तीन या ज़्यादा रास्ते: elif

elif का मतलब else-if है। Conditions ऊपर से नीचे जाँची जाती हैं; पहली True branch चलने के बाद बाकी सब skip होते हैं। इसलिए अधिक specific condition पहले रखो।

score = 85
if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 33:
    grade = "C"
else:
    grade = "F"
print(grade)  # output: B

अगर score >= 33 पहले लिख दिया होता, तो 85 पर भी C मिलता। अलग-अलग if लिखने से एक से ज्यादा branch चल सकती हैं; यहाँ केवल एक grade चाहिए।

= और == का फर्क; and / or

= value assign करता है; == values compare करता है। Python में if age = 18: लिखने से SyntaxError आएगा। Range जाँचने के लिए and या chained comparison इस्तेमाल करो:

age = 17
if 13 <= age < 20:
    print("teenager")
# output: teenager

logged_in = True
is_admin = False
if logged_in and is_admin:
    print("admin access")
else:
    print("no admin access")
# output: no admin access

and को दोनों True चाहिए; or को कोई एक True काफी है। not True/False उलट देता है। Input से number चाहिए तो input() को पहले int में बदलने का तरीका देखो।

Nested if और indentation की गलती

एक condition के अंदर दूसरी condition रख सकते हो। नीचे discount सिर्फ adult और student दोनों होने पर मिलेगा:

age = 20
student = True
if age >= 18:
    if student:
        print("Student discount")
    else:
        print("Regular adult ticket")
else:
    print("Minor ticket")
# output: Student discount

दूसरा else outer if का है क्योंकि दोनों का indentation बराबर है। गलत level पर रखोगे तो logic बदल जाएगा या IndentationError आएगा। सरल case में if age >= 18 and student: लिखना भी ठीक है।

खुद solve करो: zero, positive या negative?

Exercise: number = 0 से शुरू करो। Positive, Negative या Zero में से सही word print करो। पहले number > 0, फिर number < 0, अंत में else रखो। Expected output Zero है। उसके बाद -4 और 9 पर भी जाँचो। Playground में code run कर सकते हो; इस indentation bug को fix करके देखो।

Common trap: if value: केवल value की truthiness जाँचता है; 0 और empty string False हैं। अगर खासकर zero पहचानना है तो value == 0 लिखो।

🛠️ Practice करो (free, browser में)

पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।

सारे free problems देखो →

अक्सर पूछे जाने वाले सवाल

Python में if elif else का क्रम क्या है?

पहले if, फिर जरूरत के मुताबिक elif, और अंत में optional else। ऊपर से पहली True condition का block चलता है।

if condition में = की जगह == क्यों लिखते हैं?

= assignment है, == equality comparison है। Condition में equality जाँचने के लिए == लिखें।

Python if के बाद colon और spaces ज़रूरी हैं?

हाँ। Condition के अंत में colon और अगले block में consistent indentation (आमतौर पर चार spaces) ज़रूरी है।

आगे पढ़ो