🐍 Python Beginner

What is Python's indentation rule?

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.