← Back to Python Course | Chapter 3: Control Flow | Lesson 10 of 10

Python में Nested If

Nested if असल में एक if statement है जो दूसरे if statement की body के अंदर लिखा गया है -- यह आपको पहली condition संतुष्ट होने के बाद ही दूसरी condition जाँचने देता है, यह उन decisions के लिए उपयोगी है जो सच में क्रम से एक से ज़्यादा independent सवालों पर निर्भर करते हैं।
Syntax
python
if condition1:
    if condition2:
        # both are True
    else:
        # only condition1 is True

बेसिक Nested If Structure

एक if statement को दूसरे के indented body के अंदर रखने से nested if बनता है -- Python खुद indentation का इस्तेमाल करके तय करता है कि inner if किस block का हिस्सा है, इसलिए inner condition तक सिर्फ़ तभी पहुँचा जाता है जब outer condition पहले ही संतुष्ट हो चुकी हो।

Note: जब भी संभव हो nested if blocks को shallow (एक या दो levels) रखें -- गहरी nesting आमतौर पर इस बात का संकेत है कि logic को ज़्यादा साफ़ तरीके से restructure किया जा सकता है।
Warning: nested if में असंगत (inconsistent) indentation Python में सिर्फ़ style की बात नहीं है -- यह बदल देती है कि code असल में किस block का हिस्सा है, या सीधे एक IndentationError raise कर देती है।

उदाहरण: Basic Nested If Structure

python
age = 25
has_id = True
if age >= 18:
    if has_id:  # inner if only reached because the outer condition was True
        print("Entry allowed")

Else Block के अंदर Nesting

nested if उतनी ही आसानी से else block के अंदर भी रह सकता है जितना if block के अंदर, जिससे आप outer condition की किस branch पर गए इस पर निर्भर करते हुए sub-cases का बिल्कुल अलग सेट संभाल सकते हैं।

Note: else के अंदर nesting का इस्तेमाल खासतौर पर तब करें जब "otherwise" वाले path के खुद के ऐसे distinct sub-cases हों जिन्हें अलग करना सच में उचित हो।
Warning: एक ही outer statement की if AND else दोनों branches में गहराई से nest करना जल्दी ही समझना मुश्किल बना सकता है -- कई branches के लिए match-case या restructuring पर विचार करें।

उदाहरण: Nesting Inside an Else Block

python
logged_in = False
is_admin = False
if logged_in:
    print("Welcome")
else:
    if is_admin:  # nested if inside the else branch
        print("Admin fallback")
    else:
        print("Please log in")

कब Conditions को and से Combine करें

अगर कोई inner if तभी चलना चाहिए जब outer AND inner दोनों conditions True हों -- और "outer true, inner false" के लिए कोई खास handling न चाहिए हो -- तो उन्हें and से एक ही if में combine करना कम nesting और कम indentation के साथ बिल्कुल वही behavior देता है।

Note: दो if statements को nest करने से पहले यह पूछें कि क्या आपको सच में "outer true, inner false" case के लिए खास handling चाहिए -- अगर नहीं, तो and उन्हें साफ़-सुथरे ढंग से combine कर देता है।
Warning: and से conditions को combine करना nesting जैसा बर्ताव तभी करता है जब outer if और inner if के बीच ऐसा कोई code न हो जिसे inner condition चाहे जो भी हो, हर हाल में चलना ज़रूरी हो।

उदाहरण: When to Combine Conditions with and Instead

python
age = 20
has_ticket = True
if age >= 18 and has_ticket:  # combines both checks into a single condition
    print("Entry allowed")

Multi-Step Validation के लिए Nested If

Nested ifs स्वाभाविक रूप से तब फिट बैठते हैं जब हर validation step को पिछले step के पास होने के बाद ही चलना चाहिए -- जैसे किसी dictionary में key exists करने की पुष्टि करना, उसकी value की कोई property जाँचने से पहले, क्योंकि किसी missing चीज़ की property जाँचने से error आ सकता है।

Note: nested validation checks को "क्या यह बिल्कुल exist करता है" से शुरू करके बाहर की ओर "क्या यह specifically सही है" तक क्रम में रखें, ताकि पहली जाँचें बाद वाली जाँचों को missing data पर काम करने से बचाएँ।
Warning: existence check को skip करके सीधे detailed validation में कूद जाने से, जब जाँची जा रही value बिल्कुल भी exist नहीं करती, तो KeyError या AttributeError आ सकता है।

उदाहरण: Nested If for Multi-Step Validation

python
user = {"name": "Alex"}
if "name" in user:  # check the key exists before using it
    if len(user["name"]) > 0:
        print("Valid name:", user["name"])

Readability: Deep Nesting के विकल्प

दो या तीन levels के बाद, nested ifs को समझना सच में मुश्किल हो जाता है -- early returns (किसी condition के fail होते ही function से बाहर निकल जाना) या conditions को and से combine करने जैसी techniques गहराई से nested logic को ऊपर से नीचे स्कैन करने में कहीं आसान चीज़ में flatten कर सकती हैं।

Note: किसी function में, "invalid" case के लिए शुरुआत में ही एक early return अक्सर बाद में आने वाले "valid" logic के लिए nesting का पूरा एक level हटा देता है।
Warning: बहुत गहरी nesting (चार या ज़्यादा levels) एक मज़बूत संकेत है कि logic को refactor किया जाना चाहिए, यह सिर्फ़ personal style की पसंद की बात नहीं है।

उदाहरण: Readability: Alternatives to Deep Nesting

python
def check_access(age, has_id):
    if age < 18:
        return "Denied"  # early return flattens the nesting
    if not has_id:
        return "Denied"
    return "Allowed"

print(check_access(25, True))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.