Python Indentation
Indentation means we provide some space before the beginning of our code line. In other programming languages, indentation is used for readability or style, but in Python, indentation is part of our code.
We can indicate a block of code by using indentation. Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code.
Whitespace (space) is used for indentation in Python. So basically, indentation in Python provides two benefits.
First, it represents code blocks; other programming languages use curly brackets {} to define code blocks.
Second, it increases the code readability. Indentation provides clean, organized, and easily understandable code lines.
Indentation is very important in Python, because if we don’t do proper indentation, our code will not run and it will show an IndentationError.
How to Use Indentation in Python?
Every object in Python doesn’t need indentation. Some objects, like functions, loops, conditional statements, etc., used indentation. So basically, indentation is used when we’re using a block of code. For example:
Code:
def first_function():
a = 100
b = 100
print(a+b)
In the second line, when we write a=100
, we didn’t write it just under the first line. We provide some space in the beginning, and then we write a=100
and the same with the third and fourth lines. Now this space we provide in the beginning is called indentation.

Now what if we didn’t provide this space?
Code:
def first_function():
a = 100
b = 100
print(a+b)
Output:
IndentationError: expected an indented block after function definition on line 1
We get an IndentationError. It says ‘expected an indented block after function definition’. So if we are working with functions, we need to use indentation. So when we used indentation in the next three lines, it means we are declaring that the second, third, and fourth lines are related to the function. And this is a block of code.

Now if we do indentation in the second line but not in the third line:
Code:
def first_function():
a = 100
b = 100
print(a+b)
Output:
IndentationError: unexpected indent
We get an IndentationError, but the message is different. Because in first we create a function and in second line we use indentation. That is correct. And in the third line we write b = 100
so as soon as we end the indentation. Interpreter assumes this third line as a new block of code. And now it looks like we are using indentation in the fourth line. But there is no need for indentation. So that’s why we get unexpected indent errors.

Another example: As we learn, every object does not need indentation in Python. Functions need indentation. Conditional statements need indentation. For example:
Code:
a = 10
b = 5
if a > b:
print("Yes, a is greater than b.")
else:
print('No, a is not greater than b.')
Output:
Yes, a is greater than b.
So here if and else are conditional statements in Python. And we used indentation after both statements.

Same if we don’t use indentation. We will get an IndentationError.
Code:
a = 10
b = 5
if a > b:
print("Yes, a is greater than b.")
else:
print('No, a is not greater than b.')
Output:
IndentationError: expected an indented block after 'if' statement on line 3
How Many Spaces Are Required For Indentation in Python?
So basically, how many spaces are required for indentation? PEP (Python Enhancement Proposals) recommended; four spaces are required for indentation. For example:
Code:
def first_function():
print("Learning Indentation")
Here in the second line, when we used indentation, we provided four spaces for indent. However, if we are using an IDE like PyCharm, we don’t need to do indenting manually. IDE automatically adds four spaces as default indentation settings.

Now what if we use three or may be five spaces? For example:
Code:
def first_function():
print("We use five space for indentation")
Code:
def first_function():
print("We use three space for indentation")
Code:
def first_function():
print("We use two space for indentation")
Code:
def first_function():
print("We use one space for indentation")
All the upper examples are correct. Any number of spaces can be used for indentation, but a minimum of one space is required to do it. So as per the Python documentation, four spaces are recommended, but we can use any number of spaces we want. However, there is a rule that we should follow. Consider the below example:
Code:
def first_function():
a = 100
b = 100
print(a+b)
Output:
IndentationError: unexpected indent
This is a block of code because all the other statements are under the function definition. And here we used three spaces to indent the second line, four spaces to indent the third line, and five spaces to indent the fourth line. So when we run this, we get an IndentationError because the number of spaces should be the same in a block of code. So if we indent the second line with three spaces, then the rest of the lines in this block of code should use three spaces for indentation.
Code:
def first_function():
a = 100
b = 100
print(a+b)
Now this is correct. We used three spaces for the second line, and then in the third and fourth lines we used the same three spaces for the indentation. So remember, a block of code should have the same number of spaces for indentation.
Another example: In a program, different blocks of codes could have different kinds of spaces for the indentation. For example:
Code:
def first_function():
a = 100
b = 100
print(a+b)
def second_function():
x = 'Python'
print('X is strings data type')
print('Learning Python')
print('Learning Indentation')
In the upper example, we used five spaces to indent first_function
and two spaces to indent second_function
So we can do this because both functions have separated blocks of codes. And we are following the rules; in the same block of code, we used the same number of spaces for indentation.

Nested Indentation in Python
When we used indentation inside the indentation, that is called the nested indentation. For example:
Code:
def first_function():
a = 100
b = 100
print(a + b)
def second_function():
x = 200
y = 200
print(x + y)
print('Learning Python')
print('Learning Indentation')
Here second_function
is a nested function, and the indentation we used for this function is called the nested indentation.

Now this whole function is a block of code. However, indentation spaces could be different for both functions. For example:
Code:
def first_function():
a = 100
b = 100
print(a + b)
def second_function():
x = 200
y = 200
print(x + y)
print('Learning Python')
print('Learning Indentation')
Here for first_function
we used three spaces, and for nested second_function
we used two spaces. Now definitely this whole function is a block of code, but this block of code is divided into two blocks of code.

So even if we are using nested indenation, we can change the number of spaces for indentation.
Rules for Using Indentation in Python
There are some mandatory rules, which we should follow when indenting our Python code.
1: Indentation is mandatory in Python. It is not just for readability or style, but it is an important part of code.
2: The default indentation spaces are four spaces in Python. However, we can use any number of spaces, but a minimum of one space is required for indenting.
3: Python used indentation to define the block of codes.
4: A block of code should have the same number of spaces for indentation.
5: It is recommended to use space for indent. However, you can use Tab, but it is not recommended. Also, never mix the code with a space and tab.
6: The first line of Python code cannot have an indentation.
What is a block of code in Python?
A program can have different blocks of codes. A block of code consists of several statament or lines of code that are related to each other. Any changes we make in any line will effect the whole block of code.
In Python, indentation is used to define blocks of code. Other programming languages use curly brackets {} to define blocks of code.
Code:
def first_function():
a = 100
b = 100
print(a + b)
def second_function():
x = 200
y = 200
print(x + y)
Here we created two functions. And each function has its own block of codes.

It is also possible a block of code can have another block of codes. See the below diagram:

Conclusion
So this is the end of the Python indentation tutorial. Indentation is very important in Python. If you don’t indent your code with proper indenation, your program will never work. The main work of indentation is to define the block of code.
End of Python Indentation Tutorial
2 thoughts on “Python Indentation: What is Indentation in Python?”