6) b. Indentation

As you have learned earlier, certain constructs such as if, for, def and others require you to indent the body of the construct.

1
2
3
4
5
6
7
8
9
10
11
if condition:
    # Do something useful if condition is true.
    pass

for i in range(10):
    # Do something useful 10 times.
    pass

def my_function():
    # Do something useful as a function.
    pass

The question now is what should you use for indentation, spaces or tabs? From the first glance using tabs seems easier than inserting spaces multiple times especially in basic text editors.

However, according to PEP 8, 4 spaces are preferred over tabs. Unlike using tabs, using spaces allows the code to look the same across different text editors and consistency is your main goal here.

Most code-oriented text editors allow you to bind Tab to insert specified number of spaces.

Using tabs is only preferred when your code base already uses tabs.