Module 2 · Python Syntax and Fundamentals — topic 2 of 14
Indentation
Indentation is the leading whitespace that tells Python which statements belong together. Learn the four-space rule, how blocks nest and how to fix indentation errors.
Learning objectives
- Explain why indentation is part of Python’s grammar
- Indent blocks consistently with four spaces
- Fix IndentationError and unexpected indent messages
Definition
Indentation is the leading whitespace that tells Python which statements belong together. Learn the four-space rule, how blocks nest and how to fix indentation errors.
Introduction
Indentation means the spaces at the start of a line. In many languages it is only decoration. In Python it decides which statements belong to which block, so it changes what the program does.
This forces every Python program to be laid out tidily, which is one reason Python code is easy to read.
Why it matters
Getting indentation right is essential: a single line indented one level too far can move it inside a loop and make it run a hundred times instead of once. Reading indentation correctly is how you see the structure of a program at a glance.
Detailed explanation
A block is a group of statements that run together, such as the body of an if or a loop. All statements in a block must be indented by exactly the same amount.
The standard indentation is four spaces per level. Editors insert four spaces when you press Tab in a .py file.
Blocks can be nested: a loop inside a function inside an if. Each level adds four more spaces.
A block ends when a line returns to the previous indentation level. There is no closing keyword or brace.
Never mix tabs and spaces
A tab and four spaces can look identical but are different characters. Mixing them raises TabError. Let your editor convert tabs to spaces.
Syntax
- statement_a and statement_c are in the outer block (4 spaces).
- statement_b is in the inner block (8 spaces).
- statement_d is outside both blocks and always runs.
Code examples
Inside and outside a block
Hot day Drink water Check finished
The same code with a false condition
Check finished
Both indented lines are skipped together because they form one block.
More examples
Nested blocks
120 pays delivery Empty order skipped 560 qualifies for free delivery All orders checked
Step-by-step explanation
- The for body (4 spaces) runs once per amount.
- Inside it, if/else chooses between an empty order and a real one.
- The real-order branch contains another if/else at 12 spaces.
- The last print returns to column 0, so it runs once after the loop.
Expected output
Running “Nested blocks” prints exactly:
Practical applications
- Code reviewers spot logic bugs by reading indentation, for example a save call accidentally inside a loop.
- Style checkers flag lines indented with tabs or with three spaces.
- YAML configuration files used with Python tools also rely on indentation, so the habit transfers.
Best practices
- Use four spaces per level and configure your editor to insert spaces.
- Keep nesting shallow; more than three levels is usually a sign to split the code into functions.
- Turn on visible whitespace in your editor when hunting indentation errors.
Common mistakes
Unexpected indent
Avoid
Prefer
A line can only be indented when it is inside a block. Here there is no block, so Python raises IndentationError: unexpected indent.
Accidentally ending a block early
Avoid
Prefer
Moving a line left takes it out of the block; it then runs only once.
Key takeaways
- Indentation defines blocks in Python.
- Use four spaces per level, never mixed with tabs.
- A block ends when indentation returns to the previous level.
Try it yourself
- 1Predict the output of the nested example if orders were [600, 50]. Then run it.
- 2Write an if statement with two indented lines and one line after it that always runs.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat happens when this code runs?