Python Nested If Statement क्या है? Examples, Syntax & Programs | RBSE Class 12
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 कैसे काम करता है?
- सबसे पहले outer
ifकी condition check होती है। - यदि outer condition True होती है, तो उसके अंदर का code execute होता है।
- इसके बाद inner
ifकी condition check होती है। - 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")
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
- Nested If Statement क्या है?
- Python में Nested If की syntax लिखिए।
- Nested If का एक example लिखिए।
- Nested If में indentation का क्या महत्व है?
- Nested If और Elif में अंतर लिखिए।
- Nested If में else का उपयोग कैसे किया जाता है?
- एक number positive और even है या नहीं, check करने का Python program लिखिए।
- Student के pass होने और grade check करने का Python program लिखिए।
- तीन numbers में सबसे बड़ा number ज्ञात करने के लिए Nested If का program लिखिए।
- 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 |
