Indentation Error in Python
Before understanding the indentation problem in python, let us look at the brief about the python programming language.
What makes python different from other programming languages?
- Python is an interpreted, high-level and object-oriented programming language along with having dynamic semantics.
- It has a very simple syntax, and it is very easy to implement compared with the other programming languages.
- Using python, programmers can focus on logic building rather than remembering the syntax or facing a problem to implement the logic in coding.
- It is a dynamic and smart high-level language and is most widely used among various users.
- We compare the python programming language syntax with other programming languages, like C, C++, Java, and many more that have followed the particular syntax. For every block of code, there are opening and closing braces.
- But in python, we will follow the proper rule of indentation.
- Indentation refers to the proper participation of white space while starting a particular block of code.
- It is not too much complicated and not more focused on the syntax.
- It is much faster and efficient than the other programming languages.
- Python code can be easily implemented and easily written, and easily be executable.
Now let’s discuss the concept of indentation in python.
What is an indentation in Python Programming Language?
- Indentation refers to implementing proper spaces and tabs at starting any statement, method and block of code in the python programming language.
- Due to these indentations, we can easily identify the beginning point and at the endpoint of any conditional loop, functions, if-else statements and so on.
- Suppose we observe the other programming languages like C, C++, and Java. In that case, there is the use of small and curly braces along with the semicolon for specifying the particular block of code and identifying the particular statement.
- But here in python, we will not be concerned about using the semicolon and different braces in syntax.
- There is no importance of spaces in C. Other programming languages or the compiler will ignore all the whitespace and comments, in the lexical phase itself, in the starting phase; hence, it cannot generate syntax errors.
- The role of all whitespaces matters; if a block of code is starting by applying a white space, it must end on the same indent.
- All statements must have the same number of whitespaces or tabs so that the distance to the left of the screen belongs to the same block of code. If a block has to be more deeply nested, it is indented further to the right.
- Suppose we observe in the python programming language. In that case, there is no concept of the main function, as we compared it with other programming languages. They all have a concept of the main function, which is executed after then other functions. If the main function calls the other function, the pointer will point to execute the other functions and again return to the main function.
- Due to the indentation, it is decided that the initial indented block is the body of the main function. If there are other functions, then the initial indented block will call that function. The indentation of the additional function is also different from the main body so that the compiler can distinguish the block of the main and derived function.
Some basic Indentation Rules that are used in Python programming Language
- There is a proper indentation for a particular block of code that must be maintained while beginning and ending any code block.
- A programmer can split the indentation into multiple lines by using a backslash.
- If any block where proper indentation is not maintained, the python compile will throw the indentation error.
- Being consistent in following the indentation rule is quite confusing for a programmer if the code is too large or contains lots of lines of code.
- But indentation helps us implement the code using simple syntax, rather than applying brackets for every block of code, as we do in other programming languages.
- Not only it is easy for us, but also it saves our time and reduces the lines of code; hence ultimately, speed increases and execution tile reduces.
- The number of whitespace for a particular block of code is already fixed. If we use nested conditional statements, then the number of whitespaces has increased, according to the rule of sub-sub statements.
How do indentation errors Occur in Python?
If we are writing a certain code of lines based on some logic to get the desired result, but, meanwhile, we are following the above rules of indentation. The compiler will generate an indentation error.
Let us understand with the help of an example –
In the above figure, if we observe that it is mentioned how python recognises every block of code.
As we will notice in this figure, the indentation of every block is the calculation of whitespaces from the left corner to the statement began. Whenever the particular block of code starts, it will follow the indentation. At first, it will maintain the proper whitespaces and tabs, whenever the block starts and until it ends. If in the case inside that particular block includes a nested block, it will assign whitespaces to it till that nested block ends.
In this way, in the python programming language the indentation of a block matches.
An indentation error occurs whenever there is no ending of the particular block, or the number of whitespaces assigned for starting the code block is not the same. At the same time that blocks ends, ultimately, an indentation error occurs.
Description of the above figure –
- In the above figure, statement one will be executed. Then, the pointer will point to the ‘ if statement ‘ and check the condition; if the condition is true, it will enter the block and enter the nested if-else block.
- Again in the nested if statement, it will check the condition if it is true, then executes the next statement.
- In this way, the execution of the statements will be carried on, now; if we take a case where the block fails, it will not enter into the nested if-else block. It directly exit out form all the blocks and the final statement will execute, which matches with the block 1, initial block.
Now let us see the figure having indentation error –
The figure mentioned above clearly has an indentation error because the block 2 indentation does not match the starting and ending point whitespaces. The beginning of block 2 will contain 0 whitespaces, but while it ends, it contains 2 whitespaces which will produce an error.
Now a doubt arises that, if block 2 below block 3 can act as block 4 a nested block, then it will not generate an indentation error, then the answer of this doubt is no because block 4; a nested block below block 3 will have more whitespaces as compare to the block 3. After all, it is nested inside block 3. And the second reason is that the python interpreter will find the end of block 2, but it will be unable to detect the matching whitespaces from block 2; hence it will generate an indentation error.
How to fix indentation error in a python programming language?
There are points that one must remember while fixing the indentation error :
As we have seen earlier, indentation error occurs only due to the unmatched whitespaces and tabs; hence, we need to fix all the whitespaces and tabs.
- Firstly you need to find the error in a particular line of code.
- After that, check the number of whitespaces in that line and detect the block of code in which that line lies.
- Match the indentation of that block with the ending and starting point of that block by calculating the number of whitesces used in the starting and at the ending of that block.
- Still, if the error is not resolved, match the indentation of every block of code with another corresponding block of code by using the same way calculating the whitespaces and tabs used in it.
- Also, you take the help of the editor that you are using to check the number of whitespaces and tabs used, and you can easily detect from it by finding out the unnecessary whitespaces used.
Advantages of Indentation in Python
- Indentation is used in python to represent a certain block of code, but in other programming languages, they refer to various brackets. Due to indentation, the code looks more efficient and beautifully structured.
- Indentation rules used in a python programming language are very simple; even a programmer wants their code to be readable.
- Also, indentation increases the efficiency and readability of the code by structuring it beautifully.
Disadvantages of Indentation in Python
- Due to the uses of whitespaces in indentation, sometimes it is a very tough task to fix the indentation error when there are many lines of code.
- The various popular programming languages like C, C++, Java use braces for indentation, so anybody coming from the other side of the developed world finds it hard to adjust to the idea of using whitespaces for the indentation.
Examples of Python Indentation Error:
Example 1:
The output of the above program:
IndentationError: expected an indented block
Example 2:
The output of the above program:
IndentationError: expected an indented block
Example 3:
The output of the above program:
IndentationError: expected an indented block
Now Fixing the Indentation Error in the above examples of python Programs :
Example 1:
The output of the above program:
Example 2:
The output of the above program:
Example 3:
The output of the above program:
|