What is Indentation in Python
Indentation is the leading whitespace ( spaces and tabs ) before any statement in python. Indentation in other languages like c, c++, etc is just for readability but in python, the indentation is a very important and mandatory concept that should be followed when writing a python code, otherwise, IndentationError is thrown by the python interpreter.
The reason why indentation is important in python is because the indentation serves another purpose other than code readability. Python treats the statements that have the same indentation level (statements that have equal no of whitespaces before them) as a single block of code. 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.
Below are some of the observations that can be made from the above figure:
- All the statements which are on the same level of Indentation(Same no of whitespaces before them) belong to a single block, so from the above diagram, statements in line 1, line 2, and line 7 belong to a single block and the block has the zero or lowest level of indentation. Statements 3 and 5 are indented one step, hence they together form another block at the first level of indentation. Similarly, statements 4 and 6 are indented two steps, so they together form another block at the second level of indentation.
- Below the line 2 statement which is an if statement, statements 3 and 5 are indented one step hence they both belong to a single block. And since line 2 is an if statement, the block indented below the if forms the body of if. So here the body of the if statement at line 2 includes all the lines that are indented below it i.e., lines 3,4,5, and 6.
- Now that we know that statement at line numbers 3,4,5, and 6 forms the body of the if statement at line 2. Let us understand the indentation for them. Statements at 3 and 5 are uniformly indented so they belong to a single block (block2 from the interpretation) and they will be executed one by one.
- Statement at line 4 makes up the body of the if statement at line 3, as we know any statements that are indented below an if form the body of if statement, similarity the statement at line 6 makes up the body of else statement at line 5.
- This is how the indentation helps defines the blocks and also to identify to which statements the block belongs.
- The execution starts at line 1 followed by the statement at line 2, the if condition is evaluated and in case it returns true, then control goes inside the body of the if statement, which brings statements 3,4, 5, and 6 to the picture.
- Now, the statement at line 3 is executed and the if condition is evaluated, in case it returns true then line 4 is executed after which control goes to line 7. In case the if condition at line 3 returns false, then control goes to else statement that is the line 5, and then line 6 is executed, followed by the statement at line 7.
- In case if condition at line number 2 returns false, the control skips lines 3, 4, 5, and 6, and goes to the statement at line 7.
Comments
Leave a comment