ShareYourGroupShareYourGroupPython

Lesson 10 of 21 · about 6 minutes with the exercises

For loops and range

The for loop takes a collection of things and runs a block of code once for each item. It is the loop you will write most often. There is no counter to set up and no risk of forgetting to move it forward.

Going through a string

for letter in "cat":
    print(letter)

Output

c
a
t

Read it aloud: "for each letter in the string cat, print the letter". The name letter is a variable you choose. On each round Python puts the next item into it.

Going through a list

A list is a row of values in square brackets. Lists have their own lesson next. Looping through one looks the same.

fruits = ["mango", "banana", "guava"]
for fruit in fruits:
    print("I like", fruit)

Output

I like mango
I like banana
I like guava

A good habit is a plural name for the list and the singular for the loop variable.

Counting with range

range produces a run of whole numbers for a for loop to walk through.

for i in range(5):
    print(i, end=" ")
print()

Output

0 1 2 3 4 

range(5) gives five numbers, starting at 0 and stopping before 5. This matches how indexes work, which is why it starts at zero. To repeat something five times when you do not care about the number, this is all you need.

Give two numbers for a start and a stop. Give three for a step as well.

for i in range(1, 6):
    print(i, end=" ")
print()
for i in range(0, 21, 5):
    print(i, end=" ")
print()
for i in range(10, 0, -2):
    print(i, end=" ")
print()

Output

1 2 3 4 5 
0 5 10 15 20 
10 8 6 4 2 
CallNumbers produced
range(5)0 1 2 3 4
range(1, 6)1 2 3 4 5
range(0, 21, 5)0 5 10 15 20
range(10, 0, -2)10 8 6 4 2

The stop value is never included. To count from 1 to 10 you write range(1, 11).

A multiplication table

n = 9
for i in range(1, 11):
    print(f"{n} x {i:2} = {n * i:2}")

Output

9 x  1 =  9
9 x  2 = 18
9 x  3 = 27
9 x  4 = 36
9 x  5 = 45
9 x  6 = 54
9 x  7 = 63
9 x  8 = 72
9 x  9 = 81
9 x 10 = 90

Totals and counts

The pattern from the last lesson works here too: start a variable before the loop and update it inside.

marks = [78, 45, 91, 60, 33]
total = 0
passed = 0
for m in marks:
    total += m
    if m >= 40:
        passed += 1
print("Total:", total)
print("Passed:", passed, "of", len(marks))

Output

Total: 307
Passed: 4 of 5

Position and item together

Sometimes you need the position as well as the item. enumerate hands you both. The second value tells it where to start counting.

names = ["Asha", "Ravi", "Meena"]
for number, name in enumerate(names, 1):
    print(number, name)

Output

1 Asha
2 Ravi
3 Meena

break and continue

Both work in for loops exactly as in while loops. Here the loop stops at the first number that divides by 7.

for n in range(50, 60):
    if n % 7 == 0:
        print("First multiple of 7:", n)
        break

Output

First multiple of 7: 56

A loop inside a loop

A nested loop runs the whole inner loop for each round of the outer one. It is the way to print grids and patterns.

for row in range(1, 5):
    for col in range(row):
        print("*", end="")
    print()

Output

*
**
***
****

The outer loop chooses the row. The inner loop prints as many stars as the row number. The empty print() ends the line.

The same pattern can be written with string repetition, which is shorter:

for row in range(1, 5):
    print("*" * row)

Output

*
**
***
****

Do not change a list while looping over it

Removing items from a list inside a for loop over that same list makes Python skip items. If you need to filter a list, build a new one. The lesson on list comprehensions shows a neat way.

Try it yourself

Exercise 1. Print the squares of the numbers 1 to 10 on one line.

Show one answer
for n in range(1, 11):
    print(n * n, end=" ")
print()

Output

1 4 9 16 25 36 49 64 81 100 

Exercise 2. Count how many vowels are in the sentence "education is the key".

Show one answer
sentence = "education is the key"
count = 0
for ch in sentence:
    if ch in "aeiou":
        count += 1
print(count)

Output

8

Exercise 3. Print all numbers from 1 to 30 that divide by 3 but not by 2.

Show one answer
for n in range(1, 31):
    if n % 3 == 0 and n % 2 != 0:
        print(n, end=" ")
print()

Output

3 9 15 21 27 

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.