Python Indentation

Indentation in Python is used to create a group of statements. Many popular languages such as C, and Java uses braces ({ }) to define a block of code, Python use indentation.


How Does Python Indentation work?

When writing python code, we have to define a group of statements for functions and loops. This is done by properly indenting the statements for that block.

The leading whitespaces (space and tabs) at the start of a line is used to determine the indentation level of the line. You have to increase the indent level to group the statements for that code block. Similarly, reduce the indentation to close the grouping.

Generally four white spaces or a single tab character is used to create or increase the indentation level of the code. Let’s look at an example to understand the code indentation and grouping of statements.

def foo():
    print("Hi")

    if True:
        print("true")
    else:
        print("false")

print("Done")
Python Indentation
Python Indentation

Python Indentation Rules

  • We can’t split indentation into multiple lines using backslash.
  • The first line of Python code can’t have indentation, it will throw IndentationError.
  • You should avoid mixing tabs and whitespaces to create indentation. It’s because text editors in Non-Unix systems behave differently and mixing them can cause wrong indentation.
  • It is preferred to use whitespaces for indentation than the tab character.
  • The best practice is to use 4 whitespaces for first indentation and then keep adding additional 4 whitespaces to increase the indentation.

Benefits of Indentation in Python

  • In most of the programming language, indentation is used to properly structure the code. In Python, it’s used for grouping, making the code automatically beautiful.
  • Python indentation rules are very simple. Most of the Python IDEs automatically indent the code for you, so it’s very easy to write the properly indented code.

Disadvantages of Indentation in Python

  • Since whitespaces are used for indentation, if the code is large and indentation is corrupted then it’s really tedious to fix it. It happens mostly in copying the code from online sources, Word document, or PDF files.
  • Most of the popular programming languages use braces for indentation, so anybody coming from other side of the development world finds it hard at first to adjust to the idea of using whitespaces for the indentation.

IndentationError Examples

Let’s look at some examples of the IndentationError in the Python code.

>>>     x = 10
  File "<stdin>", line 1
    x = 10
    ^
IndentationError: unexpected indent
>>> 

We can’t have indentation in the first line of the code. That’s why IndentationError is thrown.


if True:
    print("true")
     print("Hi")
else:
    print("false")
Python IndentationError
Python IndentationError

The code lines inside the if block have different indentation level, hence the IndentationError.


if True:
    print("true")
else:
    print("false")

 print("Done")
Python Indentation Level Error Unindent
Python Indentation Level Error Unindent

Here the last print statement has some indentation but there is no statement to attach it, hence the indentation error is thrown.


if True:
print("true")
else:
print("false")

Output:

File "/Users/pankaj/Documents/PycharmProjects/PythonTutorialPro/hello-world/indent.py", line 2
    print("true")
        ^
IndentationError: expected an indented block

Conclusion

Python indentation makes our code beautiful. It also serves the purpose of grouping the statements into a code block. This results into the habit of writing beautiful code all the time because it’s not a Good-To-Have feature but a Must-Have requirement of the code.


References: