Introduction to Indentation in Python
Indentation in general means indenting words or spaces or lines in the document to follow the styling rule for documentation, or it may be used to explain the distances that should be used or the spaces that should be used while writing the documents or codes. In Python, indentation is one of the most distinctive features, and it can be defined as it refers to the same meaning as that of the general, which means that the compiler in Python cannot execute without indentation; therefore, the code is written with some spaces or tabs into many different blocks of code to indent it so that the interpreter can easily execute the Python code.
Role of Indentation in Python with Examples
The identification of a block of code in Python is done using Indentation. In many different programming languages like C, C++, Java, etc. use flower brackets or braces {} to define or to identify a block of code in the program, whereas in Python, it is done using the spaces or tabs, which is known as indentation and also it is generally known as 4 space rule in Pep8 documentation of rules for styling and designing the code for Python. Let us consider an example of this.
Example #1
Code:
n = 10
if n>5:
print "n is greater than 5"
else:
print "n is not greater than 5"
Output:
In the above block of code, the indentation is used after the “if” and “else” statement so that the Python interpreter can execute the print statement and gives the proper output; else if it is not properly indented, then it would throw us an error which is seen in below output. The print statement (“ n is greater than 5” ) and the print statement ( “ n is not greater than 5” )are two different clocks of code. To indicate these blocks of code, Python uses indentation at the beginning of each line of the block with the same number of spaces that are 4 spaces.
Example #2
Code:
n = 10
if n>5:
print "n is greater than 5"
else:
print "n is not greater than 5"
Output:
In general, the block is defined as a set of statements in a program or code or script. The block of the code can contain only one statement or multiple statements declarations depending on the logic of the program or scripts. Any programming languages with this process of grouping these set of the statement(s) with the block is known as block-structured language. We can also notice that a block can have another block in it, such as a nested block structure. The block is considered one single statement by the interpreter while executing the program or script, which aids in reducing the lexical scope of variables and functions. In Python, this helps to indent the code, making other people easily understand and read the developer’s code.
So the working is very simple; all statements of a block of code with the same distance to the right belong to the same block, which means the statements inside this block line up vertically. So for the nested block structure, the indentation will be done in which the block will be simply indented more towards the right. Usually, the block ends at the line, which is less indented or if it is the end of the file. The nested block structure with proper indentation looks like as below:
BLOCK 1
BLOCK 2
BLOCK 3
BLOCK 2 (continuation)
BLOCK 1 (continuation)
In many different programming languages, indentation is used only for the code written to look better and neat, but in Python, indentation is used mainly to know which statement belongs to which block and accordingly, the interpreter will execute the Python code. Therefore unlike other programming languages, Python gives meaning to the indentation rule, which is very simple and also helps to make the code readable. In Python IDLE, it automatically indents the code; it takes the proper amount of spaces, and then you can write the code for which it will be indented. For example:
Example #3
Code:
n = 10
if n>5:
print "n is greater than 5"
For the above code, when you write the “if” statement and complete it by ending it with symbol double colon ( : ), when the interpreter reads this double colon, it automatically jumps to the next line with 4 spaces when you press enter after the double colon ( : ) then it automatically takes the print statement to the next line with 4 spaces at the beginning of the print statement. Hence the code will be indented by the Python IDLE. Sometimes after indenting, if we miss some space or add some extra space, it will throw an error, or the program would execute in unexpected behavior; therefore, the same block’s code or statements must be indented level. So we say Python structures by colons and indentation.
Indentation can be ignored during the line continuation of statements or lines. But it is not a good idea to ignore which the code doesn’t look neat and readable to others. Let us see the example of this:
Example #4
if True:
print "Welcome to Educba"
n = 10
In Python, the above code can also be written as
if True: print "Welcome back to Educba"; n =10
The output will be the same for both the codes, and they mean the same. But the first written code is more readable and easy to understand than the second code.
Output:
Conclusion
This article concludes that indentation in Python is very important than in any other programming language. As indentation in other programming languages is used only to make the code or script look neat and better, but in Python, indentation is must execute any block of the code as the interpreter notices these 4 spaces which are used for indenting the code and in another language they use flower brackets or braces. If the blocks of code or any program are not properly indented, then the code will throw an error such as an Indentation error in Python.
Recommended Articles
This is a guide to Indentation in Python. Here we discuss the Introduction and Role of Indentation in Python along with different examples and its code implementation. You may also have a look at the following articles to learn more –