Tuesday 20 September 2022

How Indentation works in Python

 Lets say we want to write a python program to check whether a variable has a value 10 or not.


Code : 

var = 10

if (var == 10):

print(' A is 10')


Error :

File "<ipython-input-53-61659dc01be1>", line 3

    print(' A is 10')
    ^
IndentationError: expected an indented block

>> As we can see above we get the above error. This means the print statement is not as per expected indentation. Lets see if put it in the right way like below :

var = 10
if (var == 10):
  print(' A is 10')

In above code i try to shift/move the print statement a bit towards right side with a couple of spaces it could also be possible with tab and it works fine now.

 A is 10
In [ ]:

It prints the expected statement now as above we can see the actual cell is displayed .


No comments:

Post a Comment