Everything about Python indentation

Found this interesting blog that lists everything one needs to know about indentation in Python programming language.

For the same reason, Python needs indentation to make things easier to read.

First is the rule that indent is required after 11 keywords

Where to Indent:

You will need to indent after these 11 Python keywords:

  1. if, else, elif 
  2. for, while 
  3. def 
  4. class 
  5. try, except, finally  
  6. with 

Here are 5 Rules for indentation in Python programming language

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.

  1. price = 7 
  2. if price < 10: 
  3. print("I want the food") 

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

  1. price = 7 
  2. if price < 10: 
  3. print("I want the food") 

.

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 an indentation error:

  1. price = 7 
  2. if price < 10: 
  3. print("I want the food") 
  4. 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.

  1. a==1 
  2. b==2 
  3. c==3 
  4. print('start') 
  5. if a==1: 
  6. print(a) 
  7. if b==2: 
  8. print(b) 
  9. if c==3: 
  10. print(c) 
  11. 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.

  1. nums = [5,81,3,47,12,55,87] 
  2. for num in nums: 
  3. print(num) 
  4. if num % 2 is 0: 
  5. 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.

  1. nums = [5,81,3,47,12,55,87] 
  2. for num in nums: 
  3. if num % 2 is 0: 
  4. break 
  5. 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.

  1. a==1 
  2. b==3 

These rules and easier explanation is taken from Programming Hero


Comments

Popular posts from this blog

VMware fix for Invalid manifest and ova file import failed errors

SOAPUI - import certificate

Centrally Managed Users (CMU) - New Feature in Oracle Database 18c