dev.rean.me
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

Share:

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") and print("Welcome!") have 4 space โ†’ inside if.
  • Line print("Done") no space โ†’ outside if, 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 Tab key โ€” it auto add 4 space.
  • Always put : at end of if, 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


๐Ÿ“š Related Post

โ† Back to python
Share: