Indentation is an important concept to understand in Python, if you are not indenting your Python code properly, you will end up getting the IndentationError and your code will not compile.

Indentation

An indentation refers to adding white space/ space bar before a statement, is it really necessary?‌‌ Well, imagine you are reading a book and all the page numbers suddenly disappeared. You now have no idea of where you have read to, and you will get confused, you can think of this as a book reader would expect that a book would have page numbers. Python has similar expectations when it comes to indenting your code properly. If you don't properly indent your code, Python would not know which statement to execute next or which statement belongs to which block. When you try to run your code, you will get IndentationError.

Fig 1

indentation fig 1

In fig 1 we can see,

  • Statement (line 1), if condition (line 2), and statement (last line) belongs to the same block which means that after statement 1, if condition will be executed. And suppose the if condition becomes, False then the Python will jump to the last statement for execution.
  • The nested if-else belongs to block 2 which means that if nested if becomes False, then Python will execute the statements inside the else condition.
  • Statements inside nested if-else belongs to block 3 and only one statement will be executed depending on the if-else condition.

Python uses indentation as a way of telling the Python interpreter where each group of statements belongs for a particular block of code. A block is a combination of all these statements. Blocks can be thought of as the grouping of statements for a specific purpose. Most programming languages like C, C++, Java use braces { } to define a block of code. Instead of braces, Python uses indentation to highlight the different blocks of code. White spaces are used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code.

Example #1:

The lines print(‘Logging on to techiemike…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, so it doesn't belong to the else-block.

Example #2:

So to indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=5): is not indented, and so it is not within while block. So, Python code structures by indentation.

Note: By default, Python uses 4 spaces for indentation. However, the number of spaces you use is up to you, but there is a minimum of 1 space.

Techie Mike
Techie Mike
Self-taught techie, with a passion for computers and all the cool things you can do with them. Techie Mike, B.Eng. B.Sc.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Techie Mike - The IT guy in Thailand.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.