Indentation in Python

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, and Python uses indentation.


What is Indentation in Python?

Indentation in Python refers to the whitespaces at the start of the line. We can create indentation using space or tabs. 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 are used to determine the indentation level of the line. We have to increase the indent level to group the statements for that code block. Similarly, reduce the indentation to close the grouping.

The four white spaces or a single tab character are 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 a backslash.
  • The first line of Python code can’t have an indentation, it will throw IndentationError.
  • You should avoid mixing tabs and whitespaces to create an indentation. It’s because text editors in Non-Unix systems behave differently and mixing them can cause the wrong indentation.
  • It is preferred to use whitespaces for indentation than the tab character.
  • The best practice is to use 4 whitespaces for the first indentation and then keep adding additional 4 whitespaces to increase the indentation.

Benefits of Indentation in Python

  • In most programming languages, 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 by copying the code from online sources, Word documents, or PDF files.
  • Most of the popular programming languages use braces for indentation, so anybody coming from a different programming world finds it hard at first to adjust to the idea of using whitespaces for the indentation.

Indentation in Python for loop

We have to increase the indentation in the for loop so that the code will be grouped together. Let’s look at an example of indentation in python for loop.

def print_numbers(x, y):
    for i in range(x, y):
        print(i)

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 an 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 has 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

Summary

Python indentation makes our code beautiful. It also serves the purpose of grouping the statements into a code block. This results in 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.


What’s Next?

References: