Skip to content

Indentation in Python

Indentation in Python

Indentation in Python is like page number in a book . As without page number we can not understand what page to read next , without proper indentation Python can not understand what line to compile next . If our python code is not indented perfectly then it will not compile and give us IndentationError .

Indentation means whitespaces at the beginning of a line to define a block of code . In other language (such as c , c++ etc ) we use {} to define a block of code . All the code written between {} will be treated as a block of code . But in python all the code written continuously with same indentation will be treated as a block of code .

Example 1: Program without indentation

# python code without indentation
a=20
b=15
if a>b:
print("a>b")    
print("a is greater than b")
else:
print("b>a")    
print("b is greater than a") 
  File "C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10792/1344091706.py", line 5
    print("a>b")
    ^
IndentationError: expected an indented block

Note :

if statement in Python can not be empty . In the above code as next line after if statement is not indented with if statement so , Python treats if block as empty and raise an IndentationError .

Example 2: Program with default (4 whitespaces) indentation

# python code with default indentation
a=20
b=15
if a>b:
    print("a>b")    
    print("a is greater than b")
else:
    print("b>a")    
    print("b is greater than a") 
a>b
a is greater than b
  • In the above code we have seen a properly indented Python code which is compiled successfully .
  • We can see that line1 , lin2 , line3 , line4 , line7 start at same position so , Python will treat these line as a single block . It means first line1 will be compiled ,then line2 , then line3 , then line4(if statement) and then line7( if condition in if statement becomes False ) will be compiled .
  • We can also see that line5 and line6 is indented by 4 whitespaces . So , both the line will be treated as a block by Python and both the line belongs to line4 (if statement) . If condition in if statement becomes True then both the line will be compiled .
  • We can also see that line8 and line9 is indented by 4 whitespaces . So , both the line will be treated as a block by Python and both the line belongs to line7 (else statement) . If condition in if statement becomes False then both the line will be compiled .

Note :

By default Python maintains indentation as 4 whitespaces or 1 tab . But we can use any indentation (such as -1 whitespace ,2 whitespaces etc ) . Only we need to remember that indentation should be same through out the block otherwise it will give error .

Example 1: Program with 3 whitespaces indentation

# python code with indentation as 
# 3 whitespaces through out the code
a=20
b=15
if a>b:
   print("a>b")    
   print("a is greater than b")
else:
   print("b>a")    
   print("b is greater than a") 
a>b
a is greater than b

In the above code we have used indentation as 3 whitespaces through out the code . So our code is compiled successfully by Python .

Example 2: Program with different indentation in different block

# python code with indentation as 
# 3 whitespaces and 2 whitespaces
# but indentation is same in a block
a=20
b=15
if a>b:
   print("a>b")    
   print("a is greater than b")
else:
  print("b>a")    
  print("b is greater than a") 
a>b
a is greater than b

In the above code we have used indentation as 3 whitespaces in if block and 2 whitespaces in else block . As all the line in a single block is indented by same amount so our code is compiled successfully by Python .

Though in this situation code is compiled successfully but it is not advisable to use different indentation at different block as the code becomes unreadable .

Example 3: Program with different indentation in same block

# python code with indentation as 
# 3 whitespaces and 4 whitespaces
# but indentation is different in if block
a=20
b=15
if a>b:
   print("a>b")    
    print("a is greater than b")
else:
   print("b>a")    
   print("b is greater than a") 
  File "C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10792/22737292.py", line 6
    print("a is greater than b")
    ^
IndentationError: unexpected indent

In the above code we have used indentation as 3 whitespaces and 4 whitespaces . As both the line in if block is indented by different amount so our code is not compiled successfully by Python and gave us IndentationError .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply