What is Python's indentation rule?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Python development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Python uses indentation (whitespace at the beginning of lines) to define code blocks, instead of curly braces ({}) used in most other languages. All statements in a block must use the same indentation level. PEP 8 (Python's style guide) recommends 4 spaces per indentation level — never mix tabs and spaces (Python 3 raises an error for mixed indentation). The colon (:) at the end of compound statements (if, for, while, def, class) signals that a new indented block follows. While this forces consistent formatting and improves readability, incorrect indentation is a common source of IndentationError and TabError exceptions.
Pro Tip
This topic has Python-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is the difference between a list and a dictionary?
Next
What are Python functions and how do you define them?