Indentation in Python

In Python, indentation is used to create a group or set of statements. Many popular programming languages, including C and Java, use braces ( ) to define a block of code, whereas Python uses indentation.

Working of Indentation in Python:

We must define a set of statements for functions and loops when writing Python code. This is accomplished by properly indenting the statements within that block.

The leading whitespaces (spaces and tabs) at the beginning of a line are used to determine the line’s indentation level. To group the statements for that code block, you must increase the indent level. Similarly, in order to close the grouping, reduce the indentation.

To create or increase the indentation level of the code, four white spaces or a single tab character are typically used. Let’s look at an example to better understand code indentation and statement grouping.

For Example:

def number():
    print("hello btechgeeks")

    if True:
        print("It is True")
    else:
        print("This is False")


print("Task completed")

Rules of Python Indentation:

  • A backslash cannot be used to divide indentation into multiple lines.
  • The first line of Python code cannot have indentation; otherwise, an IndentationError will be thrown.
  • To create an indentation, avoid combining tabs and whitespaces. Because text editors in non-Unix systems behave differently, mixing them can result in incorrect indentation.
  • It is preferable to use whitespaces rather than the tab character for indentation.
  • The best practice is to use four whitespaces for the initial indentation and then add four more whitespaces to increase the indentation.

The Advantages of Python Indentation:

Indentation is used to properly structure code in most programming languages. It’s used for grouping in Python, which makes the code automatically beautiful.

Indentation rules in Python are extremely simple. Most Python IDEs indent the code for you, making it very simple to write properly indented code.

The Disadvantages of Python Indentation:

Because whitespaces are used for indentation, if the code is large and the indentation is corrupted, it is extremely difficult to fix. It most commonly occurs when copying code from online sources, Word documents, or PDF files.

Because most popular programming languages use braces for indentation, anyone coming from the other side of the developed world finds it difficult to adjust to the idea of using whitespaces for indentation at first.

Python Indentation

Examples:

Example1:

     gvn_str = "hello btechgeeks"

Output:

File "/home/1822dd42441310e7f90ca50f56c63b98.py", line 1
    gvn_str = "hello btechgeeks"
    ^
IndentationError: unexpected indent

Explanation:

Since there is an indentation given in the first line, it raises an error.
We should not give any spaces/indentation in the code's first line.

Example2:

gvn_num = 10
if gvn_num%2==0:
print("Even number")

Output:

 File "/home/06af42430346f69242da94a74c132502.py", line 3
    print("Even number")
        ^
IndentationError: expected an indented block

Explanation:

You should give an indentation after the if conditional statement.
Hence error has occured.

Example3:

1.gvn_num = 10
2.if gvn_num%2==0:
3.    print(gvn_num)
4.      print("Even Number")

Output:

 File "/home/4b9b157f046e5479f32e894aa9c29a40.py", line 4
    print("Even Number")
                       ^
IndentationError: unindent does not match any outer indentation level

Explanation:

The indentation level of the code lines within the if block differs,
resulting in the IndentationError.

Example4:

def number():
    print("hello btechgeeks")

    if True:
        print("It is True")
    else:
        print("This is False")


  print("Task completed")

Output:

 File "/home/443adb7a95629211bdf53e795522d50a.py", line 10
    print("Task completed")
                          ^
IndentationError: unindent does not match any outer indentation level

Explanation:

The indentation error is thrown because the last print statement has 
some indentation but no statement to attach it to.

Conclusion

Python indentation makes our code look nice. It also serves as a container(grouping) for the statements in a code block. This leads to the habit of writing beautiful code all the time because it is a Must-Have requirement of the code rather than a Nice-To-Have feature.