Guide Python Beginner

4.4 Indentation

Why indentation is part of Python's syntax rather than a style choice, the indentation rules, nested blocks, and the most common IndentationError.

3 min read

1.1 What Is Indentation?

Indentation means spaces at the beginning of a line. Python uses indentation to show which lines belong together.

In many languages, braces mark a block. Python uses indentation instead:

if temperature > 30:
    print("It is hot")

The indented print() line belongs to the if statement.

1.2 The Basic Rule

When a line ends with a colon, the next related lines must be indented:

if server_status == "running":
    print("Server is running")

Statements at the same level should have the same indentation:

if server_status == "running":
    print("Server is running")
    print("Health check passed")

1.3 Use Four Spaces

The usual Python style is four spaces for each indentation level:

if True:
    print("Level one")

Most code editors insert four spaces when you press Tab. Configure your editor to use spaces instead of tabs for Python files.

1.4 Indentation in Functions and Loops

Indentation is used with functions, loops, conditions, and other blocks:

def check_servers(servers):
    for server in servers:
        print(f"Checking {server}")

The indentation shows the relationship:

def check_servers        level 0
    for server           level 1
        print(...)       level 2

1.5 Nested Blocks

A nested block is a block inside another block. Add one indentation level for each new block:

servers = ["web-01", "web-02"]

for server in servers:
    if server == "web-01":
        print(f"Checking important server: {server}")
    else:
        print(f"Checking server: {server}")

The if statement is inside the for loop, so it has more indentation.

flowchart TD A[for loop] --> B[if condition] B --> C[Indented action]

1.6 Incorrect Indentation

This code causes an error because the line after the colon is not indented:

if True:
print("This is not inside the if block")

Python reports an IndentationError.

Correct it by adding four spaces:

if True:
    print("This is inside the if block")

1.7 Inconsistent Indentation

Lines in the same block must use the same indentation:

if True:
    print("First line")
  print("Second line")

The second line does not line up with the first line, so Python may report an IndentationError.

1.8 Do Not Mix Tabs and Spaces

Mixing tabs and spaces can cause a TabError or make the code difficult to read.

Use your editor’s Python formatting settings to insert spaces. In VS Code, you can use the indentation indicator in the bottom status bar to convert indentation to spaces.

1.9 Indentation with else

The else statement must line up with its matching if:

disk_usage = 70

if disk_usage > 80:
    print("Disk usage is high")
else:
    print("Disk usage is normal")

The code inside each branch is indented, but if and else are at the same level.

1.10 Practice Exercise

Create a file named indentation-practice.py:

services = ["api", "database", "cache"]

for service in services:
    if service == "database":
        print(f"Checking critical service: {service}")
    else:
        print(f"Checking service: {service}")

Change the indentation intentionally and observe the error. Then fix the indentation and run the program again.

Interview Questions

  • Why is indentation important in Python?
  • How many spaces are normally used for one indentation level?
  • What happens when indentation is missing after a colon?
  • What is a nested block?
  • Why should tabs and spaces not be mixed?
  • How should else align with if?

Quick Interview Answer

Python uses indentation to define blocks of code instead of braces. Lines in the same block must have the same indentation, and a new block normally uses four spaces. Indentation is required after statements such as if, for, and def, and mixing tabs and spaces can cause an error.

Key Takeaways

  • Indentation is part of Python syntax.
  • Use four spaces for each indentation level.
  • Indent code after a line ending with :.
  • Keep statements in the same block aligned.
  • Use more indentation for nested blocks.
  • Do not mix tabs and spaces.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form