🔁
Coding🔬 Ages 11-13Beginner 9 min read

Python Loops and Lists

Combine Python loops and lists: use a for loop to visit every item, build lists, count and total values, and use range() to repeat. Runnable examples and a quiz.

Key takeaways

  • A for loop visits every item in a list, one at a time
  • range(n) gives the numbers 0 up to n-1 for repeating a set number of times
  • You can total or count values by updating a variable inside a loop
  • append() inside a loop builds a new list automatically

Two ideas that fit together

A list stores many values. A loop repeats an action. Put them together and you get one of the most useful tools in programming: visiting every item in a list and doing something with it — without writing the same line over and over.

If you're new to either idea, read Lists and Arrays and Loops: Making the Computer Repeat first. This lesson combines them in Python.

The for loop

A for loop walks through a list one item at a time:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

Read it almost like English: "for each fruit in fruits, print that fruit." The word fruit is a temporary variable. On the first pass it holds "apple", then "banana", then "cherry". The indented line is the body — the code that runs each time. Indentation (the spaces at the start of the line) is how Python knows which code belongs inside the loop.

Repeating a fixed number of times with range()

Sometimes you don't have a list — you just want to repeat something a set number of times. That's what range() is for:

for i in range(5):
    print("Hello!")

This prints Hello! five times. range(5) produces the numbers 0, 1, 2, 3, 4 — it starts at 0 and stops before 5. The variable i takes each of those values in turn:

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Totalling numbers in a list

A common task is adding up all the numbers in a list. You start with a total of 0, then add each number as the loop runs:

scores = [8, 5, 10, 7]
total = 0
for score in scores:
    total = total + score
print("Total:", total)     # Total: 30

The line total = total + score means "take the current total and add the new score." After the loop visits every item, total holds the sum.

Counting items that match

You can also count how many items meet a condition by adding a check inside the loop:

scores = [8, 5, 10, 7]
high = 0
for score in scores:
    if score >= 8:
        high = high + 1
print("Scores of 8 or more:", high)   # 2

Here the loop checks each score, and only the scores 8 and above add 1 to high.

Building a new list

You can create a brand new list from an old one by appending inside a loop. Suppose you want to double every number:

numbers = [1, 2, 3, 4]
doubled = []
for n in numbers:
    doubled.append(n * 2)
print(doubled)     # [2, 4, 6, 8]

The list doubled starts empty. Each pass through the loop appends n * 2, so it grows to hold the doubled values.

Looping with the index

If you need the position number as well as the item, combine range() and len():

names = ["Sam", "Alex", "Jordan"]
for i in range(len(names)):
    print(i, names[i])

Output:

0 Sam
1 Alex
2 Jordan

len(names) is 3, so range(3) gives 0, 1, 2, and names[i] reads each item by its index.

Why this is powerful

Whether a list has 3 items or 3 million, the same loop handles it. That is the real magic: you write the logic once, and it scales to any amount of data. Music players, scoreboards, and search results all rely on looping over lists.

Practice challenges

  • Print every number from 1 to 10 using range().
  • Make a list of five numbers and print only the even ones (a number is even if n % 2 == 0).
  • Build a new list containing each name from a list with "!" added to the end.

Quick quiz

Test yourself and earn XP

What does this print? ``` for x in [1, 2, 3]: print(x) ```

What numbers does `range(4)` produce?

After `total = 0` then `for n in [2, 3, 5]: total = total + n`, what is total?

Which line adds the current item to a list called `result`?

FAQ

A for loop is best when you know what you want to repeat over — every item in a list, or a fixed number of times. A while loop repeats as long as a condition stays true, which is useful when you don't know how many times in advance.

It helps. Loops and lists work together: a list stores many values, and a loop lets you visit each one. Learning lists first makes loops over lists much clearer.