Python for Loop

The Python for loop is one of the most fundamental tools you will use as a Python programmer. Whenever you need to go through a collection of items — a list of names, a range of numbers, characters in a word — the Python for loop is how you do it. It lets you repeat a block of code once for each item in a sequence without writing the same line over and over again. In this guide you will learn exactly how the Python for loop works, see it applied to every common Python data type, and build up to a complete working example by the end.

Python for Loop Syntax

The basic syntax of a Python for loop is simple and readable. You write the keyword for, then a variable name that will hold each item one at a time, then in, then the sequence you want to iterate over, followed by a colon. The code block underneath is indented and runs once per item.

for variable in sequence:
    # code to run for each item

The variable name you choose is entirely up to you. Python assigns each item in the sequence to that variable automatically on each pass through the loop. You do not need to manage an index counter yourself — Python takes care of it.

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
apple
banana
cherry

Python reads the list, assigns "apple" to fruit on the first pass, runs the indented block, then moves to "banana" and repeats until there are no more items.

Iterating Over a List

A list is the most common thing you will iterate over with a Python for loop. You can loop through a list of strings, numbers, or any mix of values.

scores = [85, 92, 78, 95, 60]

for score in scores:
    print("Score:", score)
Score: 85
Score: 92
Score: 78
Score: 95
Score: 60

You can also do calculations inside the loop. Here each score gets a 5-point bonus applied and printed:

scores = [85, 92, 78, 95, 60]

for score in scores:
    print("Adjusted score:", score + 5)
Adjusted score: 90
Adjusted score: 97
Adjusted score: 83
Adjusted score: 100
Adjusted score: 65

The original list is not changed — the loop just reads each item and uses it in the expression.

Iterating Over a String

Strings in Python are sequences of characters, so the Python for loop can go through each character one at a time. This is useful when you need to inspect, count, or transform individual characters.

word = "Python"

for char in word:
    print(char)
P
y
t
h
o
n

You could use this to count how many vowels are in a word:

word = "programming"
vowel_count = 0

for char in word:
    if char in "aeiou":
        vowel_count += 1

print("Vowels found:", vowel_count)
Vowels found: 3

Python for Loop with range()

The range() function generates a sequence of numbers on the fly. It is one of the most common companions to the Python for loop when you need to repeat something a specific number of times or work with numeric indexes.

Calling range(n) gives you the numbers from 0 up to but not including n.

for i in range(5):
    print(i)
0
1
2
3
4

You can also give range() a start and stop value. range(start, stop) goes from start up to but not including stop.

for i in range(2, 8):
    print(i)
2
3
4
5
6
7

The third argument to range() is the step — how much to increment by on each pass. You can use it to count by twos, fives, or even count backwards with a negative step.

for i in range(0, 20, 4):
    print(i)
0
4
8
12
16

Counting backwards:

for i in range(10, 0, -2):
    print(i)
10
8
6
4
2

Iterating Over a Tuple

A tuple works exactly like a list when you iterate over it with a Python for loop. The only difference is that a tuple is immutable — its contents cannot be changed — but the loop reads through each element just the same.

colors = ("red", "green", "blue")

for color in colors:
    print(color)
red
green
blue

Tuples of pairs are especially handy. Each iteration can unpack both values directly into two variables:

coordinates = ((1, 2), (3, 4), (5, 6))

for x, y in coordinates:
    print("x:", x, "y:", y)
x: 1 y: 2
x: 3 y: 4
x: 5 y: 6

Python automatically unpacks each inner tuple into x and y on every pass.

Iterating Over a Dictionary

When you use a Python for loop on a dictionary, it iterates over the keys by default. To access both keys and values at the same time, call the .items() method on the dictionary.

Iterating over keys only:

student = {"name": "Alice", "age": 21, "grade": "A"}

for key in student:
    print(key)
name
age
grade

Iterating over keys and values together using .items():

student = {"name": "Alice", "age": 21, "grade": "A"}

for key, value in student.items():
    print(key, "->", value)
name -> Alice
age -> 21
grade -> A

If you only need the values, use .values():

prices = {"apple": 1.20, "banana": 0.50, "cherry": 2.00}

for price in prices.values():
    print(price)
1.2
0.5
2.0

