Programming Search⌘K

Featureintro

Indentation

Also called significant whitespace, off-side rule, INDENT and DEDENT, IndentationError, TabError.

Python groups statements by indentation, not by braces. The tokenizer turns changes of indentation into INDENT and DEDENT tokens; bad indentation is an IndentationError or a TabError.

Language
Python

Synopsis

for n in [1, 2, 3]:
    if n % 2:
        print(n, "is odd")
    else:
        print(n, "is even")
print("done")
Output
1 is odd
2 is even
3 is odd
done

The body of a compound statement is the lines after the colon that are indented more than its header. The first line with less indentation ends the body.1

Semantics

r1. The spaces and tabs at the start of a logical line give its indentation level, and the levels group the statements.2

r2. A tab moves to the next column that is a multiple of eight. The indentation of a line is the number of columns before its first character that is not blank.2

r3. A source file that mixes tabs and spaces so that the meaning depends on the width of a tab is rejected with a TabError.2 TabError is a subclass of IndentationError, which is a subclass of SyntaxError.3 Example: tab-error.

r4. The tokenizer keeps a stack of levels, with 0 at the bottom. At the start of each logical line, a level greater than the top is pushed and gives one INDENT token. A smaller level must be on the stack: each greater level is popped and gives one DEDENT token. At the end of the file, each level above 0 gives a DEDENT.2 4 Example: indent-tokens.

r5. The levels need not be multiples of anything. Each body can use its own width, as long as the lines of one body agree. A dedent to a level that is not on the stack is an error.2 1 Example: errors.

r6. Only the start of a logical line counts. The continuation lines inside brackets have free indentation, and a backslash cannot split the indentation over two lines.2

r7. In the grammar, a block is NEWLINE INDENT statements DEDENT, or simple statements on the line of the header.5

Examples

The tokens that the indentation of a small program makes (rule r4).

import io
import tokenize

source = "if x:\n    if y:\n        a()\n    b()\nc()\n"
names = []
for token in tokenize.generate_tokens(io.StringIO(source).readline):
    if token.type in (tokenize.INDENT, tokenize.DEDENT):
        names.append(f"{tokenize.tok_name[token.type]}@{token.start[0]}")
print(" ".join(names))
Output
INDENT@2 INDENT@3 DEDENT@4 DEDENT@5

The errors of rules r3 and r5, from compile() on CPython 3.12.

programs = {
    "unexpected indent": "x = 1\n    y = 2\n",
    "no body": "if True:\nprint('hi')\n",
    "bad dedent": "if True:\n        a = 1\n    b = 2\n",
    "tabs and spaces": "if True:\n        a = 1\n\tb = 2\n",
}
for label, source in programs.items():
    try:
        compile(source, "demo.py", "exec")
    except SyntaxError as error:
        print(f"{label:18} {type(error).__name__}: {error.msg}")
Output
unexpected indent  IndentationError: unexpected indent
no body            IndentationError: expected an indented block after 'if' statement on line 1
bad dedent         IndentationError: unindent does not match any outer indentation level
tabs and spaces    TabError: inconsistent use of tabs and spaces in indentation

Pitfalls

A line at the wrong level changes the program

A wrong indentation that is still consistent gives no error, and the program does something else. Here the addition is outside the loop, so it runs once, with the last square.

This code runs but gives a wrong answer on purpose.

total = 0
for n in [1, 2, 3]:
    square = n * n
total += square
print(total)
Output
9

Indented one level, total += square runs three times and the total is 14.

Tabs and spaces

A tab and eight spaces are the same column for the rule r2, but the tokenizer rejects a file in which the two forms decide the structure (rule r3). PEP 8 says: "Use 4 spaces per indentation level", and "Spaces are the preferred indentation method".6

This code raises TabError, on purpose.

compile("if True:\n        a = 1\n\tb = 2\n", "demo.py", "exec")

Comparison

In Go, a block is "a possibly empty sequence of declarations and statements within matching brace brackets", and the indentation has no meaning.7 In Python the indentation is the block (rule r7), so a program cannot look grouped one way and run another way.

References

  1. The Python Tutorial, Python 3.15, 3.2 First Steps Towards Programming
  2. The Python Language Reference, Python 3.15, 2.1.8 Indentation
  3. Built-in Exceptions, Python 3.15 documentation, IndentationError, TabError
  4. token, Python 3.15 documentation, token.INDENT, token.DEDENT
  5. The Python Language Reference, Python 3.15, 10 Full Grammar specification, block
  6. PEP 8: Style Guide for Python Code, Indentation; Tabs or Spaces?
  7. The Go Programming Language Specification, Blocks

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source