PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Indentation: Whitespace Rules and Syntax Errors (पायथन इंडेंटेशन (Indentation): कोड ब्लॉक्स और सिंटैक्स नियम) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python uses leading whitespace (indentation) rather than curly braces `{}` to delimit code blocks under headers ending in a colon (`:`) such as `if`, `for`, `def`, and `class`. PEP 8 recommends 4 spaces per level, and inconsistent spacing or mixing tabs with spaces raises `IndentationError` or `TabError` (both subclasses of `SyntaxError`).
अध्याय सार: पायथन कर्ली ब्रेसेज़ `{}` के बजाय शुरुआती व्हाइटस्पेस (इंडेंटेशन) का उपयोग करके `if`, `for`, `def`, `class` जैसे कोलन (`:`) से समाप्त होने वाले कोड ब्लॉक्स की सीमा तय करता है। PEP 8 प्रति स्तर 4 स्पेस की सलाह देता है; असंगत स्पेसिंग या टैब-स्पेस मिलाने पर `IndentationError` या `TabError` (जो `SyntaxError` की उप-कक्षाएँ हैं) उत्पन्न होता है।
Python Indentation: Whitespace Rules and Syntax Errors: Detailed Notes & Architecture (पायथन इंडेंटेशन (Indentation): कोड ब्लॉक्स और सिंटैक्स नियम)
1. Significant Whitespace and the Off-Side Rule (महत्वपूर्ण व्हाइटस्पेस और कोड ब्लॉक नियम)
[EN] Unlike C, C++, and Java which use `{ }` braces to group statements, Python follows the off-side rule where a suite (block) begins after a colon (`:`) and continues while statements maintain equal or deeper leading indentation. When indentation returns to an outer column (dedent), the inner block automatically terminates. Because formatting directly defines execution scope, visually misleading dangling-else bugs from brace languages are structurally impossible in Python.
[HI] C, C++ और Java के `{ }` ब्रेसेज़ के विपरीत, पायथन में कोलन (`:`) के बाद आने वाला प्रत्येक कोड ब्लॉक शुरुआती स्पेस (इंडेंटेशन) की गहराई से पहचाना जाता है। एक ही ब्लॉक के सभी स्टेटमेंट का बायाँ इंडेंटेशन बिल्कुल समान होना चाहिए, और इंडेंटेशन बाहर लौटने (dedent) पर ब्लॉक स्वतः समाप्त हो जाता है। यह नियम कोड की पठनीयता को अनिवार्य बनाता है और ब्रेसेज़ छूटने जैसी सिंटैक्स गलतियों से बचाता है।
2. PEP 8 Standards and Valid Space Counts (PEP 8 मानक और स्पेस संख्या के नियम)
[EN] Python's official style guide (**PEP 8**) strongly recommends using **4 spaces per indentation level** and avoiding tab characters in new projects. Syntactically, Python accepts any uniform positive number of spaces per block (even 1 space, 2 spaces, or 8 spaces), provided every line within that block uses the exact same count. Using 0 spaces where a block is expected after `:` triggers `IndentationError: expected an indented block`, whereas trailing/blank lines are ignored.
[HI] पायथन की आधिकारिक स्टाइल गाइड **PEP 8** प्रति इंडेंटेशन स्तर **4 स्पेस** का उपयोग करने और टैब से बचने की अनुशंसा करती है। सिंटैक्स की दृष्टि से एक ब्लॉक में 1, 2, 4 या कोई भी निश्चित धनात्मक स्पेस संख्या मान्य है, बशर्ते उस ब्लॉक की सभी लाइनें ठीक उतनी ही स्पेस से शुरू हों। कोलन (`:`) के बाद 0 स्पेस देने पर `IndentationError: expected an indented block` आता है।
3. IndentationError and TabError Hierarchy (IndentationError और TabError अपवाद पदानुक्रम)
[EN] Both `IndentationError` and `TabError` are compile-time parsing exceptions: specifically, `TabError` is a direct subclass of `IndentationError`, and `IndentationError` is a subclass of `SyntaxError`. An `IndentationError` occurs on missing block indents, unexpected top-level leading whitespace (`unexpected indent`), or dedents that fail to align with any enclosing block level (`unindent does not match any outer indentation level`). In Python 3, mixing tabs and spaces inconsistently in the same block raises `TabError: inconsistent use of tabs and spaces in indentation`.
[HI] `IndentationError` और `TabError` दोनों पार्सिंग के समय आने वाले एरर हैं: `TabError`, `IndentationError` की उप-क्लास है और `IndentationError`, `SyntaxError` की उप-क्लास है। गलत जगह पर अतिरिक्त स्पेस देने (`unexpected indent`), ब्लॉक न बनाने, या बाहर आते समय पिछले स्तर से मेल न खाने (`unindent does not match...`) पर `IndentationError` आता है। पायथन 3 में एक ही ब्लॉक में टैब और स्पेस मिलाने पर `TabError` उत्पन्न होता है।
4. Empty Blocks (pass), Line Continuation, and Suites (खाली ब्लॉक (pass), लाइन कंटीन्यूएशन और विशेष नियम)
[EN] Because Python forbids completely empty indented blocks after `:`, placeholder definitions (`def todo():`, `class Empty:`, `if cond:`) require the `pass` statement (or `...` / docstring) as an indented body. Inside implicit line continuation delimiters—parentheses `()`, square brackets `[]`, or curly braces `{}`—Python suspends indentation rules across line breaks, allowing clean multi-line expressions. Explicit continuation outside brackets uses a trailing backslash (`\`), and simple single-line suites like `if ok: run()` are valid though discouraged for multi-statement blocks.
[HI] चूँकि पायथन में `:` के बाद खाली ब्लॉक मान्य नहीं है, इसलिए भविष्य में लिखे जाने वाले फंक्शन या क्लास में प्लेसहोल्डर के रूप में इंडेंटेड `pass` स्टेटमेंट (या `...`) लिखना जरूरी होता है। कोष्ठक `()`, `[]`, `{}` के अंदर मल्टी-लाइन एक्सप्रेशन लिखते समय इंडेंटेशन नियम लागू नहीं होते (इम्प्लिसिट लाइन कंटीन्यूएशन)। कोष्ठक के बाहर लाइन जारी रखने के लिए अंत में बैकस्लैश (`\`) लगाया जाता है, तथा `if ok: run()` जैसा वन-लाइन स्टेटमेंट भी मान्य है।
Syntax, Formula & Technical Example (Python Indentation: Whitespace Rules and Syntax Errors)
def classify_num(n):
if n > 0:
result = "Positive"
elif n == 0:
pass # Empty suite requires pass
else:
result = "Negative"
return result if n != 0 else "Zero"
print(classify_num(5))
Python Indentation: Whitespace Rules and Syntax Errors Quick Reference & Comparison Table
| Indentation Scenario | Python 3 Outcome | Exam Explanation |
|---|---|---|
| 4 spaces per level | PEP 8 compliant valid block | Official recommended indentation width |
| Mixed tabs & spaces in block | Raises TabError | Subclass of IndentationError (SyntaxError) |
| 0 spaces after if/def colon | Raises IndentationError | Expected an indented block error |
| Multi-line inside () [] {} | Valid implicit continuation | Indentation ignored inside brackets |
Python Indentation: Whitespace Rules and Syntax Errors Top 20 Bilingual MCQs with Answers
Q1. How does Python determine which statements belong inside an `if`, `for`, `def`, or `class` block?
(पायथन यह कैसे तय करता है कि कौन-से स्टेटमेंट `if`, `for`, `def` या `class` ब्लॉक के अंदर आते हैं?)
(A) By `begin` and `end` keywords
(B) By consistent leading whitespace (indentation) following a colon (`:`)
(C) By semicolons `;` at the end of every line
(D) By matching curly braces `{` and `}`
Correct Answer: (B)
[EN] Python groups statements into suites using uniform leading indentation depth after a `:` header.
[HI] पायथन कोलन (`:`) के बाद एक समान शुरुआती व्हाइटस्पेस (इंडेंटेशन) के आधार पर कोड ब्लॉक्स की पहचान करता है।
Q2. How many spaces per indentation level does PEP 8 (the official Python Style Guide) recommend?
(आधिकारिक पायथन स्टाइल गाइड PEP 8 प्रति इंडेंटेशन स्तर कितने स्पेस उपयोग करने की अनुशंसा करता है?)
(A) 8 spaces
(B) 1 tab character
(C) 4 spaces
(D) 2 spaces
Correct Answer: (C)
[EN] PEP 8 specifies 4 spaces per indentation level and discourages tabs in Python 3.
[HI] PEP 8 के अनुसार प्रति इंडेंटेशन स्तर 4 स्पेस उपयोग किए जाने चाहिए।
Q3. In Python's exception hierarchy, what is `IndentationError` a direct subclass of?
(पायथन के एक्सेप्शन पदानुक्रम में `IndentationError` किसकी सीधी उप-क्लास (subclass) है?)
(A) `NameError`
(B) `SyntaxError`
(C) `RuntimeError`
(D) `ValueError`
Correct Answer: (B)
[EN] `IndentationError` inherits from `SyntaxError` and is caught during source compilation before execution begins.
[HI] `IndentationError` सीधे `SyntaxError` की उप-क्लास है (और `TabError`, `IndentationError` की उप-क्लास है)।
Q4. Which specific exception is raised in Python 3 when tabs and spaces are mixed inconsistently in indentation?
(पायथन 3 में इंडेंटेशन के लिए टैब और स्पेस को असंगत रूप से मिलाने पर कौन-सा विशिष्ट एरर आता है?)
(A) `SystemError`
(B) `TabError`
(C) `TypeError`
(D) `SpaceError`
Correct Answer: (B)
[EN] Python 3 forbids mixing tabs and spaces in the same file/block indentation and raises `TabError`.
[HI] पायथन 3 में एक ही ब्लॉक में टैब और स्पेस मिलाने पर `TabError` उत्पन्न होता है।
Q5. What happens if a Python script starts its very first line with a single leading space (` x = 10`)?
(यदि किसी पायथन स्क्रिप्ट की पहली लाइन ही एक अतिरिक्त स्पेस से शुरू हो (` x = 10`), तो क्या होगा?)
(A) `TabError`
(B) `NameError`
(C) It executes normally as `x = 10`
(D) `IndentationError: unexpected indent`
Correct Answer: (D)
[EN] Top-level statements must start at column 0; leading whitespace without a preceding `:` header causes `unexpected indent`.
[HI] टॉप-लेवल स्टेटमेंट हमेशा कॉलम 0 से शुरू होने चाहिए; बिना `:` के शुरुआती स्पेस देने पर `IndentationError: unexpected indent` आता है।
Q6. Is a Python block using 2 spaces (consistently across all lines of that block) syntactically valid?
(क्या किसी ब्लॉक की सभी लाइनों में समान रूप से 2 स्पेस का इंडेंटेशन उपयोग करना सिंटैक्स की दृष्टि से मान्य है?)
(A) No, Python strictly rejects anything other than 4 spaces
(B) Only inside `while` loops
(C) Yes, any positive consistent space count per block is valid (though PEP 8 prefers 4)
(D) No, minimum allowed is 4 spaces
Correct Answer: (C)
[EN] Python's parser only requires consistency within each block (minimum 1 space), even though PEP 8 recommends 4.
[HI] हाँ, सिंटैक्स के लिए ब्लॉक के भीतर समान स्पेस संख्या (न्यूनतम 1) होना काफी है, यद्यपि PEP 8 में 4 स्पेस अनुशंसित हैं।
Q7. Which statement is specifically used to create a syntactically valid empty indented block (e.g., an empty function or class)?
(खाली फंक्शन या क्लास जैसे सिंटैक्स-मान्य रिक्त इंडेंटेड ब्लॉक बनाने के लिए किस स्टेटमेंट का उपयोग होता है?)
(A) `void`
(B) `continue`
(C) `pass`
(D) `skip`
Correct Answer: (C)
[EN] `pass` is a null operation statement serving as a syntactically required indented body for empty blocks.
[HI] `pass` एक नल स्टेटमेंट है जो `:` के बाद खाली ब्लॉक को सिंटैक्स रूप से वैध बनाने के लिए उपयोग होता है।
Q8. What error does the following code produce? `if True: x = 1 y = 2`
(कोड `if True:` के बाद `x = 1` (4 स्पेस) और फिर `y = 2` (2 स्पेस) लिखने पर कौन-सा एरर आएगा?)
(A) No error; `y = 2` runs outside the `if` block
(B) `ValueError`
(C) `IndentationError: unindent does not match any outer indentation level`
(D) `TabError`
Correct Answer: (C)
[EN] Dedenting from 4 spaces to 2 spaces does not match column 0 (the outer level), raising `IndentationError`.
[HI] 4 स्पेस से घटकर 2 स्पेस पर आने पर वह बाहरी कॉलम 0 से मेल नहीं खाता, इसलिए `IndentationError: unindent does not match...` आता है।
Q9. When can indentation rules be safely ignored across multiple lines in Python?
(पायथन में कई लाइनों पर लिखे कोड के दौरान इंडेंटेशन नियम कब लागू नहीं होते?)
(A) Whenever a line ends with a semicolon `;`
(B) Inside every `for` loop body
(C) Inside `try...except` blocks
(D) Inside implicit line continuation delimiters: `()`, `[]`, or `{}`
Correct Answer: (D)
[EN] Inside parentheses, square brackets, or curly braces, expressions can wrap across lines with arbitrary alignment.
[HI] कोष्ठक `()`, `[]` या `{}` के भीतर इम्प्लिसिट लाइन कंटीन्यूएशन के कारण किसी भी तरह के एलाइनमेंट की अनुमति होती है।
Q10. Can an `IndentationError` inside an unexecuted `else` branch be caught at runtime using `try...except IndentationError:` in the same file?
(क्या एक ही फाइल में चल रहे कोड द्वारा `try...except IndentationError:` लिखकर रन-टाइम पर `IndentationError` को पकड़ा जा सकता है?)
(A) Yes, in Python 3.10+
(B) Only when using `finally`
(C) Yes, if `try` wraps the `else` branch
(D) No, because `IndentationError` is raised during compile/parse time before the script runs
Correct Answer: (D)
[EN] The entire module is parsed into bytecode before execution starts, so syntax/indentation errors abort compilation immediately (unless parsing external strings via `exec()`/`eval()`/`import`).
[HI] नहीं, क्योंकि पूरा प्रोग्राम चलने से पहले ही कंपाइल/पार्स होता है; पार्सिंग के दौरान ही `IndentationError` प्रोग्राम को रोक देता है (जब तक `exec`/`import` से दूसरी फाइल न चले)।
Q11. What happens if two sibling `if` blocks in the SAME file use different indentation widths: the first `if` uses 2 spaces and the second top-level `if` uses 4 spaces?
(यदि एक ही फाइल में पहला टॉप-लेवल `if` ब्लॉक 2 स्पेस और दूसरा स्वतंत्र टॉप-लेवल `if` ब्लॉक 4 स्पेस उपयोग करे, तो क्या होगा?)
(A) `IndentationError`
(B) `TabError`
(C) `SyntaxError` on the second `if`
(D) It runs without any error
Correct Answer: (D)
[EN] Indentation width only has to remain consistent within each individual block and its nested levels, not globally across unrelated top-level blocks.
[HI] यह बिना एरर चलेगा क्योंकि इंडेंटेशन की समानता केवल एक ही ब्लॉक के भीतर आवश्यक होती है, अलग-अलग टॉप-लेवल ब्लॉक्स में नहीं।
Q12. Is a single-line suite like `if x > 0: print(x)` syntactically valid in Python?
(क्या पायथन में `if x > 0: print(x)` जैसा सिंगल-लाइन स्टेटमेंट सिंटैक्स की दृष्टि से मान्य है?)
(A) Yes, simple statements can follow a colon on the same line
(B) Only inside `lambda` functions
(C) No, a newline and 4 spaces are always mandatory after `:`
(D) Only in Python 2
Correct Answer: (A)
[EN] A simple statement (not a compound statement like another `if`/`for`) can appear on the same line after `:`.
[HI] हाँ, `:` के बाद उसी लाइन पर एक सिंपल स्टेटमेंट लिखा जा सकता है, यद्यपि PEP 8 मल्टी-लाइन को बेहतर मानता है।
Q13. What happens if a comment line `# comment` inside an `if` block starts at column 0 instead of 4 spaces?
(यदि `if` ब्लॉक के बीच में कोई कमेंट लाइन `# comment` 4 स्पेस के बजाय कॉलम 0 से शुरू हो जाए तो क्या होगा?)
(A) Nothing fails; comments and blank lines are ignored by indentation checking
(B) It prematurely closes the `if` block
(C) `TabError`
(D) `IndentationError`
Correct Answer: (A)
[EN] Python's lexer ignores blank lines and pure comment lines when emitting `INDENT`/`DEDENT` tokens.
[HI] कुछ नहीं बिगड़ता; पायथन के लेक्सर द्वारा खाली लाइनें और कमेंट लाइनें `INDENT`/`DEDENT` जाँच में अनदेखी की जाती हैं।
Q14. Which Python command-line flag in legacy Python 2 raised an error on mixed tabs and spaces (behavior now always default in Python 3)?
(पायथन 2 में कौन-सा कमांड-लाइन फ्लैग टैब और स्पेस मिलाने पर एरर देता था (जो अब पायथन 3 में हमेशा डिफ़ॉल्ट है)?)
(A) `-u`
(B) `-tt`
(C) `-O`
(D) `-B`
Correct Answer: (B)
[EN] In Python 2, `-t` issued warnings and `-tt` issued errors for inconsistent tabs/spaces; Python 3 enforces `-tt` strictly by default.
[HI] पायथन 2 में `-tt` फ्लैग टैब-स्पेस मिक्सिंग को एरर बनाता था, जिसे पायथन 3 में डिफ़ॉल्ट बना दिया गया है।
Q15. How many spaces is a single tab (`\t`) treated as in Python's indentation column expansion algorithm?
(पायथन के लेक्सर एल्गोरिदम में एक टैब (`\t`) को कितने स्पेस के बराबर कॉलम पर विस्तारित किया जाता था?)
(A) 16 spaces
(B) Up to 8 spaces (nearest multiple of 8)
(C) Exactly 4 spaces always
(D) Exactly 2 spaces always
Correct Answer: (B)
[EN] Python's tokenizer expands tabs to the next multiple of 8 columns (though mixing tabs and spaces in Python 3 triggers `TabError`).
[HI] पायथन का टोकनाइजर टैब को 8 के अगले गुणज (8 स्पेस कॉलम) तक गिनता है, पर पायथन 3 में टैब-स्पेस मिक्स करने पर `TabError` आता है।
Q16. Which statement about Python indentation is NOT true?
(पायथन इंडेंटेशन के संबंध में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) Missing indentation after `def f():` raises `IndentationError`
(B) `TabError` is a subclass of `IndentationError`
(C) Parentheses `()` allow multi-line wrapping without strict indentation
(D) Python allows curly braces `{}` via `from __future__ import braces` to avoid indentation
Correct Answer: (D)
[EN] `from __future__ import braces` is a famous Python Easter egg that intentionally raises `SyntaxError: not a chance`.
[HI] `from __future__ import braces` पायथन का एक प्रसिद्ध ईस्टर एग है जो `SyntaxError: not a chance` देता है; पायथन में `{}` से ब्लॉक नहीं बनते।
Q17. Within Python Indentation: Whitespace Rules and Syntax Errors, which indentation scenario directly maps to: "PEP 8 compliant valid block"?
(Python Indentation: Whitespace Rules and Syntax Errors में कौन सा Indentation Scenario सीधे "PEP 8 compliant valid block" से संबंधित है?)
(A) 0 spaces after if/def colon
(B) Mixed tabs & spaces in block
(C) Multi-line inside () [] {}
(D) 4 spaces per level
Correct Answer: (D)
[EN] In Python Indentation: Whitespace Rules and Syntax Errors, '4 spaces per level' handles 'PEP 8 compliant valid block' (Official recommended indentation width).
[HI] Python Indentation: Whitespace Rules and Syntax Errors में '4 spaces per level' का कार्य/विवरण 'PEP 8 compliant valid block' (Official recommended indentation width) है।
Q18. Which option accurately describes 'Mixed tabs & spaces in block' in Python Indentation: Whitespace Rules and Syntax Errors?
(Python Indentation: Whitespace Rules and Syntax Errors के संदर्भ में 'Mixed tabs & spaces in block' की सही विशेषता कौन सी है?)
(A) Raises IndentationError
(B) Raises TabError
(C) Valid implicit continuation
(D) PEP 8 compliant valid block
Correct Answer: (B)
[EN] 'Mixed tabs & spaces in block' provides 'Raises TabError' — key point: Subclass of IndentationError (SyntaxError).
[HI] 'Mixed tabs & spaces in block' का मुख्य विवरण 'Raises TabError' है (Subclass of IndentationError (SyntaxError))।
Q19. In Python Indentation: Whitespace Rules and Syntax Errors, identify the correct parameter for the specification: "Raises IndentationError".
(Python Indentation: Whitespace Rules and Syntax Errors में "Raises IndentationError" विवरण किस विकल्प के लिए सही है?)
(A) 0 spaces after if/def colon
(B) 4 spaces per level
(C) Mixed tabs & spaces in block
(D) Multi-line inside () [] {}
Correct Answer: (A)
[EN] '0 spaces after if/def colon' corresponds to 'Raises IndentationError' (Expected an indented block error).
[HI] '0 spaces after if/def colon' का संबंध 'Raises IndentationError' (Expected an indented block error) से है।
Q20. Considering Python Indentation: Whitespace Rules and Syntax Errors, what key behavior defines 'Multi-line inside () [] {}'?
(Python Indentation: Whitespace Rules and Syntax Errors में 'Multi-line inside () [] {}' की मुख्य भूमिका या मान क्या है?)
(A) Valid implicit continuation
(B) Raises IndentationError
(C) PEP 8 compliant valid block
(D) Raises TabError
Correct Answer: (A)
[EN] 'Multi-line inside () [] {}' represents 'Valid implicit continuation' (Indentation ignored inside brackets).
[HI] 'Multi-line inside () [] {}' का अर्थ/मान 'Valid implicit continuation' है (Indentation ignored inside brackets)।
Python Indentation: Whitespace Rules and Syntax Errors — Exam FAQs
Q1: Why did Guido van Rossum choose indentation instead of curly braces `{}`?
गुइडो वैन रोसुम ने कर्ली ब्रेसेज़ `{}` के बजाय इंडेंटेशन को क्यों चुना?
[EN] In C/Java, programmers already indent code for humans while the compiler reads braces; Guido merged the two so visual structure and program logic can never disagree, reducing line noise and boilerplate.
[HI] C/Java में प्रोग्रामर पहले से पठनीयता के लिए इंडेंटेशन करते हैं; इसे ही सिंटैक्स नियम बना देने से कोड अधिक साफ रहता है और ब्रेसेज़-इंडेंटेशन में विरोधाभास नहीं होता।
Q2: Can I use `from __future__ import braces` in Python?
क्या पायथन में `from __future__ import braces` लिखकर `{}` ब्लॉक इस्तेमाल किए जा सकते हैं?
[EN] No! That import is a deliberate joke/Easter egg built into CPython that always raises `SyntaxError: not a chance`.
[HI] नहीं! यह CPython में एक मज़ाक (Easter egg) है जिसे चलाने पर हमेशा `SyntaxError: not a chance` एरर आता है।
Q3: How do I quickly fix `TabError` in an existing Python file?
किसी मौजूदा पायथन फाइल में `TabError` को जल्दी कैसे ठीक करें?
[EN] Configure your editor to convert tabs to 4 spaces ('Expand Tabs' / 'Convert Indentation to Spaces') or run `python -m tabnanny script.py` to detect ambiguous tab/space lines.
[HI] अपने एडिटर में 'Convert Indentation to Spaces (4 spaces)' सेटिंग चलाएँ या `python -m tabnanny script.py` से गलत लाइनों का पता लगाएँ।
Post a Comment
Post a Comment