Python Indentation

Popular programming languages like C, C++, C#, and Java use the curly brackets {} in order to define code blocks for namespaces, for, while, function, and related implementation. Python has a bit different mechanism to define code blocks which are called indentation.


Indentation is creating Python code blocks by using spaces to the left. There is not a specific count of spaces for indentation but it should be consistent for the entire Python file. As an example, the developer uses 3 spaces from the start of the line for the first level indentation. Another 3 spaces for the 2nd level indentation etc. This space count should be consistent and the same for all indentation implementations. Using indentation force the Python code to be more readable and easy to grasp but also create problems if the space count is not the same or tabs can break the code block and Python interpretation.

Indentation Example

Let’s make some Python indentation example in order to understand better. Every block definition like for, while, the function requires indentation in order to create and code block for this statements.

for i in range(1,5):
   print("We will check ",i)
      print(i,"is diviseable to 2")
  • for i in range(1,5): line do not have indentation but using the for loop requires indentation definition.
  • print(“We will check “,i) line indentation 3 spaces which makes it the code clock of the previous for loop.
  • if(i%2==0): line is the code block of the for loop and requires 2nd level indentation because of if definition.
  • print(i,”is diviseable to 2″) is the 2nd level indentation which is previous if statement code block.

Indentation Space Count

The space count for the indentation is very important because it should be the same and consistent throughout the Python file or script. Generally, 3 spaces are used for each level of the indentation. Using the tab will create errors because the indentation requires spaces or whitespaces which are not the same as tabs even if they may seem the same. If you use tab as indentation you should continue to use tab for every indentation but this will create problems in general. Python 3 does not allow to use of both tabs and spaces for indentation.

Indentation Errors

Especially newcomers to the Python programming language will face indentation errors in the first months of their learning process. Even though there may be different indentation errors the most popular and generic indentation error is “IndentationError: unexpected indent” which is like below.

Indentation Errors

Python Indentation Rules

  • Python default indentation is 4 spaces but there is no standard for users. Developers can use different numbers of spaces which should be consistent for the same Python file.
  • To prevent ambiguities the Python file’s first line can not have an indentation.
  • To create different code blocks like functions, classes, if and while conditions, etc. the indentation should be used.
  • Even indentation can be created by using tabs using spaces are more readable and less error-prone.

Leave a Comment