Using enumerate() with a Python for Loop

The built-in enumerate() function adds an automatic counter to your for loop. Instead of just getting each item, you get both the index position and the item on every pass. This is much cleaner than manually tracking a counter variable.

languages = ["Python", "JavaScript", "Rust", "Go"]

for index, language in enumerate(languages):
    print(index, language)
0 Python
1 JavaScript
2 Rust
3 Go

By default the counter starts at 0, but you can pass a starting number as the second argument to enumerate():

languages = ["Python", "JavaScript", "Rust", "Go"]

for index, language in enumerate(languages, start=1):
    print(index, language)
1 Python
2 JavaScript
3 Rust
4 Go

This is ideal for producing numbered lists, tracking positions, or when you need the index alongside the value without resorting to range(len(...)).

Using zip() to Iterate Over Multiple Lists

The built-in zip() function lets you iterate over two or more sequences in parallel inside a single Python for loop. It pairs up items from each sequence by position and stops when the shortest sequence runs out.

names = ["Alice", "Bob", "Carol"]
scores = [88, 74, 95]

for name, score in zip(names, scores):
    print(name, "scored", score)
Alice scored 88
Bob scored 74
Carol scored 95

You can zip three lists at once the same way:

cities = ["Paris", "Tokyo", "Cairo"]
countries = ["France", "Japan", "Egypt"]
populations = [2161000, 13960000, 21323000]

for city, country, pop in zip(cities, countries, populations):
    print(city, "is in", country, "with population", pop)
Paris is in France with population 2161000
Tokyo is in Japan with population 13960000
Cairo is in Egypt with population 21323000

Nested Python for Loops

A nested for loop is a for loop placed inside another for loop. The inner loop runs completely through all its iterations for every single iteration of the outer loop. This is useful for working with grids, matrices, combinations, or any structure that has rows and columns.

for row in range(1, 4):
    for col in range(1, 4):
        print(row, col)
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

A classic use of nested for loops is building a multiplication table:

for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end="\t")
    print()
1	2	3	
2	4	6	
3	6	9	

The end="\t" argument stops print() from adding a newline after each number, and the inner print() at the end of the outer loop moves to a new line after each row is complete.

You can also nest a for loop over a list of lists, which comes up often when processing table data:

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

for row in matrix:
    for value in row:
        print(value, end=" ")
    print()
1 2 3 
4 5 6 
7 8 9 

Full Working Example

This example brings together everything covered — iterating over a list, using range(), enumerate(), zip(), and a nested loop — in a student grade report program.

students = ["Alice", "Bob", "Carol", "David"]
scores = [92, 78, 85, 61]
subjects = ["Math", "Science", "English"]
subject_scores = [
    [90, 95, 91],
    [70, 80, 84],
    [88, 82, 85],
    [55, 65, 63]
]

print("--- Student Score Report ---")
for index, (student, score) in enumerate(zip(students, scores), start=1):
    print(str(index) + ". " + student + " | Overall Average: " + str(score))

print()
print("--- Subject Breakdown ---")
for student, subject_row in zip(students, subject_scores):
    print(student + ":")
    for subject, s_score in zip(subjects, subject_row):
        print("  " + subject + ": " + str(s_score))

print()
print("--- Class Averages by Subject ---")
for col_index, subject in enumerate(subjects):
    total = 0
    for row in subject_scores:
        total += row[col_index]
    average = total / len(subject_scores)
    print(subject + " average: " + str(average))

print()
print("--- Number Sequence (1 to 10, step 2) ---")
for n in range(1, 11, 2):
    print(n, end=" ")
print()
--- Student Score Report ---
1. Alice | Overall Average: 92
2. Bob | Overall Average: 78
3. Carol | Overall Average: 85
4. David | Overall Average: 61

--- Subject Breakdown ---
Alice:
  Math: 90
  Science: 95
  English: 91
Bob:
  Math: 70
  Science: 80
  English: 84
Carol:
  Math: 88
  Science: 82
  English: 85
David:
  Math: 55
  Science: 65
  English: 63

--- Class Averages by Subject ---
Math average: 75.75
Science average: 80.5
English average: 80.75

--- Number Sequence (1 to 10, step 2) ---
1 3 5 7 9