What is Indentation in Python and Why is Python Indentation Important?

Python indentation is an important concept to know even before you get started with coding in Python. This is because if you are not aware of how to indent a Python code, then you will end up seeing indentation errors and you code will not get compiled. So quickly go through this article to know Python indentation and its importance.

What is Python Indentation

Compilers/interpreters generally do not know the sequence in which they need to execute the statements in a piece of code. Hence, to make it easier, we divide the code into several blocks of code and indent it. This indentation helps them understand the order in which each block/statement should be executed.

why is python indentation important

Similarly, Python indentation is a way of telling the Python interpreter that a series of statements belong to a particular block of code. In languages like C, C++, Java, we use curly braces { } to indicate the start and end of a block of code. In Python, we use space/tab as indentation to indicate the same to the compiler. 

In simple, all the statements with the same distance (space) to the right, belong to the same block. In other words, statements belonging to a block will necessarily start from the same vertical line. You can imagine it this way.

python-indentation-example

Note: By default, Python uses four spaces for indentation. However, the number of spaces you use is up to you, but a minimum of one space has to be used.

How to Indent your Python Code?

Let us consider this example – Write a Python program to take input from the user regarding the easiest programming language. Display an error message if the user inputs any language apart from Python. 

Solution:

a = input('Enter the easiest programming language: ')  //statement 1
if a == 'python':  //statement 2
    print('Yes! You are right')  //statement 3
else: //statement 4
    print('Nope! You are wrong') //statement 5

In the above code,

Statement 1 gets executed first, then it gets to statement 2. Now only if statement 2 is correct, we would want statement 3 to get printed. Hence Statement 3 is indented with 2 spaces. Also, statement 5 should be printed, if statement 4 is true and hence this is also indented with 2 spaces. Statement 1, 2 and 4 are main statements which need to be checked and hence these 3 are indented with the same space.

A comparison of C & Python will help you understand it better.

indentation-in-python

How to Avoid Python Indentation Errors 

Here are a few ways to avoid Python indentation errors.

  • Use either the space button or tab button to indent your code. If both are used interchangeably, the interpreter will not be able to interpret the code and hence throws an error.
  • If you have used only one among them, then check for consistency in the indentation. If you indented block code 1 with 4 spaces, then block code 2 has to be indented with 8 spaces and not anything less than that.
  • Finally, change your editor to make tabs and spaces visible and this will help you spot them easily.

Python Indentation FAQs

Yes! In the case of Python, indentation is not for styling purpose. It is rather a requirement for your code to get compiled and executed. Thus it is mandatory.

The compiler will throw an indentation error.

 

Some common causes of this error include:

  • Forgetting to indent the statements within a compound statement (such as the bodies of if and for)
  • Forgetting to indent the statements of a user-defined function

Some common causes of this error include:

  • Forgetting to indent the statements within a compound statement (such as the bodies of if and for)
  • Forgetting to indent the statements of a user-defined function