A for loop is one of the most commonly used loops in Python. It allows us to repeat a block of code for each item in a sequence or iterable.
For example, if we have a list of students, we can use a for loop to process each student without writing the same code again and again.
What is a for Loop?
A for loop is used to iterate over items in an iterable, such as a list, string, tuple, or range.
The basic syntax is:
for variable in iterable:
# code to executeFor example:
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)Output:
Apple
Banana
MangoPython takes each item from fruits, stores it in fruit, and executes the indented code.
Using for with range()
A very common use of for is with the range() function.
for i in range(5):
print(i)Output:
0
1
2
3
4Notice that range(5) starts from 0 and stops before 5.
You can specify a starting value as well:
for i in range(1, 6):
print(i)Output:
1
2
3
4
5Using a Step
range() can also accept a step value.
for i in range(2, 11, 2):
print(i)Output:
2
4
6
8
10Here, Python increases the number by 2 each time.
Looping Through a String
A string is also iterable, so you can loop through its characters.
word = "Python"
for character in word:
print(character)Output:
P
y
t
h
o
nThis can be useful when processing text character by character.
Using if Inside a for Loop
You can combine loops with conditions.
numbers = [1, 2, 3, 4, 5, 6]
for number in numbers:
if number % 2 == 0:
print(number)Output:
2
4
6Here, the loop checks every number, while the if statement prints only the even ones.
break in a for Loop
The break statement immediately stops the loop.
for number in range(1, 10):
if number == 5:
break
print(number)Output:
1
2
3
4As soon as number becomes 5, the loop stops.
continue in a for Loop
The continue statement skips the current iteration and moves to the next one.
for number in range(1, 6):
if number == 3:
continue
print(number)Output:
1
2
4
5The number 3 is skipped.
for Loop with else
Python also allows an else block with a for loop.
for i in range(3):
print(i)
else:
print("Loop finished")Output:
0
1
2
Loop finishedThe else block runs when the loop finishes normally.
If the loop is stopped using break, the else block does not run:
for i in range(5):
if i == 3:
break
print(i)
else:
print("Loop finished")Output:
0
1
2Nested for Loops
You can place one for loop inside another.
for i in range(1, 4):
for j in range(1, 4):
print(i, j)This is called a nested loop.
Nested loops are useful for working with things like tables, grids, and multidimensional data.
A Practical Example
Let's print a simple multiplication table:
number = 5
for i in range(1, 11):
print(f"{number} × {i} = {number * i}")Output:
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
...
5 × 10 = 50Instead of manually writing ten calculations, the for loop handles them automatically.
When Should You Use a for Loop?
Use a for loop when you want to iterate over something, such as:
A list of items
Characters in a string
A range of numbers
Tuple or set elements
Dictionary items
Other Python iterables
Once you get comfortable with for loops, you'll find them everywhere in Python. They're one of the main tools for processing collections and repeating tasks efficiently.