Indentation in Python: A Beginner’s Guide

With multiple lines of code in a program, it is just a matter of time before one finds amidst a spaghetti of logic, conditions and other sets of instructions having a hard time figuring out which works alongside what. This situation is highly unfavourable when it comes to troubleshooting the code or integrating it with another program.

So, does Python offer any assistance as a way around this mishap? Enter the Indentation! It is a technique by which the succeeding line of code starts a few characters to the right of the preceding line of code so as to differentiate between the main codes with their corresponding subcodes. The following sections in this article elaborate on this front.

Also Read: A Detailed Guide on Python Identifiers

Indentation – a Simple Definition

To put it in simple terms, when the beginning of any code in Python starts after a number of empty spaces or tabs, it is said to be indented. Python programming sets 4 empty spaces before the start of a code by default for an indentation. This is in accordance with the PEP8 style guide which Python complies with. However, it is up to the discretion of the programmer to modify it at will.

Benefits of Indenting in Python

Python programming is done by creating several blocks of code that are run alongside each other. It is the indentation that plays a crucial role in conveying the Python interpreter which are all the set of codes that belong to a particular block & all those that aren’t.

Thereby a small shift in the indentation of any given line of code might send a wrong signal to the Python Interpreter that it belongs to a different code block & one might end up in some unforeseen error.

Looking out for Indentation Errors

Well, for starters, one might very well start out by verifying the indentations within a program line by line. However, this can be a nightmare in some situations.

Especially during times of creating programs with lengthy sets of codes, the potential occurrence of missing an indentation is seemingly plausible. The result in this case might be like looking for a needle in a haystack, to find out where the indentation was missed in the first place.

But worry not, for there is a way around! One can open the settings of the code editor and choose the option that shows the tabs and whitespace.

Given below is a typical example of how indentation is used by the Python interpreter to identify the different code blocks in a program.

Block 1
	Block 2
		Block 3
	    Block 3
    Block 2
Block 1

Examples of using Indentation

Given below are some examples which would help you understand better the usage of indentations.

sex = 'male'
if sex == 'female':
    print("You are eligible for concession")
    print("You are not eligible for concession")
Indents Isolating the 'if' From The 'else' Block
Indents isolating the ‘if’ from the ‘else’ block

So what if we don’t specify the codes with the indentation as given in the above image? Let us see what happens then!

Indentation Error Appears
Indentation Error Appears!

As stated earlier without the indents, the Python interpreter stops executing the code right where it encounters a missing indent & throws up an indentation error as shown in the above image. So, it is now evident that only with the indent the Interpreter can identify which set of codes are to be executed ‘if’ the given logical condition is met, ‘else’ which set of codes are to be executed when not met.


Now that we have reached the end of this article, hope it has elaborated on how to use indentation effectively in Python programming. Here’s another article that can be your definitive guide to the different types of keywords in Python. There are numerous other enjoyable and equally informative articles in CodeforGeek that might be of great help for you to gain valuable insights. Until then, ciao!


Arulius Savio
Arulius Savio
Articles: 26