Conditional Statements: if, elif, else in Python Notes in Hindi & English : पायथन कंडीशनल स्टेटमेंट्स: if, elif और else की संरचना और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Conditional Statements: if, elif, else in Python (पायथन कंडीशनल स्टेटमेंट्स: if, elif और else की संरचना) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Conditional statements (if, elif, else) control branching in Python by evaluating conditions sequentially from top to bottom and executing the first matching indented block. Python enforces indentation instead of curly braces (PEP 8 specifies 4 spaces) and implicitly converts any expression via truthy/falsy rules.

अध्याय सार: पायथन में कंडीशनल स्टेटमेंट्स (if, elif, else) शर्तों का ऊपर से नीचे क्रमिक मूल्यांकन करके पहले सत्य होने वाले इंडेंटेड ब्लॉक को चलाते हैं। पायथन में कोष्ठकों के स्थान पर इंडेंटेशन (PEP 8 के अनुसार 4 स्पेस) अनिवार्य है और शर्तों में किसी भी मान की जाँच ट्रुथी/फॉल्सी नियमों के आधार पर होती है।

Conditional Statements: if, elif, else in Python: Detailed Notes & Architecture (पायथन कंडीशनल स्टेटमेंट्स: if, elif और else की संरचना)

1. Syntax, Colons, and Mandatory Indentation (सिंटैक्स, कोलन और अनिवार्य इंडेंटेशन नियम)

[EN] Every conditional header—if condition:, elif condition:, or else:—must terminate with a colon (:), and its body must be consistently indented (PEP 8 recommends 4 spaces per level, never mixing tabs and spaces). Mixing tabs and spaces triggers TabError, while misaligned blocks raise IndentationError. Notice Python uses the single keyword elif rather than else if, keeping multi-way branches flat without nested staircases.

[HI] प्रत्येक कंडीशनल हेडर—if condition:, elif condition: या else:—के अंत में कोलन (:) लगाना और उसके कोड ब्लॉक को एकसमान इंडेंट करना (PEP 8 के अनुसार 4 स्पेस) अनिवार्य है। टैब और स्पेस मिलाने पर TabError तथा असमान इंडेंट पर IndentationError आता है। पायथन में बहु-शाखा शर्तों के लिए else if के स्थान पर एकल कीवर्ड elif होता है।

2. Exclusive if-elif Chains vs Independent if Statements (विशिष्ट if-elif श्रृंखला बनाम स्वतंत्र if स्टेटमेंट्स)

[EN] In an if / elif / else ladder, Python tests conditions top-down and stops checking immediately after the first truthy condition matches, so AT MOST ONE suite executes. The optional trailing else: runs only when every preceding if and elif evaluated falsy. By contrast, consecutive separate if statements evaluate independently, meaning multiple blocks can execute for a single input.

[HI] if / elif / else श्रृंखला में पायथन ऊपर से नीचे शर्तें जाँचता है और पहली शर्त सत्य होते ही आगे की सभी elif और else शाखाओं को छोड़ देता है, अर्थात् अधिकतम केवल एक ही ब्लॉक चलता है। अंतिम else: तभी चलता है जब ऊपर की सभी शर्तें असत्य रही हों। इसके विपरीत, अलग-अलग लिखे कई if स्टेटमेंट एक-दूसरे से स्वतंत्र होते हैं और एक से अधिक ब्लॉक चल सकते हैं।

3. Truthy, Falsy, and Chained Comparisons (ट्रुथी, फॉल्सी और चेन्ड तुलना (Chained Comparisons))

[EN] Python evaluates any object in an if condition using bool(obj). Exactly these built-in values are falsy: False, None, numeric zeros (0, 0.0, 0j), and empty collections ('', [], (), {}, set(), frozenset(), range(0)). All other values—including non-empty strings like 'False' or '0', negative numbers like -1, and [0]—are truthy! Python also supports mathematical chained comparisons such as 10 <= x <= 20, which evaluates x once and acts like 10 <= x and x <= 20.

