Python: Notes on Indentation

In Python the interpreter identifies blocks of code based on indentation.

By way of example, in an if-loop if the next line is indented then it means that this indented block is within the loop. You can use either spaces or tabs, e.g. you can not mix both, to quote PEP 8 – Style Guide for Python Code:

Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

The standard method is to use 4 spaces for indentation, which also happens to be the necessary number of spaces to designate a codeblock on Stackoverflow.

Fortunately, if you’re using the PyDev plugin for Eclipse or many other popular IDE’s, then you can press tab in the text editor and the IDE will automatically translate that to 4 spaces.