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:
- if, else, elif
- for, while
- def
- class
- try, except, finally
- 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.
- 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")
.
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:
- 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.
Comments
Post a Comment