Does python rely on indentation?

Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

Python will give you an error if you skip the indentation:

The number of spaces is up to you as a programmer, but it has to be at least one.

Example

if 5 > 2:
 print["Five is greater than two!"] 
if 5 > 2:
        print["Five is greater than two!"] 

Try it Yourself »

You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:

Example

Syntax Error:

if 5 > 2:
 print["Five is greater than two!"]
        print["Five is greater than two!"]

Try it Yourself »



Learn about Indentation in Python.

Overview

Indentation in Python Programming is simply the spaces at the beginning of a code line. Indentation in other languages like c, c++, etc., is just for readability, but in Python, the indentation is an essential and mandatory concept that should be followed when writing a python code; otherwise, the python interpreter throws IndentationError.

To understand this topic, you should have some knowledge of the following Python Programming topics:

  • Python Syntax
  • Comments in Python
  • Scope of Variable in Python

Scope of article

  • In this topic, we are going to learn about Indentation in Python.
  • We will learn a few rules on indentation with some example codes.
  • Then, we will discuss some advantages and disadvantages of indentation.

What is Indentation in Python

Indentation is the leading whitespace [ spaces and tabs ] before any statement in Python. The reason why indentation is important in python is that the indentation serves another purpose other than code readability. Python treats the statements with the same indentation level [statements with an equal number of whitespaces before them] as a single code block. So whereas in languages like c, c++, etc. a block of code is represented by Curly braces { }, in python a block is a group of statements that have the same Indentation level i.e same number of leading whitespaces.

Below are some of the observations that can be made from the above figure:
  • All the statements which are on the same level of Indentation[Same no of whitespaces before them] belong to a single block, so from the above diagram, statements in line 1, line 2, and line 7 belong to a single block, and the block has the zero or lowest level of indentation. Statements 3 and 5 are indented one step, forming another block at the first level of indentation. Similarly, statements 4 and 6 are indented two steps, so they together form another block at the second level of indentation.
  • Below the line 2 statement, which is an if statement, statements 3 and 5 are indented one step; hence, they belong to a single block. And since line 2 is an if statement, the block indented below the if forms the body of if. So here, the body of the if statement at line 2 includes all the lines that are indented below it, i.e., lines 3,4,5 and 6.
  • Now that we know that statement at line numbers 3,4,5 and 6 forms the body of the if statement at line 2. Let us understand the indentation for them. Statements at 3 and 5 are uniformly indented, so they belong to a single block [block2 from the interpretation], and they will be executed one by one.
  • Statement at line 4 makes up the body of the if statement at line 3, as we know any statements that are indented below an if form the body of if statement, similarity the statement at line 6 makes up the body of else statement at line 5.
  • This is how the indentation helps define the blocks and also to identify to which statements the block belongs.
  • The execution starts at line 1 followed by the statement at line 2; if the condition is evaluated and in case it returns true, then control goes inside the body of the if statement, which brings statements 3,4, 5, and 6 to the picture.
    • Now, the statement at line 3 is executed, and if the condition is evaluated, in case it returns true, then line 4 is executed, after which control goes to line 7. If the condition at line 3 returns false, then control goes to another statement that is line 5, and then line 6 is executed, followed by the statement at line 7.
  • In case condition at line number 2 returns false, the control skips lines 3, 4, 5, and 6 and goes to the statement at line 7.

Examples

Example 1: Below is an example code snippet with the correct indentation in python.

name = 'Rahul'
  
if name == 'Rahul':
   print['WelCome Rahul..']
   print['How are you?']
else:
   print['Dude! whoever you are ']
   print['Why you here?']
 
print['Have a great day!']

Output:

WelCome Rahul..
How are you?
Have a great day!

Explanation:

  • The name variable gets assigned to Rahul in the first statement
  • Now the statement if name == ‘Rahul’: is evaluated, it returns true, so it executes the body of the if, which is the indented next two statements below the if statement. The two statements inside the body are print[‘WelCome Rahul..’] and print[‘How are you?’] and they get executed.
  • Once the statement gets executed the else part is skipped and control goes to the next statement which is print[‘Have a great day!’], which is executed.
  • In this program, the statements inside the bodies of if and else are indented.

Example 2: Below is an example code snippet with correct indentation.

i = 1
while[i 

Chủ Đề