Python For Loops
1. What is a For Loop?
A loop is a block of code that executes multiple times. A for loop repeats for a defined number of iterations.
for i in range(10):
print(i) # prints 0 through 9
2. The range() Function
Controls how many times and in what way the loop runs.
Single argument — stop only
for i in range(10): # 0, 1, 2 ... 9
Two arguments — start and stop
for i in range(5, 20): # 5, 6, 7 ... 19
Three arguments — start, stop, step
for i in range(5, 20, 2): # 5, 7, 9 ... 19
Important: The stop value is never included — the loop ends as soon as
ireaches it.
Negative step (counting down)
for i in range(10, -20, -5): # 10, 5, 0, -5, -10, -15
Summary
| Argument | Role | Default |
|---|---|---|
start |
First value of i |
0 |
stop |
Loop ends when i reaches this |
required |
step |
Amount added to i each iteration |
1 |
3. Iterating Through Collections
Lists, tuples, and strings are iterable — you can loop directly through them.
Method 1 — By index (using range + len)
lst = [1, 2, 3, 4, 5]
for i in range(len(lst)):
print(lst[i])
✅ Have access to the index. ⚠️ More verbose.
Method 2 — By item (direct iteration)
for element in lst:
print(element)
✅ Clean and simple. ❌ No access to the index.
Method 3 — By index AND item (enumerate)
for i, element in enumerate(lst):
print(i, element)
✅ Access to both index and element. ✅ Preferred when you need both.
Works the same way for tuples and strings.
4. Practical Example — Summing Numbers
result = 0
for i in range(1, 11): # 1 through 10 (stop at 11 to include 10)
result += i # same as: result = result + i
print(result) # → 55
5. break and continue
break — stop the loop immediately
lst = [1, 2, 3, 3, 4, 4, 1, 2]
for num in lst:
if num == 4:
break # exits the loop as soon as 4 is seen
print(num)
# Output: 1 2 3 3
continue — skip the current iteration
for num in lst:
if num == 4:
continue # skips printing 4, keeps looping
print(num)
# Output: 1 2 3 3 1 2
| Keyword | Effect |
|---|---|
break |
Exits the loop entirely |
continue |
Skips to the next iteration |
6. Nested For Loops
A for loop inside another for loop. The inner loop runs completely for each iteration of the outer loop.
for i in range(10):
for j in range(10):
print(j)
# print runs 10 × 10 = 100 times
⚠️ Name iterator variables differently (e.g.
i,j,w) to avoid confusion.
Iterating through a nested list
lst = [[1, 2], [3, 4], [5, 6], [7, 8]]
for i in range(len(lst)):
interior = lst[i]
for j in range(len(interior)):
print(interior[j])
# Output: 1 2 3 4 5 6 7 8
7. Common Mistake — Declaring Variables Inside the Loop
# ❌ WRONG — list gets reset on every iteration
for i in range(3):
numbers = [] # resets to empty each time!
numbers.append(int(input("Enter a number: ")))
# ✅ CORRECT — declare the list BEFORE the loop
numbers = []
for i in range(3):
numbers.append(int(input("Enter a number: ")))
print(numbers)
8. The pass Keyword
A placeholder that does nothing — prevents errors when a loop body is empty.
for i in range(10):
pass # valid, does nothing
Without
pass, an empty loop body causes anIndentationError.
9. For-Else Statement
The else block runs only if the loop was NOT exited with break.
words = ("hello", "name", "world")
target = "name"
for word in words:
if word == target:
print("Found the word!")
break
else:
print("Didn't find the word.")
breakencountered →elseblock is skipped- Loop finishes normally →
elseblock runs
This removes the need for a separate
found = Falseboolean flag.
Cheat Sheet
# Basic loop
for i in range(10): # 0–9
for i in range(2, 10): # 2–9
for i in range(0, 10, 2): # 0, 2, 4, 6, 8
# Iterating collections
for element in lst: # by item
for i in range(len(lst)): # by index
for i, element in enumerate(lst): # both
# Loop control
break # exit loop immediately
continue # skip to next iteration
pass # do nothing (placeholder)
# For-else
for x in collection:
if condition:
break
else:
# runs only if break was never hit