SkilDock
All Python tutorialsFree interactive tutorial

Python For Loop — Tutorial with Examples

Learn the Python for loop the way real developers use it — with enumerate, zip, and range. Includes a runnable browser editor.

Try it in your browser

Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.

Python's for loop is a for-each loop, not a counter loop. It reads "for each item in this iterable, do this."

for name in ["Alice", "Bob", "Carol"]:
    print(f"Hello, {name}")

This is fundamentally different from C / Java's for (int i = 0; i < n; i++). Python doesn't think in terms of counters — it thinks in terms of *iterables*.

What you can loop over

Anything that's an iterable: lists, tuples, strings, sets, dicts, ranges, file objects, generators. Strings iterate character by character. Files iterate line by line.

for c in "hello":         # 'h', 'e', 'l', 'l', 'o'
    print(c)

for line in open("notes.txt"):   # one line at a time
    print(line.strip())

When you genuinely need an index — range()

range(5)        # 0, 1, 2, 3, 4
range(2, 8)     # 2, 3, 4, 5, 6, 7
range(0, 10, 2) # 0, 2, 4, 6, 8 (step 2)

Use range() when you need to count — not when you have a collection to iterate.

The three iteration patterns every Python dev uses

1. enumerate() — when you need index + value

Instead of:

for i in range(len(names)):
    print(i, names[i])

Write:

for i, name in enumerate(names):
    print(i, name)

Cleaner, faster, more Pythonic. Pass start=1 if you want one-based numbering.

2. zip() — when you need parallel iteration

for name, score in zip(names, scores):
    print(f"{name}: {score}")

Stops at the shorter iterable. Use itertools.zip_longest if you want it to fill the shorter one.

3. .items() — when looping a dict

for key, value in my_dict.items():
    print(f"{key} = {value}")

The else clause — Python's surprising feature

for item in items:
    if item == target:
        print("Found!")
        break
else:
    print("Not found")

The else block runs only if the loop completed without break. Useful for search-and-fallback logic, and a classic interview trivia question.

Common mistakes

  • for i in range(len(xs)) when enumerate would do. Pure code smell.
  • Modifying a list while iterating it. Build a new list instead, or use a list comprehension.
  • Forgetting range() end is exclusive. range(10) is 0..9, not 0..10.

Where this fits in the 7-Day Python Sprint

Loops are introduced on Day 2 alongside conditionals and comprehensions. By the end of Day 2, you'll be writing FizzBuzz and a CLI calculator that loops menu prompts.

Practice

Auto-graded — run in your browser, no signup.

Exercise 1: Build `evens` as a list of even numbers from 0 to 19 using a `for` loop and an `if` check (no comprehension this time).

Try it in your browser

Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.

Like this tutorial? Get the full Python Sprint.

7 days, daily videos, auto-graded labs, AI tutor, mini-project. 1-year content access.

See the Python Sprint

Related tutorials