python
03 How to use Indentation in Python (Video in Khmer)
Learn Python indentation with simple example and tips. Short and clear. Good for beginner!
2026-10-18 ยท 3 min read
Hello friend! ๐ In Python, space at start of line is very important. Wrong space = error!
In this post, I show you how to use indentation in Python. Easy and short. ๐
๐ฅ Video: How to use Indentation in Python (Khmer)
๐ What is Indentation?
Indentation is the space at the start of code line. Other language use { } for group code. Python use space instead.
Indentation tell Python: "This code is inside this block."
โ Example 1: if statement
age = 18
if age >= 18:
print("You can vote")
print("Welcome!")
print("Done")
- Line
print("You can vote")andprint("Welcome!")have 4 space โ insideif. - Line
print("Done")no space โ outsideif, always run.
โ Example 2: for loop
for i in range(3):
print("Number:", i)
Output:
Number: 0
Number: 1
Number: 2
โ Example 3: function
def say_hello(name):
print("Hello", name)
print("Nice to meet you")
say_hello("Dara")
โ Example 4: nested block (block inside block)
score = 85
if score >= 50:
print("Pass")
if score >= 80:
print("Very good!")
else:
print("Fail")
Each level add 4 more space.
โ Common Error
if True:
print("Hello")
Error:
IndentationError: expected an indented block
Fix: add 4 space before print.
if True:
print("Hello")
Another error โ space not same:
if True:
print("One")
print("Two")
Error: IndentationError: unexpected indent. Make both line same space.
๐ก Tips
- Use 4 space for one level. This is Python standard.
- Don't mix Tab and Space. Pick one (space is best).
- In VS Code, press
Tabkey โ it auto add 4 space. - Always put
:at end ofif,for,while,def,class. - Code in same block must have same indentation.
- Error
IndentationError? Check space first! - Turn on "Render Whitespace" in VS Code to see the space.
โ Any Question?
Tell us in comment below! ๐
#Python #Indentation #LearnPython #Beginner