[HI] पायथन if शर्त में किसी भी ऑब्जेक्ट पर स्वतः bool(obj) लागू करता है। मानक फॉल्सी मान हैं: False, None, शून्य (0, 0.0, 0j) तथा खाली संग्रह ('', [], (), {}, set(), frozenset(), range(0))। इनके अलावा अन्य सभी मान—जैसे 'False', '0', -1 और [0]—ट्रुथी होते हैं। पायथन 10 <= x <= 20 जैसी गणितीय चेन्ड तुलना को भी सपोर्ट करता है, जिसमें x केवल एक बार इवैल्यूएट होता है।

4. Nested Conditionals, pass Placeholder, and Dangling-Else (नेस्टेड कंडीशंस, pass स्टेटमेंट और डैंगलिंग-एल्स समाधान)

[EN] An if block can be nested inside another if or else branch, and indentation explicitly pairs each else with its matching if—completely eliminating the C/Java dangling-else ambiguity. If you need an empty stub branch during development, write pass inside the block because Python forbids empty suites after a colon. Note also that standard assignment (=) inside an if header is a SyntaxError, though the walrus operator (:=, Python 3.8+) permits inline assignment expressions like if (n := len(a)) > 5:.

[HI] एक if ब्लॉक के भीतर दूसरा if या else नेस्ट किया जा सकता है, और पायथन में इंडेंटेशन स्पष्ट कर देता है कि प्रत्येक else किस if से जुड़ा है—जिससे C/Java जैसी डैंगलिंग-एल्स अस्पष्टता नहीं होती। खाली ब्लॉक छोड़ने पर IndentationError आता है, इसलिए प्लेसहोल्डर के रूप में pass लिखना आवश्यक है। ध्यान दें कि if की शर्त में साधारण असाइनमेंट (=) SyntaxError देता है, जबकि वालरस ऑपरेटर (:=, पायथन 3.8+) मान्य है।

Syntax, Formula & Technical Example (Conditional Statements: if, elif, else in Python)

marks = 78
if 90 <= marks <= 100:
    grade = 'A+'
elif marks >= 75:
    grade = 'A'
else:
    grade = 'B'
print(grade, bool('False'), bool([]))  # Output: A True False

Conditional Statements: if, elif, else in Python Quick Reference & Comparison Table

Construct / ValueEvaluation BehaviorKey Exam Fact
if / elif / elseExecutes at most one branchRemaining elif blocks are skipped after match
0, None, '', [], {}All evaluate as FalsyNon-empty strings like 'False' are Truthy
10 <= x < 20Equivalent to 10 <= x and x < 20x is evaluated only once in chained check
Missing colon or indentRaises SyntaxError / IndentationErrorPEP 8 specifies 4 spaces per indent level

Conditional Statements: if, elif, else in Python Top 20 Bilingual MCQs with Answers

Q1. Which keyword does Python use for chained multi-way conditional branches?

(पायथन में बहु-शाखा शर्तों के लिए कौन-सा कीवर्ड उपयोग होता है?)

(A) elif

(B) elseif

(C) switch

(D) else if

Correct Answer: (A)

[EN] Python uses the single keyword elif rather than two words else if.

[HI] पायथन में दो शब्दों के else if के बजाय एकल कीवर्ड elif का प्रयोग होता है।

Q2. What does if 'False': print('Yes') else: print('No') output?

(if 'False': print('Yes') else: print('No') का आउटपुट क्या होगा?)

(A) No

(B) Nothing prints

(C) Yes

(D) Raises TypeError

Correct Answer: (C)

[EN] A non-empty string 'False' is truthy in Python regardless of its text content.

[HI] पायथन में कोई भी गैर-खाली स्ट्रिंग—भले ही वह 'False' हो—हमेशा ट्रुथी मानी जाती है।

Q3. Which value evaluates to True inside an if condition?

(if शर्त में निम्नलिखित में से कौन-सा मान True के रूप में कार्य करेगा?)

(A) None

(B) 0.0

(C) []

(D) [0]

Correct Answer: (D)

[EN] [0] is a non-empty list of length 1 so it is truthy; [], 0.0, and None are all falsy.

[HI] [0] एक गैर-खाली लिस्ट है इसलिए ट्रुथी है; [], 0.0 और None सभी फॉल्सी हैं।

