For loops let you repeat an action across a sequence of values without writing the same code over and over. This guide covers how Python’s for loop actually works, how to use the range() function, and the mistakes that trip up most beginners.
If you’ve worked through our guide on Python variables and data types, you’re ready for one of the most useful tools in the language: the ability to repeat an action automatically, rather than writing the same line of code five, ten, or a hundred times.
This guide covers Python’s for loop specifically — how it differs slightly from for loops in other languages, how the range() function fits in, and the small set of practical patterns that cover most everyday use.
What a For Loop Actually Does
A for loop repeats a block of code once for every item in a sequence — a list of names, the characters in a word, a range of numbers, or any other collection of values. Here’s the simplest possible example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)This prints each fruit on its own line. The variable fruit takes on the value of each item in the list, one at a time, and the indented block underneath runs once per item.
How Python’s For Loop Differs From Other Languages
If you’ve glanced at for loops in languages like Java or C, Python’s version looks noticeably simpler. According to Python’s official tutorial, Python’s for statement iterates over the items of any sequence directly, rather than requiring you to manually manage a counter variable, a condition, and an increment step the way many other languages do.
In practice, this means Python’s for loop is built around the idea of “for each item in this collection,” rather than “repeat this exact number of times using a counter I manage myself.” Both ideas exist in Python, but the collection-based approach is the default and most common pattern.
Using range() to Loop a Specific Number of Times
Sometimes you don’t have an existing list to loop through — you just want to repeat something a specific number of times. This is where the built-in range() function comes in:
for i in range(5):
print(i)This prints the numbers 0 through 4 — five values total, since range() starts at 0 by default and stops just before the number you give it. According to W3Schools’ guide to Python for loops, range() can also take a starting point and a step value: range(2, 10, 2) counts from 2 up to (but not including) 10, in steps of 2.
As freeCodeCamp’s tutorial on for loops puts it, the stop value passed to range() is the highest number you want, but it isn’t included — it’s just the stopping point, which is exactly why range(5) produces 0, 1, 2, 3, 4, and not 0 through 5.
Looping Through a String
Strings are sequences of characters in Python, which means you can loop through them the same way you’d loop through a list:
word = "Python"
for letter in word:
print(letter)This prints each letter of “Python” on its own line — a small but genuinely useful pattern when you need to examine or process text one character at a time.
Getting the Index Along With the Value
Sometimes you need to know both a value and its position while looping — for example, printing “Item 1: apple” instead of just “apple.” Python’s built-in enumerate() function handles this cleanly:
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)This prints each fruit alongside its position (starting from 0): “0 apple,” “1 banana,” “2 cherry.” Without enumerate(), getting the index would require manually tracking a separate counter variable — enumerate() handles that bookkeeping for you automatically.
Breaking Out Early With break and continue
Two keywords let you control a loop’s flow beyond simply running every iteration to completion. break stops the loop entirely, right away — useful when you’ve found what you were looking for and don’t need to keep checking the rest of the sequence:
numbers = [3, 7, 2, 9, 4]
for num in numbers:
if num == 2:
print("Found it!")
breakcontinue, by contrast, skips just the current iteration and moves on to the next one, without stopping the loop entirely:
for num in range(10):
if num % 2 == 0:
continue
print(num) # only prints odd numbersA helpful way to remember the difference: break means “stop everything,” while continue means “skip just this one and keep going.”
A Practical Example: Combining Loops With Earlier Concepts
Here’s a slightly larger example that ties together loops with the variables and data types covered earlier in this series:
scores = [85, 92, 78, 95, 60]
total = 0
for score in scores:
total = total + score
average = total / len(scores)
print(f"Average score: {average}")This loop adds up every value in the scores list, accumulating the total in a variable that starts at zero — a genuinely common pattern for summing, counting, or otherwise combining values across a sequence.
Nested Loops: A Loop Inside a Loop
Once the basic pattern feels comfortable, you’ll eventually run into situations that need a loop inside another loop — commonly called a nested loop. A typical use case is working through a grid or table-like structure, where an outer loop handles rows and an inner loop handles columns within each row:
for row in range(3):
for column in range(3):
print(f"({row}, {column})", end=" ")
print()This prints each coordinate pair in a 3×3 grid. The inner loop completes all of its iterations before the outer loop moves to its next value — worth keeping in mind, since nested loops can get confusing quickly if you lose track of which loop is currently running. As a beginner, it’s fine to treat this as an advanced topic to revisit once single loops feel completely natural.
The Most Common Beginner Mistakes
The single most common mistake is forgetting that range()‘s stop value is excluded. Expecting range(5) to include the number 5 is a near-universal early misunderstanding, and it’s worth simply memorizing: the stop value is always one past the last number actually produced.
Another common mistake is forgetting the colon at the end of the for line, or forgetting to indent the code underneath it. Python uses indentation to determine what’s inside the loop and what isn’t, so inconsistent indentation causes either an error or, more confusingly, code that silently runs outside the loop when you expected it to run inside.
# Incorrect - missing colon and indentation
for i in range(3)
print(i)
# Correct
for i in range(3):
print(i)Common Questions Beginners Ask About For Loops
What’s the difference between a for loop and a while loop? A for loop is built for iterating over a known sequence or a specific number of repetitions. A while loop repeats based on a condition being true, without necessarily knowing in advance how many times it will run — useful when the number of repetitions depends on something that happens during the loop itself.
Can I loop through a dictionary? Yes — Python lets you loop through a dictionary’s keys, values, or both together, though the exact syntax differs slightly from looping through a simple list, and is worth covering in a dedicated guide once you’re comfortable with the basics here.
What happens if the list I’m looping through is empty? The loop simply doesn’t run at all — there’s no error, the indented block underneath is just skipped entirely since there are no items to iterate over, and your program continues on to whatever comes after the loop.
Can I loop through two lists at the same time? Yes, using the built-in zip() function, which pairs up corresponding items from two or more sequences so you can loop through both together. This is a common next step once basic for loops feel comfortable.
Quick-Reference Python For Loop Guide
- for item in sequence: — the basic pattern for looping through any collection.
- range(stop) — generates numbers from 0 up to (not including) stop.
- range(start, stop, step) — full control over where the sequence begins, ends, and how it increments.
- Strings are loopable — a for loop can iterate character by character.
- Indentation matters — the loop’s body must be consistently indented underneath the for line.
- Empty sequences are safe — the loop simply doesn’t execute if there’s nothing to iterate over.
Conclusion
For loops are one of the most frequently used tools in Python, and the core pattern — for item in sequence: — covers the overwhelming majority of everyday use, whether you’re looping through a list, a range of numbers, or the characters in a string. Getting comfortable with range()‘s slightly counterintuitive stop-value behavior early on will save you from one of the most common beginner mix-ups, one that trips up nearly everyone at least once.
The best way to build real comfort here is direct practice: write a few small loops yourself — printing numbers, looping through a list of your own, or looping through a word character by character — rather than just reading through the examples above.
In the next guide in this series, we’ll cover Python if statements — how to make your code respond differently depending on a condition, which combines naturally with the loops covered here.
Explore More Python for Beginners Guides →

Alex Carter is a senior software developer with years of hands-on experience building real-world applications. After watching countless beginners give up on programming simply because most tutorials assumed too much prior knowledge, Alex started Vandutz Academy to do things differently — breaking every concept down into clear, judgment-free, step-by-step lessons. When not writing, Alex is probably debugging someone else’s code (or their own).