Python for Programmers: Significant Whitespace
Welcome to the Significant Whitespace lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
If a programmer only knows one fact about Python, it's probably this: Python has significant whitespace. It doesn't use {braces} or do/end markers to separate code blocks, like many other languages do. Instead, lines of indented code form a block, and are treated as one group. By contrast, most programming languages have "insignificant whitespace", where adding or removing indentation doesn't change the program's meaning.
In this lesson, we'll see significant whitespace in practice, using Python's
ifas an example.ifworks like it does in other languages: if the condition isTrue, we run the block.>
x = 10if x > 5:x = x + 1xResult:
11
>
x = 10if x == 10:x = x + 1x = x + 1xResult:
12
Python uses significant whitespace for loops, function definitions, classes, and many other language features. Significant whitespace lets Python use less punctuation, which makes the syntax "cleaner" than other languages.
However, there's also a cost to this. Mistakes in indentation can change code's meaning. For example, if we accidentally "dedent" a line inside the
ifblock, reducing its indentation level, then it's no longer part of that block.>
x = 10if x > 100:x = x + 1x = x + 1xResult:
10
>
x = 10if x > 100:x = x + 1x = x + 1xResult:
11
Indentation mistakes are most likely to happen when copying and pasting code. For example, we might copy a few lines of code from a function's body, then paste them into an
ifthat has a deeper indentation level. That can lead to situations like the one shown above: the secondx = x + 1line isn't actually part of theif.This is a minor annoyance at first, but it usually doesn't take long to adjust to it. We just need to be a bit more careful when pasting Python code.
Most Python code uses 4-space indents. In this course, we use 2-space indents to help our code examples fit better on mobile devices. Both indentation levels work. In fact, as long as we use consistent indentation levels within each block, any amount of indentation works!
>
x = 10if x < 100:x = x + 1x = x + 1xResult:
12
However, inconsistent indentation within a block is a syntax error.
(You can type
errorwhen a code example will cause an error.)>
x = 10if x > 100:x = x + 1x = x + 1xResult:
IndentationError: unexpected indent (<string>, line 4)
Python will allow inconsistent indentation from block to block, as long as each individual block is consistent with itself.
>
x = 10if x < 100:x = x + 1x = x + 1if x > 100:x = x + 5xResult:
12
When the body of an
ifonly contains one line, we can collapse the whole conditional into a single line.>
x = 10if x == 10:x = x + 1xResult:
11
>
x = 10if x == 10: x = x + 1xResult:
11
Although that syntax works, it's rarely used in complex Python projects. The community convention is to always put the condition and block on different lines.
Python's significant whitespace follows the same rules in
if,while,for, function definitions, classes, and in other language features. Everything that we saw here, including the rule about collapsing onto a single line, applies throughout the language.