Python Nested If Statement क्या है? Examples, Syntax & Programs | RBSE Class 12

इस पोस्ट में: Python Nested If Statement क्या होता है, इसकी syntax क्या है, Nested If का उपयोग कब किया जाता है और Python programs में एक condition के अंदर दूसरी condition कैसे लगाते हैं—इन सभी concepts को आसान Hindi में examples के साथ समझेंगे।

Python Nested If क्या है?

जब किसी if statement के अंदर दूसरा if statement लिखा जाता है, तो उसे Nested If Statement कहते हैं।

सरल शब्दों में, एक if के अंदर दूसरा if लगाना Nested If कहलाता है।

Nested If का Basic Example

age = 20

if age >= 18:
    if age >= 21:
        print("Age is 21 or above")
    else:
        print("Age is between 18 and 20")
else:
    print("Age is below 18")

यहाँ outer if के अंदर एक दूसरा if है। इसलिए यह Nested If का उदाहरण है।

Nested If की Syntax

if condition1:

    if condition2:
        statement

    else:
        statement

else:
    statement

Nested If में indentation यानी उचित spacing बहुत महत्वपूर्ण होती है। Python में code block की पहचान indentation से होती है।

Nested If कैसे काम करता है?

  1. सबसे पहले outer if की condition check होती है।
  2. यदि outer condition True होती है, तो उसके अंदर का code execute होता है।
  3. इसके बाद inner if की condition check होती है।
  4. Inner condition के True या False होने के आधार पर उसका संबंधित block execute होता है।

Example 1 — Positive Number और Even Number Check करना

number = 20

if number > 0:

    if number % 2 == 0:
        print("Number is positive and even")

    else:
        print("Number is positive but odd")

else:
    print("Number is not positive")

इस Program में क्या हुआ?

पहले check किया गया कि number positive है या नहीं। यदि number positive है, तब अंदर वाला if check करता है कि number even है या odd।

Example 2 — Student Pass और Grade Check करना

marks = 75

if marks >= 33:

    print("Student is Pass")

    if marks >= 75:
        print("Grade A")

    else:
        print("Grade B")

else:
    print("Student is Fail")

यहाँ पहले student के pass होने की condition check होती है। यदि student pass है, तभी अंदर grade की condition check की जाती है।

Example 3 — Number Positive, Negative या Zero

number = 10

if number >= 0:

    if number == 0:
        print("Number is Zero")

    else:
        print("Number is Positive")

else:
    print("Number is Negative")

Example 4 — Login System

Nested If का उपयोग simple login logic को समझने के लिए भी किया जा सकता है।

username = "admin"
password = "1234"

if username == "admin":

    if password == "1234":
        print("Login Successful")

    else:
        print("Incorrect Password")

else:
    print("Incorrect Username")
ध्यान दें: यह केवल programming concept समझाने का example है। वास्तविक applications में passwords को plain text में store करना उचित security practice नहीं है।

Example 5 — तीन Numbers में बड़ा Number

a = 30
b = 20
c = 10

if a > b:

    if a > c:
        print("A is largest")

    else:
        print("C is largest")

else:

    if b > c:
        print("B is largest")

    else:
        print("C is largest")

Nested If में else का उपयोग

Nested If के अंदर else भी लगाया जा सकता है।

age = 25

if age >= 18:

    if age >= 21:
        print("Age is 21 or above")

    else:
        print("Age is between 18 and 20")

else:
    print("Age is below 18")

Nested If और Normal If-Else में अंतर

Normal If-Else Nested If
एक condition के आधार पर decision लिया जाता है। एक if के अंदर दूसरा if होता है।
Structure comparatively simple होता है। Multiple dependent conditions को check किया जा सकता है।
Simple decisions के लिए उपयोगी। जब दूसरी condition पहली condition पर depend करती है तब उपयोगी।

Nested If और Elif में अंतर

Nested If Elif
एक if के अंदर दूसरा if लिखा जाता है। एक ही decision structure में दूसरी condition check की जाती है।
Dependent conditions के लिए उपयोगी हो सकता है। Multiple alternatives के लिए उपयोगी है।

Nested If का Real-Life Logic

मान लीजिए किसी school में पहले यह check करना है कि student ने परीक्षा पास की है या नहीं। यदि student पास है, तभी उसकी grade check करनी है।

Logic इस प्रकार होगा:

Pass?

   |
   |-- No  → Fail
   |
   |-- Yes
          |
          |-- Marks high → Grade A
          |
          |-- Otherwise  → Grade B

यही logic Python में Nested If के माध्यम से लिखा जा सकता है।

