Indentation in Python | Python Indentation - Scaler Topics (original) (raw)

Overview

Indentation in Python is simply the spaces at the beginning of a code line. Indentation in other languages like C, C++, etc., is just for readability, but in Python, the indentation is an essential and mandatory concept that should be followed when writing a python code; otherwise, the python interpreter throws IndentationError.

What is Indentation in Python

Indentation is the leading whitespace (spaces or/and tabs) before any statement in Python. The reason why indentation is important in python is that the indentation serves another purpose other than code readability. Python treats the statements with the same indentation level (statements with an equal number of whitespaces before them) as a single code block. So whereas in languages like C, C++, etc. a block of code is represented by curly braces { }, in python a block is a group of statements that have the same Indentation level i.e same number of leading whitespaces.

python indentationBelow are some of the observations that can be made from the above figure:

Execution

Examples

Example 1: Below is an example code snippet with the correct indentation in python.

Code:

Output:

Explanation:

Example 2: Below is an example code snippet with correct indentation.

Code:

Output:

Explanation:

How to indent your python code

Let us walk through how to indent a python code by taking a simple example.

Example: Check if a given number is even or odd and if it’s zero print neither even nor odd

Let us discuss the approach step-wise:

Code:

Output:

Explanation:

How to avoid python indentation errors

1. Python will throw an indentation error if you skip the indentation. For Example, the below code would throw IndentationError: expected an indented block error:

Wrong Indentation(Error):

With Correct Indentation:

2. The number of whitespaces in indented code should be the same for the same block of code. Otherwise, Python will throw IndentationError: unexpected indent.

Wrong Indentation(Error):

With Correct Indentation:

3. Indentation on the first line of code is not allowed. Python will throwIndentationError: unexpected indent.

Wrong Indentation(Error):

With Correct Indentation:

4. Indented statements should have an attaching statement; for instance, all the statements indented below form a block and belong to the if statement. This is applicable for while, for, functions, classes, etc in python. The below example makes this point clear.

Wrong Indentation(Error):

The above program will throw the “IndentationError: unindent does not match any outer indentation level” error because the last print statement, which is indented, does not match with any other indentation(no attaching outer statement). In the program, hence this will throw an error.

Correct Indentation:

Here the last print statement’s indentation matches with the indentation of the print statement below if block. Hence here, the outer attaching statement is the if statement.

Python Indentation Rules

Benefits of Indentation in Python

Disadvantages of indentation in Python

Conclusion

Read More:

  1. What is comments in Python?
  2. Important differences between Python 2 and 3