Python for Loops Explained
A for loop in Python lets you repeat code once for each item in a group of values. It is one of the most useful tools for beginners because it makes it easy to work through lists, strings, dictionaries, and range() values without manually managing positions.
You will use for loops when you want to:
- print each item in a list
- process each character in a string
- repeat something a fixed number of times
- go through dictionary data one piece at a time
Quick example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
Use a for loop when you want to go through each item in a collection one by one.
What a for loop does
A for loop repeats a block of code once for each item in something iterable.
Common things you can loop over include:
- lists
- strings
- tuples
- sets
- dictionaries
range()objects
This is useful when you know you want to process items one by one. In many beginner cases, a for loop is simpler and safer than trying to track an index yourself.
Basic syntax
The basic pattern looks like this:
for item in collection:
print(item)
Here is what each part means:
forstarts the loopitemis a variable name for the current valueinconnects the variable to the collectioncollectionis the thing you are looping over- the indented block runs once on each loop
Indentation matters in Python. The code inside the loop must be indented.
For example:
for number in [1, 2, 3]:
print(number)
Output:
1
2
3
You can read this as:
For each
numberin[1, 2, 3], print that number.
If indentation is confusing, see Python indentation rules and why they matter.
Looping through a list
Lists are one of the most common things to loop over.
colors = ["red", "green", "blue"]
for color in colors:
print(color)
Output:
red
green
blue
What happens here:
- On the first loop,
coloris"red" - On the second loop,
coloris"green" - On the third loop,
coloris"blue"
A good way to read this is:
For each
colorincolors
That mental pattern makes loops much easier to understand.
If you want more list-specific examples, see how to loop through a list in Python.
Using range() with for loops
Use range() when you want to repeat something a fixed number of times.
for number in range(5):
print(number)
Output:
0
1
2
3
4
range(5) gives numbers from 0 up to, but not including, 5.
You can also use this form:
for number in range(2, 10, 2):
print(number)
Output:
2
4
6
8
This uses:
start:2stop:10step:2
So the full pattern is:
range(start, stop, step)
For a full explanation, see Python range() function explained.
Looping through strings and dictionaries
Looping through a string
A string loops one character at a time.
word = "cat"
for letter in word:
print(letter)
Output:
c
a
t
Looping through a dictionary
A dictionary loops through its keys by default.
person = {
"name": "Maya",
"age": 12
}
for key in person:
print(key)
Output:
name
age
If you want both keys and values, you can use items():
person = {
"name": "Maya",
"age": 12
}
for key, value in person.items():
print(key, value)
Output:
name Maya
age 12
Keep it simple at first:
- looping over a string gives characters
- looping over a dictionary gives keys by default
Using break and continue
break
break stops the loop early.
for number in range(10):
if number == 4:
break
print(number)
Output:
0
1
2
3
As soon as number becomes 4, the loop ends.
continue
continue skips the current loop and moves to the next one.
for number in range(5):
if number == 2:
continue
print(number)
Output:
0
1
3
4
When number is 2, print(number) is skipped.
For a full guide, see Python break and continue statements.
Using else with a for loop
A for loop can have an else block.
The else runs if the loop finishes normally.
for number in range(3):
print(number)
else:
print("Loop finished")
Output:
0
1
2
Loop finished
But if break happens, the else block does not run.
for number in range(3):
if number == 1:
break
print(number)
else:
print("Loop finished")
Output:
0
This feature is real and useful sometimes, but many beginners do not need it right away.
Common beginner mistakes
Here are some common problems with for loops.
Forgetting the colon
This causes a SyntaxError.
Wrong:
for item in [1, 2, 3]
print(item)
Correct:
for item in [1, 2, 3]:
print(item)
Using bad indentation
The code inside the loop must be indented.
Wrong:
for item in [1, 2, 3]:
print(item)
Correct:
for item in [1, 2, 3]:
print(item)
If you get this error, see IndentationError: expected an indented block.
Expecting range(5) to include 5
It does not include 5.
for number in range(5):
print(number)
Output:
0
1
2
3
4
Changing the loop variable and expecting the original collection to change
This does not change the original list:
numbers = [1, 2, 3]
for number in numbers:
number = 99
print(numbers)
Output:
[1, 2, 3]
The loop variable number is just a temporary name for each value.
Confusing values with indexes
This loop gives values, not index positions:
animals = ["cat", "dog", "bird"]
for animal in animals:
print(animal)
If you need both the index and the value, use enumerate(). See how to use enumerate in Python.
When to use a for loop vs while loop
Use a for loop when:
- you are looping over items
- you are looping over a known range
- you know how many times something should happen
Use a while loop when:
- you want to keep going until a condition becomes false
- you do not know in advance how many times the loop will run
Example idea:
for: go through every item in a listwhile: keep asking for input until the user enters"quit"
For a full comparison, see Python while loops explained.
Debugging for loop problems
If your loop is not working as expected, these quick checks can help.
Check the current value
print(item)
This helps you see what the loop variable contains on each iteration.
Check the type of the thing you are looping over
print(type(collection))
This is useful if you are not sure whether your variable is a list, string, dictionary, or something else.
See what range(5) really contains
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
Check dictionary keys
print(my_dict.keys())
This shows the keys in a dictionary.
Read help for range()
help(range)
This can show how range() works directly in Python.
Common errors related to loops include:
SyntaxErrorfrom a missing colon afterforIndentationErrorfrom missing or inconsistent indentationTypeErrorwhen trying to loop over a non-iterable value like an integerNameErrorfrom using a variable that was never defined
If you see a non-iterable error, see TypeError: int object is not iterable.
FAQ
What does a for loop do in Python?
It runs a block of code once for each item in an iterable such as a list, string, or range.
What can I loop over in Python?
You can loop over lists, tuples, strings, sets, dictionaries, and range() objects.
Does range(5) include 5?
No. It gives 0, 1, 2, 3, and 4.
What is the difference between for and while in Python?
A for loop is usually for going through items. A while loop repeats while a condition is true.
How do I stop a for loop early?
Use break to exit the loop before it finishes.
See also
- Python while loops explained
- Python break and continue statements
- Python range() function explained
- How to loop through a list in Python
- How to use enumerate in Python
- Python indentation rules and why they matter
Once you understand the basic for item in collection: pattern, you can use loops for many real Python tasks like printing values, counting with range(), and processing data one item at a time.