Indentation in Python

Indentation

Indentation is a very important concept of Python because without proper indentation the Python code will end up with IndentationError and the code will not get compiled. 

Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code. A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose

Example:

  • def add():
    • #one tab space is preferd as indention generally.
indentation in python

Indentation : 

  • It simply means adding whitespace before statement.
  • 1 Tab space is considered as one indentation.
  • 1 Tab space is equal to 4 white space.
  • Genrally , after every ‘:’ we use Indentation.

Example  #1:

#indendation 
p=‘PrepInsta’
if(p==‘PrepInsta’):
    print(‘Hello Prepsters we are here to help you.’
Output:

Hello Prepsters we are here to help you.

Example #2:

 #without indentation

p=‘PrepInsta’

if(p==‘PrepInsta’):
print(‘Hello Prepsters we are here to help you.’)
Output:

  File "main.py", line 4

    print('Hello Prepsters we are here to help you.') 

        ^

IndentationError: expected an indented block