blog
blog

Python Indentation: Everything you need to know

As a beginner, indentation will bite you a little bit. Don’t worry...you're not the only one. Every Python developer faces it. As you keep coding, indentation confusion will go away...I promise!

Where to indent: You will need to indent after these 11 Python keywords:


if, else, elif
for, while
def
class
try, except, finally 
with

Indentation Rule-1:

One general rule (not always true, but good to remember as a beginner): If you use a colon sign at the end of a line, you will need to indent the following line/lines. In the code below, you have a colon after the second line. That’s why, the third line is indented.


price = 7
if price < 10:
    print("I want the food")

If you don’t indent the line after the colon, you will get an indentation error.


price = 7
if price < 10:
print("I want the food")

Why you need to Indent:

Did you notice how replies of a comment align?

If you can’t remember, look at the reply below...l
It goes a little right side than the actual comment. So, you can acknowledge that it’s a reply to that comment.

blog

All other comments don’t get indented. Only the reply to a comment indents. This is the way you can identify which one is a regular comment and which is a reply to a specific comment. Isn't it?

blog

The same thing is true in coding. If something is under something else, you will need to indent it.

Indentation Rule-2:

If you have multiple lines inside the if block, all the lines will need to be indented. And the indentation has to be the same.

For example, you want to indent the line 3 and 4. If you give four whitespaces on line 3, you have to give four whitespaces for line 4 as well.

In the code below, we gave four whitespaces for the first line and two whitespaces for the second line inside the if block. You will get indentation error:


price = 7
if price < 10:
    print("I want the food")
  print("I want the drinks")

Indentation Rule-3:

Regardless of your current level of indentation, if you write a colon at the end of the line, you will need to indent.

In the code below, you will see multiple levels of indentation. It can even go crazier 😅


a==1
b==2
c==3
print('start')
if a==1:
    print(a)
    if b==2:
        print(b)
        if c==3:
            print(c)
print('end')

Indentation Rule-4:

Indentation is determined by the depth/level of indentation, not by the line number.

Look at the code below. The third line of the code is under the for loop. The fourth line of the code has an if. Hence, the next line will be indented. And we have a break command there. So, if the number becomes an even number, loop will stop.


nums = [5,81,3,47,12,55,87]
for num in nums:
    print(num)
    if num % 2 is 0:
        break

Now, look at the code below. The print(num) line has the same indentation level as the if-condition. This means the print(num) is not inside the if block. Instead, it is still inside the for block. If you run the following code, you will see the number as output until it hits an even number.


nums = [5,81,3,47,12,55,87]
for num in nums:
    if num % 2 is 0:
        break
     print(num)

Indentation Rule-5:

If there is no colon, you should not indent the starting of a line.

In the code below, you didn’t write any specific Python keyword that requires indentation. However, you indent the second line of code. This will give you an Error. Hence, don’t indent if you don’t have any specific reason to do so.


a==1
    b==3

These rules and easier explanation is taken from Programming Hero. For more fun content like this, feel free to check out Programming Hero app.

Related Post

related post

8 Things You Must Know Before Learning Programming. Must-1: There is no perfect way to learn Some will say, watch YouTube tutorials. Others might say, read those books.

related post

If you spend 3–4 hours daily for 7 days, you will become a Junior Python Developer. I am providing this guideline using this android app called: Programming Hero.