Free visual Python lesson

Python for Loop Tutorial — Explained Visually

This beginner Python for loop tutorial shows how a for loop repeats a block of code once for every item in a sequence. Press Play below and watch Python do it, one step at a time.

Python code

1total = 0
2for i in range(1, 4):
3 total = total + i
4print(total)

Variables / Memory

Memory is empty.

Execution

STEP 1 / 9Step 1 of 9. Program starts.

Program starts.

Output

Flow chart

RanNowTrueFalseChecked, not taken◇ decision · ▱ input/output
StartStarttotal = 0total = 0next i in range(1, 4)?next i in range(1, …total = total + itotal = total + iprint(total)print(total)EndEndNEXT iDONE

How a for loop works

Python takes the first item from the sequence, stores it in the loop variable, runs the indented block, then moves to the next item. When there are no items left, the loop ends and Python continues with the code after it.

for i in range(3):
    print("Hello", i)
print("Done")

Output: Hello 0, Hello 1, Hello 2, then Done.

What range() gives you

  • range(5) → 0, 1, 2, 3, 4 (stops before 5)
  • range(2, 6) → 2, 3, 4, 5
  • range(0, 10, 2) → 0, 2, 4, 6, 8

Looping over a list or string

fruits = ["apple", "mango"]
for fruit in fruits:
    print(fruit)

for ch in "hi":
    print(ch)

Common beginner mistakes

  • Forgetting the colon : at the end of the for line.
  • Not indenting the loop body, so it runs only once (or causes an error).
  • Expecting range(5) to include 5 — it stops one before.

Practice exercises

Try each one yourself first, then open it in the Visualizer to watch every line run.

Exercise 1

Times table

Print the 7 times table from 7 x 1 to 7 x 10.

n = 7
for i in range(1, 11):
    # print one line
    pass
Hint

range(1, 11) gives 1 to 10. Print n, "x", i, "=", n * i.

Show solution and explanation
n = 7
for i in range(1, 11):
    print(n, "x", i, "=", n * i)

The loop body runs 10 times. i takes each value from range in turn while n stays fixed.

Watch the solution step by step

Exercise 2

Count the vowels

Count how many vowels are in a word.

word = "education"
count = 0
# loop over each letter
Hint

Use: if ch in "aeiou":

Show solution and explanation
word = "education"
count = 0
for ch in word:
    if ch in "aeiou":
        count = count + 1
print(count)  # 5

A for loop can walk through a string one letter at a time. count only grows when the condition is True.

Watch the solution step by step

Exercise 3

Biggest number

Find the largest number in a list without using max().

nums = [12, 45, 7, 30]
biggest = nums[0]
# compare each number
Hint

If x is greater than biggest, replace biggest.

Show solution and explanation
nums = [12, 45, 7, 30]
biggest = nums[0]
for x in nums:
    if x > biggest:
        biggest = x
print(biggest)  # 45

biggest starts as the first item and is only replaced when a larger one appears: 12 → 45, then 7 and 30 leave it unchanged.

Watch the solution step by step

Try your own loop

Paste any loop into the free visualizer, or sign up free for all 63 visual lessons with quizzes.