If you’re learning Python, one of the first concepts you need to understand is the for loop.
A for loop allows you to repeat actions efficiently without writing the same code multiple times.
Instead of manually repeating tasks, you can automate them using loops—making your code cleaner, faster, and more powerful.
In this guide, you’ll learn how Python for loops work, with practical examples you can start using immediately.
What Is a For Loop in Python?
A for loop is used to iterate over a sequence of elements.
A sequence can be:
- A list
- A string
- A tuple
- A range of numbers
Basic Syntax
for item in sequence:
# code to execute
Example
numbers = [1, 2, 3, 4]
for num in numbers:
print(num)
Output
1
2
3
4
Explanation
numbersis the sequencenumrepresents each item- The loop runs once for each item
Using range() in For Loops
The range() function is commonly used with for loops.
Example
for i in range(5):
print(i)
Output
0
1
2
3
4
How It Works
range(5)generates numbers from 0 to 4- The loop runs 5 times
Custom Range
for i in range(1, 6):
print(i)
Output
1
2
3
4
5
Looping Through Strings
You can loop through each character in a string.
Example
name = "Fimi"
for letter in name:
print(letter)
Output
F
i
m
i
Looping Through Dictionaries
Dictionaries store key-value pairs.
Example
person = {"name": "Fimi", "age": 25}for key, value in person.items():
print(key, value)
Output
name Fimi
age 25
Using break and continue
break
Stops the loop completely.
for i in range(5):
if i == 3:
break
print(i)
Output
0
1
2
continue
Skips the current iteration.
for i in range(5):
if i == 2:
continue
print(i)
Output
0
1
3
4
Nested For Loops
A loop inside another loop.
Example
for i in range(3):
for j in range(2):
print(i, j)
Output
0 0
0 1
1 0
1 1
2 0
2 1
Use Case
Useful for:
- Working with matrices
- Comparing combinations
Looping with enumerate()
enumerate() gives both index and value.
Example
names = ["John", "Jane", "Mike"]for index, name in enumerate(names):
print(index, name)
Output
0 John
1 Jane
2 Mike
Looping with zip()
zip() combines multiple sequences.
Example
names = ["John", "Jane"]
scores = [80, 90]for name, score in zip(names, scores):
print(name, score)
Output
John 80
Jane 90
List Comprehension (Advanced Shortcut)
List comprehension is a shorter way to write loops.
Example
numbers = [1, 2, 3, 4]squared = [x**2 for x in numbers]
print(squared)
Output
[1, 4, 9, 16]
Real-World Use Cases
For loops are used in:
- Data analysis (looping through rows)
- Automation scripts
- Processing datasets
- Generating reports
Common Mistakes to Avoid
1. Incorrect Indentation
Python relies on proper indentation.
2. Modifying List While Looping
Avoid changing a list inside a loop directly.
3. Infinite Loops
Be careful with loop conditions.
Best Practices
- Use meaningful variable names
- Keep loops simple
- Avoid unnecessary nested loops
- Use list comprehension when possible
For loops are one of the most fundamental concepts in Python programming.
They allow you to automate repetitive tasks and work efficiently with data.
Once you master for loops, you’ll find it much easier to handle real-world data problems and write cleaner code.
Practice regularly with different examples, and you’ll quickly become comfortable using loops in your projects.
FAQs
What is a for loop in Python?
It is used to iterate over a sequence of items.
What does range() do?
It generates a sequence of numbers.
What is the difference between break and continue?
Break stops the loop, continue skips an iteration.
Can I loop through a dictionary?
Yes, using .items().
What is list comprehension?
A shorter way to create lists using loops.