dev.rean.me
python

11 How to Use For Loop in Python + Tips (Video in Khmer)

Learn Python for loop with list, string and range. Simple example, exercise and tips. Good for beginner!

2026-10-22 ยท 4 min read

Share:

Hello friend! ๐Ÿ‘‹ Loop mean code run many time. for loop is good for go through each item one by one. List, string, range โ€” all can use with for.

In this post, I show you for loop in Python. Easy and short. ๐Ÿ


๐Ÿ” For Loop with List

for take one item from list, then run code. Next item, run again. Until list finish.

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

Output:

apple
banana
orange

fruit is new variable name. You can change it to any name. Remember: put : after for, and indent the code inside (4 space).

๐ŸŽฅ Video: Using For Loop in Python (Khmer)


๐Ÿ”ค For Loop with String

String is also a list of letter. So for can go one letter by one letter.

word = "Python"

for letter in word:
    print(letter)

Output:

P
y
t
h
o
n

๐Ÿ”ข For Loop with range()

range() make list of number for you. Easy for repeat many time.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Careful: range(5) start from 0 and stop before 5. So 5 is not include.

Want start from 1? Give 2 number: range(start, stop).

for i in range(1, 6):
    print(i)     # 1 2 3 4 5

Want jump? Give 3 number: range(start, stop, step).

for i in range(0, 11, 2):
    print(i)     # 0 2 4 6 8 10

๐ŸŽฅ Video: Using Range in For Loop in Python (Khmer)


โœ๏ธ Exercise with Range

Sum from 1 to 10

Add all number from 1 to 10. Use variable total to save result.

total = 0

for n in range(1, 11):
    total += n

print("Total:", total)   # Total: 55

Multiplication table

number = 3

for i in range(1, 11):
    print(number, "x", i, "=", number * i)

Try this: even number

Print even number from 1 to 20. Hint: % give remainder. n % 2 == 0 mean even.

for n in range(1, 21):
    if n % 2 == 0:
        print(n, "is even")

๐ŸŽฅ Video: Using Range Function in For Loop with Exercise (Khmer)


โŒ Common Error

Forget ::

for fruit in fruits     # SyntaxError
    print(fruit)

Fix: for fruit in fruits:

Forget indent:

for fruit in fruits:
print(fruit)            # IndentationError

Use number directly, not range():

for i in 5:             # TypeError: 'int' object is not iterable
    print(i)

Fix: for i in range(5):

Think range(5) include 5:

for i in range(5):
    print(i)            # 0 1 2 3 4, not 5!

๐Ÿ’ก Tips

  • Use name that easy to read: for fruit in fruits: is better than for x in y:.
  • range(n) start from 0 and stop before n.
  • range(start, stop, step) โ€” step can be negative: range(5, 0, -1) โ†’ 5 4 3 2 1.
  • Don't need the value? Use _: for _ in range(3): print("Hi").
  • Want index and item? Use range(len(fruits)): for i in range(len(fruits)): print(i, fruits[i])
  • Use break to stop loop early. Use continue to skip one round.
  • Use print(i, end=" ") to show many value in one line.
  • Don't change list size while you loop over it. Make new list instead.

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #ForLoop #Range #Loop #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: