Indentation in Python (original) (raw)
Last Updated : 27 May, 2026
Indentation is used to organise and group statements into blocks of code. Unlike many other programming languages, Python uses indentation instead of braces {} to define code structure.
- Statements with the same indentation belong to the same block of code.
- Indentation is created using spaces or tabs, but four spaces are commonly preferred.
- Inconsistent indentation causes an IndentationError. Python `
print("I have no Indentation ") print("I have tab Indentation ")
`
**Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 2
print("I have tab Indentation ")
IndentationError: unexpected indent
**Explanation:
- First print statement has no indentation, so it is correctly executed.
- Second print statement has tab indentation, but it doesn't belong to a new block of code, that's why it throws IndentationError.
Indentation in Conditional Statements
All statements in a conditional block should have same alignment.

Indentation in Python
The code below demonstrate how we use indentation to define seperate scopes of if-else statements:
Python `
a = 20
if a >= 18: print('GeeksforGeeks...') else: print('retype the URL.') print('All set !')
`
Output
GeeksforGeeks... All set !
**Explanation:
- Statements inside if and else are indented, which makes them part of their respective blocks.
- Since a is greater than or equal to 18, the if block executes.
- The last print() statement is outside the if-else block, so it runs every time.
Indentation in Loops
Indentation defines the set of statements that are executed repeatedly inside a loop.
Python `
j = 1
while(j<= 5): print(j) j = j + 1
`
**Explanation: Both print(j) and j = j + 1 are indented, so they are part of the loop block. These statements keep executing until the condition j <= 5 becomes False.