Indentation
In one line
Indentation is the leading whitespace before a line of code. In Python, it defines code blocks instead of braces.
In simple words
In Python, indentation is not just for readability — it is syntactically significant. The number of spaces at the start of a line determines which block of code the line belongs to. This is how Python groups statements, instead of using curly braces like JavaScript or C.
Consistent indentation is mandatory. Mixing tabs and spaces causes errors. The convention is four spaces per level (PEP 8). A line inside an if, for, or def must be indented one level deeper than the statement that introduces the block.
Indentation makes Python code visually clean and forces a consistent style. The downside is that a single misplaced space can change the meaning of your code or cause an IndentationError. Most editors handle this automatically, but it is important to understand.
Example
age = 20
if age >= 18:
print("Adult") # indented — inside the if
print("Can vote") # indented — inside the if
print("Done") # not indented — outside the if
# Wrong indentation causes an error:
# if age >= 18:
# print("Adult") # IndentationError: expected an indented blockLines indented under if belong to the if-block. The unindented line runs regardless. Wrong indentation is a syntax error.
Common confusions
Confused with: braces
The difference: Python uses indentation to define code blocks; most other languages (JavaScript, Java, C) use curly braces {}. Indentation is mandatory and semantic in Python, while braces are mandatory and indentation is optional (style only) in those languages.
Related terms
Related Resources
Related Lessons
- Conditions and Loops
Indentation defines blocks in conditions and loops
Learn the fundamentals
Deepen your understanding with structured lessons on this topic.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.