Q4. What is the output of x = 15; print('A' if x > 10 else 'B') if converted to an if-elif-else: if x > 5: r = 1 elif x > 10: r = 2 else: r = 3; print(r)?

(x = 15 के लिए if x > 5: r = 1 elif x > 10: r = 2 else: r = 3; print(r) का आउटपुट क्या होगा?)

(A) 1 and 2 both

(B) 1

(C) 3

(D) 2

Correct Answer: (B)

[EN] Because x > 5 is checked first and is True for x = 15, r becomes 1 and elif x > 10 is never checked.

[HI] x = 15 के लिए पहली शर्त x > 5 सत्य हो जाती है, इसलिए r = 1 बनता है और आगे की elif शाखा नहीं चलती।

Q5. What error occurs if you omit the indented body below if x > 0: and immediately write print('done') at column 0?

(if x > 0: के नीचे इंडेंटेड ब्लॉक छोड़े बिना अगली लाइन में कॉलम 0 पर print('done') लिखने से कौन-सी त्रुटि आती है?)

(A) ValueError

(B) IndentationError

(C) NameError

(D) RuntimeError

Correct Answer: (B)

[EN] Python requires an indented suite after every block header colon; missing indentation raises IndentationError.

[HI] प्रत्येक ब्लॉक हेडर के बाद इंडेंटेड कोड अनिवार्य है, अन्यथा IndentationError आता है।

Q6. What does 1 < 5 < 3 evaluate to in Python?

(पायथन में 1 < 5 < 3 का मूल्यांकन क्या होता है?)

(A) False

(B) True

(C) Raises SyntaxError

(D) Raises TypeError

Correct Answer: (A)

[EN] Chained comparison 1 < 5 < 3 expands to (1 < 5) and (5 < 3), which is True and False -> False.

[HI] चेन्ड तुलना 1 < 5 < 3 का अर्थ (1 < 5) and (5 < 3) है, जो True and False से False देता है।

Q7. What is the output of x = 0; if x != 0 and 10 / x > 2: print('OK') else: print('Safe')?

(x = 0; if x != 0 and 10 / x > 2: print('OK') else: print('Safe') क्या प्रिंट करेगा?)

(A) Safe

(B) OK

(C) Nothing prints

(D) Raises ZeroDivisionError

Correct Answer: (A)

[EN] Because x != 0 is False, logical and short-circuits and never evaluates 10 / x, avoiding ZeroDivisionError.

[HI] x != 0 असत्य होने पर and ऑपरेटर शॉर्ट-सर्किट हो जाता है और 10 / x चलाए बिना सीधे else में जाकर Safe प्रिंट करता है।

Q8. Which statement acts as a valid empty placeholder inside an if block?

(if ब्लॉक को खाली रखने के लिए कौन-सा वैध प्लेसहोल्डर स्टेटमेंट उपयोग किया जाता है?)

(A) continue

(B) pass

(C) skip

(D) null

Correct Answer: (B)

[EN] pass is Python's null operation statement used whenever syntax requires a block body with no action.

[HI] जब सिंटैक्स के अनुसार खाली ब्लॉक की आवश्यकता हो, तब pass स्टेटमेंट उपयोग किया जाता है।

Q9. What is the relative precedence order of Python logical operators from highest to lowest?

(पायथन लॉजिकल ऑपरेटर्स की प्राथमिकता (precedence) उच्च से निम्न क्रम में क्या है?)

(A) and, then or, then not

(B) not, then and, then or

(C) All three have equal left-to-right precedence

(D) or, then and, then not

Correct Answer: (B)

[EN] In Python, not binds tightest, followed by and, with or having the lowest precedence among the three.

[HI] पायथन में not की प्राथमिकता सबसे अधिक, उसके बाद and, और सबसे कम or की होती है।

Q10. What happens if you write if x = 5: in Python?

(पायथन में if x = 5: लिखने पर क्या होता है?)

(A) Assigns 5 to x and enters the block

(B) Compares x with 5

(C) Raises TypeError

(D) Raises SyntaxError

Correct Answer: (D)

[EN] Assignment statement = is disallowed in conditions to prevent accidental typos for ==; use == or walrus :=.

