Definition
The use of whitespace (indentation) to group statements together into a single logical unit of execution, called a block or clause.
Why It Matters
Enforced indentation transforms readability from a stylistic preference into a functional necessity, drastically reducing the ‘debugging tax’ and making large-scale collaborative software maintainable.
Core Concepts
- The Three Rules of Blocks:
- Blocks begin when the indentation increases.
- Blocks can contain other blocks.
- Blocks end when the indentation decreases to zero or to a containing block’s indentation level.
- The Clause: The combination of a control statement (like
iforwhile) and its associated block. - Syntactic Significance: Unlike many languages that use curly braces
{}, Python uses indentation to define scope. This enforces readable, consistent code.
# Example of indentation defining blocks
if True:
print("This is inside the 'if' block.")
if True:
print("This is nested inside another block.")
print("This is back in the main scope.")