For Loop with Range in Python- Programming Guide
What the Hell Is a For Loop with Range?
You've seen the code. You've copied it from Stack Overflow. Now you actually need to understand it.
A for loop repeats code a set number of times. range() generates that sequence of numbers. Together, they're how you tell Python "do this thing X times."
That's it. No magic. No mystery.
The Basic Syntax
Here's the simplest version:
for i in range(5):
print(i)
Output:
0
1
2
3
4
Notice something? Python starts counting at zero, not one. This trips up beginners constantly. The loop ran 5 times, but printed 0 through 4.
Understanding Range Parameters
Range takes up to three arguments. Most people only know the first one.
range(stop)
Generates 0 up to (but not including) stop.
range(10) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
range(start, stop)
Generates start up to (but not including) stop.
range(2, 8) # 2, 3, 4, 5, 6, 7
range(start, stop, step)
Generates numbers with a custom increment. Step can be negative for reverse counting.
range(0, 10, 2) # 0, 2, 4, 6, 8
range(10, 0, -1) # 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Range Parameters Cheat Sheet
| Syntax | What It Does | Example Output |
|---|---|---|
range(n) |
0 to n-1 | 0, 1, 2, 3, 4 |
range(start, stop) |
start to stop-1 | 5, 6, 7, 8, 9 |
range(start, stop, step) |
start to stop-1, incrementing by step | 0, 3, 6, 9 |
range(stop, 0, -1) |
stop-1 down to 0 | 5, 4, 3, 2, 1 |
Real-World Examples
Iterating a Specific Number of Times
for _ in range(3):
print("Printing this three times")
Using _ instead of i signals "I don't actually need the counter value."
Accessing List Items by Index
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")
Output:
0: apple
1: banana
2: cherry
Honest take: Python pros usually use enumerate() instead. But range(len()) still works fine.
Counting Down
for i in range(5, 0, -1):
print(f"Launch in {i}...")
Output:
Launch in 5...
Launch in 4...
Launch in 3...
Launch in 2...
Launch in 1...
Common Mistakes
- Off-by-one errors: range(10) gives you 0-9, not 1-10. Always double-check your boundaries.
- Forgetting step sign: range(10, 0) returns nothing. You need range(10, 0, -1) to count down.
- Modifying the list while iterating: Don't do this. Ever. Use a copy or list comprehension instead.
- Using range when you don't need it: To just loop through a list, use
for item in mylist:without range.
How To: Get Started Right Now
Open a Python interpreter and try these:
- Print numbers 1-10:
for i in range(1, 11): print(i) - Print only even numbers 0-20:
for i in range(0, 21, 2): print(i) - Build a simple multiplication table:
n = 5 for i in range(1, 11): print(f"{n} x {i} = {n * i}")
When to Use Alternatives
Range isn't always the answer. Here's when to use something else:
- Loop through items directly: Use
for item in items: - Need index AND item: Use
for i, item in enumerate(items): - Loop until condition: Use a while loop instead
- Repeat indefinitely: Use
while True:with a break condition
The Bottom Line
For loops with range are fundamental. You need to know them cold. Start with the basics, practice the parameter combinations, and you'll have this down in an hour.
No excuses.