Start your Python 3 interpreter:
> python
If on Windows or:
$ python3
On Mac or Linux.
The control flow structures of Python, such as for-loops, while-loops, and if-statements, are all introduced by statements which are terminated by a colon, indicating that the body of the construct is to follow. For example, for-loops require a body, so if you enter:
>>> for i in range(5):
...
Python will present you with a prompt of three dots to request that you provide the body. One distinctive (and sometimes controversial) aspect of Python is that leading whitespace is syntactically significant.
What this means is that Python uses indentation levels, rather the braces used by other languages, to demarcate code blocks.By convention, contemporary Python code is indented by four spaces for each level.
So when Python present us with the three dot prompt, we provide those four spaces and a statement to form the body of the loop:
... x = i * 10
Our loop body will contain a second statement, so after pressing Return at the next three dot prompt we'll enter another four spaces followed by a call to the built-in print() function:
... print(x)
To terminate our block, we must enter a blank line into the REPL:
...
With the block complete, Python executes the pending code, printing out the multiples of 10 less than 50:
0
10
20
30
40
Looking at at screenful of Python code, we can see how the indentation clearly matches — and in fact must match — the structure of the program which is as follows:
Even if we replace the code by gray lines, the structure of the program is clear as shown in the following image:
Each statement terminated by a colon starts a new line and introduces an additional level of indentation, which continues until a dedent restores the indentation to a previous level. Each level of indent is typically four spaces, although we'll cover the rules in more detail in a moment.
Python's approach to significant whitespace has three great advantages:
- It forces developers to use a single level of indentation in a code-block. This is generally considered good practice in any language because it makes code much more readable.
- Code with significant whitespace doesn't need to be cluttered with unnecessary braces, and you never need to have code-standard debates about where the braces should go. All code-blocks in Python code are easily identifiable and everyone writes them the same way.
- Significant whitespace requires that a consistent interpretation must be given to the structure of the code by the author, the Python runtime system and future maintainers who need to read the code. As a result you can never have code that contains a block from Python's point of view, but which doesn't look like it contains a block from a cursory human perspective.
The rules for Python indentation can seem complex, but they are quite straightforward in practice:
-
The whitespace you use can be either spaces or tabs. The general consensus is that spaces are preferable to tabs, and four spaces has become a standard in the Python community.
-
One essential rule is NEVER to mix spaces and tabs. The Python interpreter will complain, and your colleagues will hunt you down.
-
You are allowed to use different amounts of indentation at different times if you wish. The essential rule is that consecutive lines of code at the same indentation level are considered to be part of the same code block.
-
There are some exceptions to these rules, but they almost always have to do with improving code readability in other ways, for example by breaking up necessarily long statements over multiple lines.
This rigorous approach to code formatting is Programming as Guido intended it or, perhaps more appropriately, as Guido indented it! A philosophy of placing a high value on code qualities such as readability gets to the very heart of Python culture, something we'll take a short break to explore now.