Python Indentation

Introduction

Indentation is the placement of text from left or right to separate it from the other texts. In computer programming, a block of code is the set of statements for a specific purpose. Unlike some programming languages like C, C++ these blocks are defined by the use of curly braces {…}, Python use whitespaces for indentation. All the statements belonging to the same distance from right considered to be in the same block of code.

Let’s dig into an example.

num = 1
while(i<=10):
        print(i)
        i = i + 1

 

In the above example, the statements print(i) and i = i+1, is indented at the same distance to show which block (while loop) it belongs.

Rule of Python indentation:

  • In python if you use colon (:) at the end of the statement, you have to indent the following line of codes. Otherwise indentation error will be there.
  • If you skip the indentation in the block, it will throw an error. This will throw error

while(i<=10):

print(i)

  • The number of whitespaces used for indentation is up to the developer. But there must be at least one whitespaces.
  • The number of whitespaces used should be the same if you have multiple statements inside the block, otherwise it will throw a syntax error. This will throw error
while(i<=10):
        print(i)
                i = i + 1

 

Importance of Python Indentation

We all are aware of social networking and are widely using it in our daily life. We do comments and replies on posts every now and then. But have you noticed the pattern in which they are aligned. The replies goes little right from the comment. Why is it so? The replies are aligned little right so that you can know which comment it belongs to. Whereas, the comments don’t get aligned to right, only the replies are indented. This makes the comments easy to read and understand.

This is same with python. We can consider the comments as block and replies as the indented codes. Python Interpreter acknowledge the statements within the code through indentation.

More than the styling purpose, indentation in python is a requirement to compile and execute the code.

Errors and Fixes during Indentation

If there is anomaly in code indentation, the program will encounter indentation error while compiling the code.

What causes indentation error in Python?

  • Using both whitespaces and tabs in code.
  • Compound statements such as if, for, while etc. are not indented.
  • User-defined function and classes are not indented.
  • Indent is placed at the wrong place.

Fix – “Indentation Error: Expected an Indented block”

Checking whitespaces and tabs in code.

Solution 1:

Always keep the indentation consistence in your code. If you are using whitespaces in your code, use only whitespace throughout the code. And if you are using tab, use only tabs. The mixing of both whitespaces and tabs will cause the error.

The correct form will look like:

Python Indentation

Solution 2:

In case you are not able to identify the tabs and whitespaces you can enable the tab/space symbols from view in the menu bar in your IDE. This will enable the dots in the code which represents whitespaces and tabs.

Python Indentation Example

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.