Block Indentation in Python

By: On:

In this Python tutorial, we will discuss what is Block Indentation in Python and we will see a few examples on how to use Block Indentation in Python and how to solve IndentationError: Unindent does not match any outer indentation level in python.

Python Indentation

In python, we use indentation to define control and loop. Python uses the colon symbol (:) and indentation for showing where blocks of code begin and end. Indentation makes the code more readable and in python, indentation is very important.

Example:


def my_function():
    x = 10
    return x
print(my_function())

After writing the above code (Python Indentation), Ones you will print “my_function()” then the output will appear as a “ 10 “. Here, we have seen the whitespace which is used for indentation and it makes the code look neat and understandable.

You can refer to the below screenshot Python Indentation

Python Indentation

Here, we will see how Python will throw an error if indentation is skipped from the block of code while writing.

Example:


def my_function():
x = 10
return x
print(my_function())

After writing the above code (Python Indentation), Ones you will print “my_function()” then the output will appear as an “ IndentationError: expected an indented block “. Here, we skipped the indentation on line 2 while writing the code and it throws an error.

You can refer to the below screenshot Python Indentation Error

Python Indentation

Python Indentation can be ignored by line continuation which means we can write the code in one line but it makes the code difficult to read and understand, so it is good to indent.

Example:

if true:
 print('Welcome')

After writing the above code (Python block Indentation), Ones you will print “ Welcome ” then the output will appear as a “ Welcome “. Here, we ignored the indentation by a line continuation and it is valid too as it does the same thing.

You can refer to the below screenshot python block indentation

Python block indentation

But its always a good idea to indent, you can refer to the below screenshot as it makes the code more readable.

Example:

if true:
    print('Welcome')
Python block indentation

Unindent does not match any outer indentation level in python

This error happens if you copy and paste from the web page, which mixes up the tabs and spaces of the indentation. If a code snippet you copy into your program uses a different type of indentation, you may see this type of error in your code because it mixes tabs and spaces.

Example:

def get_factors(num):
    for i in range(1, num + 1):
        if num % i == 0:
   print("{} is a factor of {}.".format(i, num))
get_factors(10)  

After writing the above code (unindent does not match any outer indentation level in python), Ones you will print then the error will appear as “ IndentationError: unindent does not match any outer indentation level “. Here, the error occurs due to mixing spaces and tabs.

You can refer to the below screenshot for unindent does not match any outer indentation level in python

Unindent does not match any outer indentation level in python
Unindent does not match any outer indentation level in python
  • To solve this indentation error, we can check the line where the error is raised. Each line represents a tab and each dot represent a space.
  • In sublime Text Editor, open the python program and select the full program by clicking ctrl + A.
  • The entire python code and white spaces will be selected together.
  • The tab key is displayed as continuous lines, and the spaces are displayed as dots in the program.
  • Stick to any format, either on the tab or in space, and change the rest to make a uniform format.

Example:

def get_factors(num):
    for i in range(1, num + 1):
        if num % i == 0:
            print("{} is a factor of {}.".format(i, num))
get_factors(10)  

After writing the above code, you will get the output and the error is resolved. Here, we corrected the mistake and you can see the proper styling of the code with correct indentation.

You can refer to the below screenshot python unindent does not match any outer indentation level.

Unindent does not match any outer indentation level in python
Unindent does not match any outer indentation level in python

You may like the following Python tutorials:

This is how we can use Block Indentation and the IndentationError: Unindent does not match any outer indentation level in python.

Leave a Comment