[HI] शर्त में गलती से == की जगह = लिखने से रोकने के लिए पायथन if x = 5: पर SyntaxError देता है।

Q11. How many spaces per indentation level does PEP 8 officially recommend?

(PEP 8 आधिकारिक रूप से प्रति इंडेंटेशन स्तर कितने स्पेस की अनुशंसा करता है?)

(A) 8 spaces

(B) 1 tab character

(C) 2 spaces

(D) 4 spaces

Correct Answer: (D)

[EN] PEP 8 specifies 4 spaces per indentation level and discourages mixing tabs and spaces.

[HI] PEP 8 प्रति इंडेंटेशन स्तर 4 स्पेस की सिफारिश करता है।

Q12. What does mixing tabs and spaces across lines in an if-else block cause in Python 3?

(पायथन 3 में if-else ब्लॉक की पंक्तियों में टैब और स्पेस मिलाने पर क्या होता है?)

(A) Raises KeyError

(B) Raises TabError

(C) Ignored by interpreter

(D) Automatically converts tabs to 4 spaces

Correct Answer: (B)

[EN] Python 3 strictly forbids inconsistent mixing of tabs and spaces in indentation, raising TabError.

[HI] पायथन 3 इंडेंटेशन में टैब और स्पेस के मिश्रण को सख्ती से मना करता है और TabError देता है।

Q13. Which idiom is PEP 8 recommended when checking if variable x holds None?

(वेरिएबल x का मान None है या नहीं यह जाँचने का PEP 8 अनुशंसित तरीका कौन-सा है?)

(A) if x == None:

(B) if bool(x) == False:

(C) if x is None:

(D) if not x:

Correct Answer: (C)

[EN] None is a singleton object, so identity check x is None is faster and avoids custom __eq__ traps.

[HI] None एक सिंगलटन ऑब्जेक्ट है, इसलिए == या not x के बजाय आइडेंटिटी जाँच x is None सर्वश्रेष्ठ है।

Q14. In Python, what resolves which if statement an else clause belongs to in nested conditionals?

(नेस्टेड कंडीशनल्स में यह कैसे तय होता है कि कोई else किस if से संबंधित है?)

(A) Nearest preceding if regardless of spacing

(B) Indentation level

(C) Curly braces

(D) end if keyword

Correct Answer: (B)

[EN] Indentation aligns each else with its exact matching if header, eliminating dangling-else ambiguity.

[HI] पायथन में इंडेंटेशन का स्तर स्पष्ट रूप से तय करता है कि else किस if से जुड़ा है।

Q15. What is the output of: if False or True and False: print(1) else: print(2)?

(if False or True and False: print(1) else: print(2) का आउटपुट क्या होगा?)

(A) 1

(B) SyntaxError

(C) True

(D) 2

Correct Answer: (D)

[EN] Because and has higher precedence than or, False or (True and False) evaluates to False or False -> False, printing 2.

[HI] and की प्राथमिकता or से अधिक होने के कारण यह False or (True and False) बनता है, जो False होकर 2 प्रिंट करता है।

Q16. Which statement is NOT true about Python conditional statements?

(पायथन कंडीशनल स्टेटमेंट्स के बारे में कौन-सा कथन सत्य नहीं है?)

(A) Multiple elif clauses can follow one if

(B) A colon is required at the end of if, elif, and else headers

(C) An if statement must always have a matching else branch

(D) Conditions can test non-boolean objects via truthiness

Correct Answer: (C)

[EN] The else clause is completely optional in an if or if-elif block.

[HI] if या if-elif के साथ else ब्लॉक लगाना वैकल्पिक है, अनिवार्य नहीं।

Q17. Within Conditional Statements: if, elif, else in Python, which construct / value directly maps to: "Executes at most one branch"?

(Conditional Statements: if, elif, else in Python में कौन सा Construct / Value सीधे "Executes at most one branch" से संबंधित है?)

(A) 0, None, '', [], {}

(B) 10 <= x < 20

(C) if / elif / else

(D) Missing colon or indent

Correct Answer: (C)

[EN] In Conditional Statements: if, elif, else in Python, 'if / elif / else' handles 'Executes at most one branch' (Remaining elif blocks are skipped after match).

