Understanding Python Indentation: A Holistic Approach

How does Python indent as a whole? Indentation is a very important concept in Python because if you don’t indent your Python code properly, you’ll end up seeing and the code won’t be compiled.IndentationError 

Indentation: How does Python indent overall?

In a nutshell, indentation refers to adding a space before a statement. But the question arises, is it necessary?
To understand this, consider a situation where you are reading a book and suddenly all the page numbers in the book are missing. So you don’t know where to continue reading, you will be confused. The situation is similar to Python. Without indentation, Python doesn’t know which statement to execute next or which statement belongs to which block. This will lead to.IndentationError

In the example above,

  • The statement (line 1), the if condition (line 2), and the statement (the last line) belong to the same block, which means that after statement 1 will be executed. If this happens, then Python will jump to the last statement execution.if conditionif conditionFalse 
  • Nesting belongs to block 2, which means that if it becomes False, Python will execute the statement in the condition.if-elsenested ifelse 
  • Nested internal statements belong to block 3 and only one statement is executed according to the condition.if-elseif-else

How does Python indent holistically? Python indentation is a way to tell the Python interpreter that a group of statements belongs to a specific block of code. A block is a combination of all these statements. Blocks can be thought of as groupings of statements for a specific purpose. Most programming languages like C, C++, Java use curly braces to define blocks of code. Python uses indentation to highlight blocks of code. Spaces are used for indentation in Python. All statements with the same distance to the right belong to the same code block. If the block has to be nested more deeply, you can simply indent it to the right. You can understand it better by looking at the following lines of code.{ }

Python Global Indentation Example #1:

# Python program showing 
# indentation 
    
site = 'gfg'
    
if site == 'gfg': 
    print('Logging on to lsbin...') 
else: 
    print('retype the URL.') 
print('All set !') 

Output:

Logging on to lsbin...
All set !

The sum of lines is two separate blocks of code. Both blocks of code in our example if statement are indented by four spaces. final is not indented, so it doesn’t belong in the else block.print(‘Logging on to lsbin…’)print(‘retype the URL.’)print(‘All set!’)

Python Overall Indentation Example #2:

j = 1
  
while(j<= 5): 
     print(j) 
     j = j + 1

Output:

1
2
3
4
5

How does Python indent holistically? To indicate a block of code in Python, you must indent each line of the block with the same space. Both lines of code in the loop are indented by four spaces. Used to indicate which block of code the statement belongs to. For example, and is not indented, so it’s not inside the block. So, the Python code structure is indented.while j=1while(j<=5):while 

Note: How does Python indent the whole thing? Python uses 4 spaces as indentations by default. However, the number of spaces is up to you, but you must use at least 1 space.