Python Indentation

Indentation is a simple Idea of spacing or use of white spaces at the beginning of any line in code. Lets try to understand indentation with example –

Below mentioned is a simple If else code. With help of indentation we can see our code as a collection of Block of Code or Codeblock. Here If(line 1) and else(line 3) belong to 1st block of code as zero number of spaces are used in the beginning of these lines. Line 2 and 4 belong to 2nd block of code.

After executing the code we didn’t received any error. But now if I try to remove the space from the beginning of line 2, then lets see what happens

Here we received a Indentation Error for line 2. Now lets keep it simple, just 2 lines in the code as mentioned below. Here at least one white space is required to avoid Indentation Error for line 2, means 1 or more than one white space can also help you to avoid Indentation Errors in such cases.

As expected it doesn’t result in Indentation Error.

Leave a Reply

Your email address will not be published. Required fields are marked *