for Loops and range()

Python 3.14 · ✓ verified by execution on 2026-07-22

The for loop in Python iterates over the items of any sequence.

python
words = ['cat', 'window', 'defenestrate']
for w in words:
    print(w)
Output
cat
window
defenestrate

The range() Function

If you do need to iterate over a sequence of numbers, the built-in function, range() is the tool you need.

python
for i in range(3):
    print(i)
Output
0
1
2

Remember: The given end point is never part of the generated sequence. Thus, range(3) yields 0, 1, 2 but not 3.

Start, Stop, and Step

You can also choose where to start and how big each step is. The full form is range(start, stop, step) — start counting at start, stop before stop, and jump by step each time.

python
for i in range(2, 8, 2):
    print(i)
Output
2
4
6
start = 2 · step = 2 · stop = 8 (excluded) 0 1 2 3 4 5 6 7 8
range(2, 8, 2) starts at 2, counts by 2, and stops before 8 — so 8 is never produced.

Try It Yourself

Drag the slider to change how many rows this loop prints, then press Run. Notice that range(n) always starts at 0, so n is exactly how many times the loop body runs.

python
drag to change the value, then Run
n = 5
for i in range(n):
    print('*' * (i + 1))
Output
*
**
***
****
*****

Counting Backwards

You can even use a negative step to count backwards:

python
for i in range(0, -3, -1):
    print(i)
Output
0
-1
-2

Iterating with Indices

If you need both the index and the item, you can iterate over the indices of a sequence:

python
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print(i, a[i])
Output
0 Mary
1 had
2 a
3 little
4 lamb

Common Misconceptions

A for loop in Python is like C where you define the initialization, condition, and increment. Python’s for loop iterates over the items of any sequence in the order they appear.

range(5) generates numbers from 1 to 5. range(5) generates numbers from 0 to 4 (the end point is exclusive).

Edge Cases

Under the Hood

Watch how the for loop dynamically assigns the next value from range() to the iteration variable on each cycle:

python · visualize
for i in range(2, 8, 2):
    print("Value:", i)
print("Loop finished")

Check yourself

What numbers will `range(3)` generate?

Reveal answer

0, 1, 2 — range(stop) starts at 0 by default and ends before the stop value, making the end point exclusive.

How does Python's `for` loop differ from traditional C-style for loops?

Reveal answer

It directly iterates over the items of a sequence, rather than just counting numbers. — Python's for loop iterates over the items of any iterable (like a list or string) in the order they appear.

What does `range(2, 8, 2)` generate?

Reveal answer

2, 4, 6 — It starts at 2, increments by 2 (the step), and stops before it reaches 8.

Challenges

Challenge 1 +50 XP

Use a for loop and the `range()` function to print the numbers 1 through 5 (inclusive).

python
  • Test 1 — expects "1\n2\n3\n4\n5"
Need a hint? (−25% XP)

Use range(1, 6) since the stop value is exclusive.

Show solution (0 XP)
for i in range(1, 6):
    print(i)

Challenge 2 +100 XP

Iterate over the list `colors` and print each item.

python
  • Test 1 — expects "red\ngreen\nblue"
Need a hint? (−25% XP)

Use `for color in colors:`.

Show solution (0 XP)
colors = ['red', 'green', 'blue']
for color in colors:
    print(color)