How to Write a for Loop in Python
What you’ll build or solve
You’ll write for loops that iterate over lists, strings, and ranges, and you’ll control the loop with clear, readable patterns.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Process each item in a list, like names, prices, or tasks.
- Repeat an action a set number of times, like 10 retries or 12 months.
- Walk through text one character at a time, like counting letters.
Avoid this approach when:
- You need to repeat until a condition changes. A
whileloop fits better. - You need heavy numeric loops for performance. Vectorized tools may be faster.
Prerequisites
- Python 3 installed
- You know what variables are
- You know what a list is
- You can run a short Python script
Step-by-step instructions
1) Loop through a list
Use for item in my_list: when you want each element.
CSS
names= ["Ana","Bo","Chen"]
fornameinnames:
print(name)
Python takes each value from names and assigns it to name one by one.
2) Loop through a string
A string is iterable, so the loop gives you one character at a time.
word="mimo"
forchinword:
print(ch)
Each character becomes the loop variable ch.
3) Loop a fixed number of times with range()
Use range(n) to loop from 0 up to n - 1.
foriinrange(3):
print(i)
If you want 1 to 3 instead:
foriinrange(1,4):
print(i)
What to look for:
The end value is not included.
4) Get both index and value with enumerate()
Use this when you need positions and items.
colors= ["red","green","blue"]
forindex,colorinenumerate(colors):
print(index,color)
If you want the counting to start at 1:
forindex,colorinenumerate(colors,start=1):
print(index,color)
5) Loop through a dictionary
Use .items() to loop through key-value pairs.
prices= {"apple":1.2,"banana":0.8}
foritem,priceinprices.items():
print(item,price)
If you only need keys, use for key in prices:.
If you only need values, use for value in prices.values():.
6) Add simple loop control with break and continue
These statements work in any loop.
numbers= [2,5,0,7]
forninnumbers:
ifn==0:
continue
ifn==7:
break
print(n)
continueskips to the next iteration.breakstops the loop entirely.
Examples you can copy
1) Sum numbers in a list
nums= [3,5,2]
total=0
forninnums:
total+=n
print(total)
2) Find the first matching item
names= ["Ana","Bo","Chen","Dina"]
target="Chen"
found=False
fornameinnames:
ifname==target:
found=True
break
print(found)
3) Build a new list from an old one
CSS
scores= [55,70,49,88]
passing= []
forsinscores:
ifs>=60:
passing.append(s)
print(passing)
Common mistakes and how to fix them
Mistake 1: Forgetting the colon or indentation
You might write:
names= ["Ana","Bo"]
fornameinnames
print(name)
Why it breaks:
Python requires : after the loop header and indentation for the loop body.
Correct approach:
CSS
names= ["Ana","Bo"]
fornameinnames:
print(name)
Mistake 2: Changing the list while looping over it
You might write:
nums= [1,2,3,4]
forninnums:
ifn%2==0:
nums.remove(n)
print(nums)
Why it breaks:
Removing items shifts indexes, so the loop can skip elements.
Correct approach:
Build a new list instead.
CSS
nums= [1,2,3,4]
kept= []
forninnums:
ifn%2!=0:
kept.append(n)
print(kept)
Mistake 3: Off-by-one errors with range()
You might write:
foriinrange(1,3):
print(i)
Why it breaks:
The end value is excluded, so this prints 1 and 2, not 3.
Correct approach:
foriinrange(1,4):
print(i)
Troubleshooting
If you see IndentationError, make sure the loop body is indented consistently, either all spaces or all tabs.
If you see SyntaxError on the for line, check for a missing colon : at the end.
If your loop never stops, confirm you did not accidentally use a while loop. A for loop ends when the iterable ends.
If items seem skipped, check if you remove from the same list you are looping over. Build a new list instead.
Quick recap
- Use
for item in items:to loop through a list. - Use
for ch in text:to loop through characters in a string. - Use
range()for a fixed number of repeats. - Use
enumerate()for index plus value. - Use
.items()to loop through dictionary keys and values. - Use
breakto stop early andcontinueto skip items.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot