Python Indentation

No curly braces โ€” Python uses whitespace to define blocks, and it's not optional. Here's how it works and the three ways it breaks.

Overview

In many programming languages (like C, Java, JavaScript), blocks of code are grouped using curly braces {}. Python does not use curly braces โ€” instead, it uses indentation (whitespace at the start of a line) to define which statements belong to which block (if, else, for, while, functions, etc.).

This means indentation in Python is not just a style choice โ€” it is syntactically required. Getting it wrong causes an IndentationError.

Key Concepts

Detailed Explanation

1. The Non-Python Style (For Comparison)

Languages like C/Java use braces to mark blocks, so indentation there is just for readability, not required:

if (name == "xyz") {
    something;
    something;
} else {
    something_else;
    something_else;
}

Here, indentation doesn't matter to the compiler โ€” only the { } do.

2. Python Style โ€” Indentation Defines the Block

if name == "xyz":
    line1
    line2
    line3
else:
    line1
    line2
    line3

How it works:

Tip: Think of indentation as Python's replacement for { }. Whatever is indented "under" a statement is considered part of that statement's block.

3. Common Indentation Errors (from examples)

Error 1 โ€” IndentationError: expected an indented block

if name == "xyz":
print('line 1')      # โŒ not indented at all
    print('line2')
else:
    print('line3')

Why it fails: After a line ending in :, Python expects the next line to be indented (to start the block). Here print('line 1') has no indentation, so Python doesn't know it belongs to the if.

Error 2 โ€” IndentationError: unindent does not match any outer indentation level

name = "244"
if name == "xyz":
        print('line 1')   # indented 8 spaces
    print('line2')        # indented 4 spaces  โŒ
else:
    print('line3')

Why it fails: print('line 1') starts the block at an 8-space indent. The next line, print('line2'), tries to "unindent" to 4 spaces โ€” but 4 spaces doesn't match any indentation level that Python has already established (it's neither the 8-space block level nor the 0-space outer level). Python gets confused about which block this line belongs to.

Key takeaway: Once you set an indentation level for a block, every line in that block must match it exactly. You can't randomly mix 4 spaces and 8 spaces within the same block.

Error 3 โ€” IndentationError: unexpected indent

name = "244"
if name == "xyz":
    print('line 1')
    print('line2')
    if 5 == 5:
        print('line 5')
else:
    print('line3')

General pattern to remember: This example also shows a nested if โ€” an if statement inside another if block:

if name == "xyz":
    print('line 1')
    print('line2')
    if 5 == 5:              # nested if โ€” one level deeper
        print('line 5')
else:
    print('line3')

The nested if 5 == 5: block (print('line 5')) must be indented one level more than the outer if block's statements.

Summary Table โ€” Indentation Error Types

Error MessageTypical Cause
expected an indented blockA line ending in : is followed by a line with no/insufficient indentation
unindent does not match any outer indentation levelA line's indentation drops to a level that doesn't match any previously opened block
unexpected indentA line is indented without a valid reason (no preceding : block opener at that level)

Things to Remember

Quick Revision

Python doesn't use { } โ€” it uses indentation to show what belongs inside an if, else, loop, etc. After any line ending in :, the next line must be indented more than it. All lines in the same block must share the exact same indentation. If indentation is missing โ†’ expected an indented block. If indentation drops to a level Python doesn't recognize โ†’ unindent does not match any outer indentation level. If a line is indented more than it should be without reason โ†’ unexpected indent. Nested blocks (if inside if) need progressively deeper indentation.