A for loop runs its body once for each item drawn from an iterable (list, string, tuple, dict keys, set, range, and more). The Python tutorial describes this as assigning each value to the loop variable in turn.
When you only need a single yes/no over items, any() / all() with a generator can replace a manual flag loop—see Python any() and all().
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Python for loop syntax
for item in iterable:
# body (indented; colon required)The header ends with a colon. The body must be indented consistently. item can be any name; iterable is any object that produces values when iterated.
Simple Python for loop example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)You should see apple, banana, and cherry on separate lines. That is the pattern: each iteration assigns the next element to fruit, then runs the body.
Python for loop with list
Lists are the most common iterable. For more list operations, see Python list.
nums = [10, 20, 30]
total = 0
for n in nums:
total += n
print(total)You should see 60.
Python for loop with string
A string iterates character by character.
for ch in "hi":
print(ch)You should see h then i.
Python for loop with range()
In Python 3, range returns a range object, not a list. It yields integers one at a time in loops and avoids storing every number in memory. For start, stop, step, and reverse counting, see Python range(). The stop value is exclusive: range(3) yields 0, 1, 2.
for number in range(3):
print(number)You should see 0, 1, 2. With two bounds, range(2, 5) starts at 2 and stops before 5.
for n in range(2, 5):
print(n)You should see 2, 3, 4.
Python for loop with dictionary
Looping for key in my_dict walks keys. Use .items() when you need both key and value.
scores = {"Ada": 100, "Bob": 85}
for name in scores:
print(name, scores[name])
for name, value in scores.items():
print(name, value)Each pair of lines should list the same entries in insertion order. For dict basics, see Python dictionary example.
Python for loop with enumerate()
When you need an index and the value, use enumerate instead of range(len(...)) when it reads more clearly.
words = ["a", "bb"]
for i, w in enumerate(words):
print(i, w)You should see 0 a then 1 bb. For more patterns, see Python enumerate().
Common iterables (quick table)
| Iterable | Typical loop |
|---|---|
| List | for item in items: |
| String | for char in text: |
| Tuple | for item in values: |
| Dictionary | for key in data: or for k, v in data.items(): |
| Set | for item in unique_values: |
| Range | for number in range(5): |
Nested for loop in Python
Each outer iteration runs the full inner loop. Here each inner value is squared correctly:
for i in range(2, 4):
print(i, "squared is", i * i)You should see 2 squared is 4 then 3 squared is 9.
for i in range(1, 3):
for j in range(1, 3):
print(i, j, i * j)You should see four lines of small products (1 1 1 through 2 2 4).
Python for loop with break
break leaves the innermost for (or while) immediately.
for n in range(10):
if n == 3:
break
print(n)You should see 0, 1, 2. For more control-flow detail, see Python break statement.
Python for loop with continue
continue skips the rest of the current iteration and jumps to the next item.
for n in range(4):
if n == 2:
continue
print(n)You should see 0, 1, 3. See also Python continue.
Python for loop with pass
pass is a no-op placeholder—useful when syntax requires a body but you have not written logic yet.
for n in range(2):
pass
print("done")You should see done.
Python for loop with else
The else clause on a for loop runs after the loop completes all iterations without break. If break runs, the else block is skipped. This is different from if / else: think “else means no break occurred.”
for n in [1, 2, 3]:
if n == 10:
break
else:
print("no break")
for n in [1, 2, 3]:
if n == 2:
break
else:
pass # not executed because break ranYou should see only no break from the first loop; the second loop ends with break, so its else is skipped.
Common mistakes with Python for loops
- Omitting the colon at the end of the
forline (SyntaxError). - Inconsistent indentation inside the loop body (
IndentationErroror wrong logic). - Expecting
range(1, 5)to include5—the stop index is always excluded. - Using
range(len(items))everywhere whenenumerate(items)would be clearer. - Modifying a list while iterating over that same list (skips or duplicates elements).
- Mixing up
break(exit loop),continue(next iteration), andpass(do nothing). - Treating
for/elselikeif/else—rememberelseties tobreak, not truthiness of the last value. - Writing
for x in 3:—integers are not iterable (TypeError).
Python for loop quick reference table
| Need | Pattern |
|---|---|
| Walk a sequence | for x in seq: |
| Index + value | for i, x in enumerate(seq): |
| Count with range | for i in range(n): |
| Dict keys | for k in d: |
| Dict key/value | for k, v in d.items(): |
| Stop early | break |
| Skip one iteration | continue |
| Placeholder body | pass |
Run if no break |
for ...: ... else: |
Summary
Python’s for item in iterable: runs the body once per element from any iterable. Use range when you need numeric progress (remember the exclusive stop), use .items() on dicts when you need values, and prefer enumerate when an index helps readability. break, continue, and pass control flow inside the loop; for / else runs only when the loop finishes without break. When the stop rule depends on a changing condition rather than a fixed sequence, a while loop may read more clearly; branch inside either loop with if / else.

