Courses/Python Mastery/Module 2: The Building Blocks (Tokens & Syntax)
Module 2 · Lesson 515 minBeginner

Indentation — Python's Famous Whitespace Rule

Lesson goal
Why Python uses indentation instead of braces, the IndentationError, and how to never fear it again.

Indentation — Python's Famous Whitespace Rule

In most languages, indentation is decoration. In Python, indentation is the grammar. This single design decision is responsible for both Python's beautiful readability *and* the error every beginner meets in week one. Ten minutes here saves you a hundred frustrated searches.

The idea: structure through spacing

Other languages mark "inside the if" with braces:

code
# C-style languages (NOT Python)
if (age >= 18) {
    print("You can vote");
    print("Bring your ID");
}

Python uses indentation instead:

code
# Python
if age >= 18:
    print("You can vote")
    print("Bring your ID")

The colon (:) announces "a block is starting." The indented lines are the block. When indentation returns to the original level, the block ends. No braces, no end keyword — the shape of the code *is* the logic.

What indentation actually controls

Watch the difference indentation makes:

code
age = 20

if age >= 18:
    print("You can vote")       # INSIDE the if — only runs when True
print("Welcome!")               # OUTSIDE the if — always runs

Output (age is 20):

code
You can vote
Welcome!

Now change one line's indentation:

code
age = 15

if age >= 18:
    print("You can vote")
    print("Welcome!")           # now INSIDE the if

Output (age is 15):

code
(nothing — both prints are inside the if, which was False)

Same code, one space of difference, completely different behavior. Indentation isn't styling — it's meaning.

The rules (there are only three)

  • Use 4 spaces per level — the official Python standard (PEP 8). VS Code does this automatically when you press Tab.
  • Be consistent — never mix tabs and spaces in the same file. This causes the sneakiest errors in Python (modern Python versions refuse to run such files, thankfully).
  • Every indented block follows a colonif, for, while, def, class, with all end their line with :.
  • The errors you'll meet (and how to read them)

    IndentationError: expected an indented block

    code
    if age >= 18:
    print("You can vote")      # ← needs 4 spaces

    Python expected an indented block after the colon and found none. Fix: indent the line.

    IndentationError: unexpected indent

    code
    print("hello")
        print("world")     # ← why is this indented? Nothing opened a block

    A line is indented with no reason. Fix: remove the indentation.

    IndentationError: unindent does not match any outer indentation level

    Two blocks ended at different indent levels. Usually a mix of tab and spaces, or 3 spaces in one place and 4 in another. Fix: re-indent the block uniformly (select it in VS Code, Shift+Tab to dedent, Tab to indent).

    Let VS Code do the work

  • Pressing Enter after a : auto-indents the next line
  • Tab indents selected lines; Shift+Tab dedents them
  • View → Appearance → Render Whitespace shows exactly what's in your file when things get confusing

  • ✅ Checkpoint

  • What does a colon at the end of a line mean? *(An indented block is starting)*
  • How many spaces per level is the standard? *(4)*
  • In the two-version example, what changed the output? *(One print moved from outside the if to inside — indentation is logic)*
  • What usually causes "unindent does not match any outer indentation level"? *(Mixing tabs with spaces, or inconsistent space counts)*
  • Next: virtual environments — the last building block before variables, and the habit that separates careful coders from chaotic ones.