[HI] Conditional Statements: if, elif, else in Python में 'if / elif / else' का कार्य/विवरण 'Executes at most one branch' (Remaining elif blocks are skipped after match) है।

Q18. Which option accurately describes '0, None, '', [], {}' in Conditional Statements: if, elif, else in Python?

(Conditional Statements: if, elif, else in Python के संदर्भ में '0, None, '', [], {}' की सही विशेषता कौन सी है?)

(A) Executes at most one branch

(B) All evaluate as Falsy

(C) Raises SyntaxError / IndentationError

(D) Equivalent to 10 <= x and x < 20

Correct Answer: (B)

[EN] '0, None, '', [], {}' provides 'All evaluate as Falsy' — key point: Non-empty strings like 'False' are Truthy.

[HI] '0, None, '', [], {}' का मुख्य विवरण 'All evaluate as Falsy' है (Non-empty strings like 'False' are Truthy)।

Q19. In Conditional Statements: if, elif, else in Python, identify the correct parameter for the specification: "Equivalent to 10 <= x and x < 20".

(Conditional Statements: if, elif, else in Python में "Equivalent to 10 <= x and x < 20" विवरण किस विकल्प के लिए सही है?)

(A) 0, None, '', [], {}

(B) 10 <= x < 20

(C) Missing colon or indent

(D) if / elif / else

Correct Answer: (B)

[EN] '10 <= x < 20' corresponds to 'Equivalent to 10 <= x and x < 20' (x is evaluated only once in chained check).

[HI] '10 <= x < 20' का संबंध 'Equivalent to 10 <= x and x < 20' (x is evaluated only once in chained check) से है।

Q20. Considering Conditional Statements: if, elif, else in Python, what key behavior defines 'Missing colon or indent'?

(Conditional Statements: if, elif, else in Python में 'Missing colon or indent' की मुख्य भूमिका या मान क्या है?)

(A) Equivalent to 10 <= x and x < 20

(B) All evaluate as Falsy

(C) Executes at most one branch

(D) Raises SyntaxError / IndentationError

Correct Answer: (D)

[EN] 'Missing colon or indent' represents 'Raises SyntaxError / IndentationError' (PEP 8 specifies 4 spaces per indent level).

[HI] 'Missing colon or indent' का अर्थ/मान 'Raises SyntaxError / IndentationError' है (PEP 8 specifies 4 spaces per indent level)।

Conditional Statements: if, elif, else in Python — Exam FAQs

Q1: Why does Python evaluate if 'False': as True?

पायथन में if 'False': शर्त True क्यों मानी जाती है?

[EN] Because 'False' wrapped in quotes is a non-empty string of 5 characters, not the boolean literal False. Every non-empty string in Python is truthy.

[HI] क्योंकि कोट्स में लिखा 'False' बूलियन मान नहीं बल्कि 5 अक्षरों की एक गैर-खाली स्ट्रिंग है, और पायथन में हर गैर-खाली स्ट्रिंग ट्रुथी होती है।

Q2: What is the difference between using if x is None: and if not x:?

if x is None: और if not x: के बीच क्या अंतर है?

[EN] if x is None: triggers ONLY when x is literally None, whereas if not x: also triggers for 0, 0.0, False, '', [], and {}, which can cause subtle bugs when 0 is valid input.

[HI] if x is None: केवल None होने पर ही चलता है, जबकि if not x: शून्य (0), खाली स्ट्रिंग या खाली लिस्ट पर भी चल जाता है जिससे 0 के वैध होने पर बग आ सकता है।

Q3: Did Python add a switch-case alternative in newer versions?

क्या पायथन के नए संस्करणों में switch-case जैसा कोई विकल्प जोड़ा गया है?

[EN] Yes. Python 3.10 introduced structural pattern matching via match ... case (PEP 634), which goes beyond basic switch-case by supporting value, type, and sequence destructuring.

[HI] हाँ। पायथन 3.10 में स्ट्रक्चरल पैटर्न मैचिंग (match ... case, PEP 634) जोड़ा गया है जो मान, टाइप और पैटर्न दोनों की मैचिंग करता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)