Indentation (Python)

In Python, indentation (the spaces at the start of a line) defines which lines belong to a block of code. Unlike most languages, the whitespace is part of the syntax, not just style.

Python

Code inside a function, loop, or if-statement must be indented consistently — the standard is 4 spaces. Mixing tabs and spaces, or getting the indentation wrong, causes an IndentationError.

Examples

Python

if True:
    print("indented")
    print("same block")
print("outside")

Output:

indented
same block
outside

See also

Back to the Dictionary