Code structure and significant indentation

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 ...

Get The Python Apprentice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.