Execute Program

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 if as an example. if works like it does in other languages: if the condition is True, we run the block.

  • >
    x = 10

    if x > 5:
    x = x + 1

    x
    Result:
    11Pass Icon
  • >
    x = 10
    if x == 10:
    x = x + 1
    x = x + 1
    x
    Result:
    12Pass Icon
  • 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 if block, reducing its indentation level, then it's no longer part of that block.

  • >
    x = 10
    if x > 100:
    x = x + 1
    x = x + 1
    x
    Result:
    10Pass Icon
  • >
    x = 10
    if x > 100:
    x = x + 1
    x = x + 1
    x
    Result:
    11Pass Icon
  • 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 if that has a deeper indentation level. That can lead to situations like the one shown above: the second x = x + 1 line isn't actually part of the if.

  • 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 = 10
    if x < 100:
    x = x + 1
    x = x + 1
    x
    Result:
    12Pass Icon
  • However, inconsistent indentation within a block is a syntax error.

  • (You can type error when a code example will cause an error.)

  • >
    x = 10
    if x > 100:
    x = x + 1
    x = x + 1
    x
    Result:
    IndentationError: unexpected indent (<string>, line 4)Pass Icon
  • Python will allow inconsistent indentation from block to block, as long as each individual block is consistent with itself.

  • >
    x = 10
    if x < 100:
    x = x + 1
    x = x + 1

    if x > 100:
    x = x + 5
    x
    Result:
    12Pass Icon
  • When the body of an if only contains one line, we can collapse the whole conditional into a single line.

  • >
    x = 10
    if x == 10:
    x = x + 1
    x
    Result:
    11Pass Icon
  • >
    x = 10
    if x == 10: x = x + 1
    x
    Result:
    11Pass Icon
  • 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.