Nested If में Indentation क्यों जरूरी है?

Python में indentation code blocks को define करती है। इसलिए Nested If लिखते समय inner if को उचित indentation के साथ लिखना जरूरी है।

सही तरीका:

if age >= 18:
    if age >= 21:
        print("Eligible")

यदि indentation गलत होगी तो Python error दे सकता है या program का logic अपेक्षित तरीके से काम नहीं करेगा।

Nested If में Common Mistakes

  • Inner if की indentation गलत करना।
  • Condition के बाद colon : भूल जाना।
  • Comparison के लिए == की जगह गलती से = का उपयोग करना।
  • बहुत अधिक Nested If बनाकर program को unnecessarily complex करना।
  • else को गलत if के साथ associate करना।

Nested If का Practical Program

अब एक ऐसा program देखते हैं जिसमें number की value के आधार पर multiple conditions check की जाती हैं।

number = int(input("Enter a number: "))

if number > 0:

    print("Number is Positive")

    if number % 2 == 0:
        print("Number is Even")

    else:
        print("Number is Odd")

else:

    if number == 0:
        print("Number is Zero")

    else:
        print("Number is Negative")

Program का उदाहरण

यदि user input देता है:

20

तो output होगा:

Number is Positive
Number is Even

Nested If को आसान भाषा में याद रखें

याद रखने का आसान तरीका:

If के अंदर If = Nested If

पहले बाहरी condition check होती है और उसके True होने पर अंदर वाली condition check की जा सकती है।

RBSE Class 12 Important Questions

  1. Nested If Statement क्या है?
  2. Python में Nested If की syntax लिखिए।
  3. Nested If का एक example लिखिए।
  4. Nested If में indentation का क्या महत्व है?
  5. Nested If और Elif में अंतर लिखिए।
  6. Nested If में else का उपयोग कैसे किया जाता है?
  7. एक number positive और even है या नहीं, check करने का Python program लिखिए।
  8. Student के pass होने और grade check करने का Python program लिखिए।
  9. तीन numbers में सबसे बड़ा number ज्ञात करने के लिए Nested If का program लिखिए।
  10. Nested If में होने वाली कोई तीन common mistakes लिखिए।

Multiple Choice Questions (MCQs)

प्रश्न 1. एक if statement के अंदर दूसरा if statement कहलाता है—

(अ) Simple If

(ब) Nested If

(स) Loop

(द) Function

उत्तर: (ब) Nested If


प्रश्न 2. Python में code block को identify करने में किसका महत्वपूर्ण role है?

(अ) Indentation

(ब) Comment

(स) Variable

(द) Operator

उत्तर: (अ) Indentation


प्रश्न 3. Python में condition के अंत में सामान्यतः किस symbol का उपयोग होता है?

(अ) ;

(ब) :

(स) ,

(द) .

उत्तर: (ब) :


प्रश्न 4. Nested If में inner if कब execute हो सकता है?

(अ) जब outer condition True हो

(ब) हमेशा

(स) कभी नहीं

(द) केवल program समाप्त होने पर

उत्तर: (अ) जब outer condition True हो

Frequently Asked Questions (FAQs)

Nested If Statement क्या है?

जब एक if statement के अंदर दूसरा if statement लिखा जाता है, तो उसे Nested If Statement कहते हैं।

Nested If का उपयोग कब किया जाता है?

जब एक condition के True होने के बाद दूसरी dependent condition को check करना हो, तब Nested If उपयोगी हो सकता है।

क्या Nested If के अंदर else लगाया जा सकता है?

हाँ। Inner या outer decision structure के अनुसार else का उपयोग किया जा सकता है।

Nested If में indentation जरूरी है?

हाँ। Python में indentation code blocks को निर्धारित करने में महत्वपूर्ण भूमिका निभाती है।

Nested If और Elif क्या समान हैं?

नहीं। Nested If में एक if के अंदर दूसरा if होता है, जबकि elif का उपयोग एक ही conditional structure में alternative conditions check करने के लिए किया जाता है।

Quick Revision

Concept याद रखने वाली बात
Nested If if के अंदर if
Syntax if condition: → अंदर दूसरा if
Indentation Code block बताती है
else False condition के लिए alternative block
Use Dependent / multiple conditions

Related Python Topics

RBSE Python Study Tip: Nested If को समझने के लिए पहले simple If-Else को अच्छी तरह समझें। फिर छोटे programs बनाकर outer और inner conditions को अलग-अलग trace करें। इससे conditional programming की foundation मजबूत होगी।