Python Indentation Explained With Examples

In many programming languages, the indentation is used for clear readability of the code. Indentation is the technique followed to add or remove a specific amount of whitespaces before a statement of code. In Python, if the indentation is not carried out properly, the interpreter throws an “unexpected error”.

This write-up provides a complete overview of Python indentation with numerous examples. The below-listed contents are discussed in this tutorial:

So let’s get started!

What is Python Indentation?

In Python, all the whitespaces at the start of the code line are known as indentations. The indentation is created with the help of “space” and “tabs”. All the conditional statements, such as “for-loop”, ”while-loop”, etc., are defined with proper indentation blocks. The statements having the same indentation level will be treated in the same code block. Let’s understand Python Indentation with an example of the code given below.

Code

number = 191

if number > 0:
    print("Positive number")
elif number == 0:
    print("Zero")
else:
    print("Negative number")

In the above code, a simple “if-else if” statement is initialized to understand the concept of block indentation in Python.

In the above snippet, the “RED LINE” indicates the indentation of the “if-elif-else” block code. All the statements of if else block code will follow the same indentation level. The “BLUE LINE” in the above snippet indicates that the code in Python always starts without indentation.

Rules of Python Indentation

Following are the rules of Python Indentation:

  • Always use four whitespaces or a single tab for the first indentation and then keep increasing the indentation with additional spaces.
  • Use whitespace for indentation rather than tab characters. It is optional, but the chances of error become low while using whitespaces.
  • The source file code or the first line of a python script does not have any whitespaces or tabs. To code in Python, it is “compulsory” to start the script without any whitespace.
  • Do not mix the tab and whitespaces while indentation of code. It creates the wrong indentation and causes an error while executing the program.

Let’s understand the indentation concept via examples.

Example 1: Wrong Python Indentation for the While Loop

For a better understanding, we will first tell you what the wrong indentation means in Python. Let’s refer to the following code:

Code

#wrong indentation of while Loop
number = 3
while(number <= 7):
    print("Number value is " + str(number))
        number = number + 1

In the above code, the variable named “number” is placed at wrong indentation. The extra whitespace before the variable causes an “unexpected indent error”.

Output

The output shows the “unexpected indent” error.

Example 2: Correct Python Indentation for the While Loop

The following code refers to the correct indentation to address a “While” loop:

Code

Code 
#indentation of while Loop
number = 3
while(number <= 7):
    print("Number value is " + str(number))
    number = number + 1

In the above code, the number is initialized in a variable named “number”. The “while” loop statement is placed in the code by increasing the indentation level. So, the referring code will be grouped. The print statement and the variable named “number” have been placed with the same indentation level.

Output

The successful execution of code indicates that the indentation errors have been removed.

Conclusion

In Python, Indentation is the foundation of every code as it makes our code more readable and beautiful. Indentation refers to the “whitespaces” and “tabs” before code lines. Without proper indentation, you may be unable to execute the Python code. This post has demonstrated the explanation of the indentation and also describes why it is important with the help of suitable examples.