Python Indentation and Error Example


Python Indentation and Error Example

Python Indentation is just some whitespaces at the starting of the code or a block of statements to be executed. In python, this feature is very important and we must include indentation. We can use up to 4 whitespaces in the python code but must have to give at least one indentation i.e. one whitespace. If you do not include indentation at the beginning of the code then it will throw us an error.

Also read, C Program to Find Factorial of a Number with Example

Let us take some examples below one by one

Indentation Example

a = 10
b = 9
if(a>b):
   print("a is greater than b")
   print("value of a is 10")
else:
   print("b is less than a")
   print("value of b is 11")

Output:-

a is greater than b
value of a is 10

Python Indentation Error Example

a = 10
b = 9
if(a>b):
print("a is greater than b")
   print("value of a is 10")
else:
   print("b is less than a")
   print("value of b is 11")

Output:-

File “d:\python\python_programming\01_python.py”, line 5

print(“a is greater than b”)

^

IndentationError: expected an indented block after ‘if’ statement on line 4

Also, note that you must use the same indentation inside the same piece of code or block. Suppose if you are using the if condition for a block of statements then you have to use the same number of whitespaces in every line of code to be executed otherwise it will throw an error.

Conclusion:- I hope this tutorial will help you to understand the overview of indentation. If there is any doubt then please leave a comment below. Also, you can down python’s latest version from here


Leave a Comment