STR Coding Club logo STR Coding ClubLearn to code, together
Python and Practice

Python Loops Explained With Things You Already Do

Loops are where many beginners first feel that programming is getting difficult. Variables and print statements make sense, but suddenly there are for loops, while loops, ranges and indentation, and the code seems to jump around.

Abstract illustration for Python Loops Explained With Things You Already Do

Loops are where many beginners first feel that programming is getting difficult. Variables and print statements make sense, but suddenly there are for loops, while loops, ranges and indentation, and the code seems to jump around. The good news is that you already use loops every day without thinking about it. Once you connect Python loops to everyday activities, they become much easier to understand.

I taught maths for years before teaching Python, and loops are where everyday examples help most. Here is how Python loops work, explained with things you already do.

A loop is just repetition with a rule

A loop tells the computer to repeat a set of instructions. You do this constantly. When you wash dishes, you repeat the same steps for each plate: pick it up, scrub it, rinse it, put it on the rack. You stop when there are no plates left. That is a loop.

Python has two main kinds of loops. A for loop repeats once for each item in a collection, like each plate in the sink. A while loop repeats as long as a condition is true, like stirring soup while it is still too cold.

The for loop: once for each item

Imagine you have a shopping list and want to read each item aloud. In Python:

shopping = ["bread", "milk", "eggs"]
for item in shopping:
    print(item)

The loop takes each item in the list in turn, stores it in the variable item and runs the indented line. When there are no items left, the loop ends. The indentation matters: everything indented under the for line belongs to the loop.

Counting with range

Sometimes you want to repeat something a set number of times, like doing ten push-ups. Python's range() function creates a sequence of numbers to loop over:

for number in range(1, 11):
    print("Push-up", number)

This prints "Push-up 1" through "Push-up 10". Note that range(1, 11) stops before 11. That off-by-one detail trips up almost every beginner, so do not worry if it confuses you at first.

The while loop: until something changes

A while loop keeps going as long as a condition is true. Think of filling a kettle: you keep pouring while the water is below the line.

water = 0
while water < 5:
    water = water + 1
    print("Water level:", water)

Each time through the loop, the water level rises. When it reaches 5, the condition is false and the loop stops. If you forget to change the water level inside the loop, the condition never becomes false, and the loop runs forever. That is called an infinite loop, and every programmer creates one occasionally.

Stopping early with break

Sometimes you want to stop a loop before it finishes, like stopping your search through a drawer once you find your keys. The break keyword exits a loop immediately:

for thing in drawer:
    if thing == "keys":
        print("Found them!")
        break

Should beginners avoid while loops?

Some tutorials tell beginners to avoid while loops entirely and use only for loops, because while loops can run forever. I understand the caution, but I think it is unhelpful advice.

while loops are the natural tool for many real problems: games that run until the player quits, programs that keep asking for input until it is valid and simulations that run until a goal is reached. Learning to write them carefully, always making sure something inside the loop changes the condition, is an important skill. Infinite loops are a normal part of learning, and stopping one is as simple as pressing Ctrl+C.

Loops in games

Almost every game runs inside a loop: check input, update the game, draw the screen, repeat. Even simple text games use a while loop that keeps asking for the player's move until the game ends. That makes games a great way to practise loops, as described in how online game logic teaches loops and conditions.

Practice ideas

  • Print the times table for any number.
  • Count down from 10 to 1, then print "Lift off!".
  • Ask for passwords until the user types the right one.
  • Add up all the numbers in a list.
  • Find the largest number in a list without using max().

Try writing one loop every day for a week, each based on something you did that day. By the end, loops will feel like second nature.

If a loop ever confuses you, act it out on paper: write the variable values for each pass. Tracing loops by hand is one of the fastest ways to understand them.

You already know how to loop

Every time you repeat a task with a rule for when to stop, you are thinking in loops. Python simply asks you to write that rule down precisely. Practise with small examples, and loops will soon feel natural. If you get stuck on an exercise, see how to get unstuck on a practice problem without peeking. More in our Python and Practice section.

BN
Bartholomew Ng

Bartholomew taught maths for years before switching to teaching Python. He writes about the basics of the language and about teaching code without drowning learners in syntax.

More posts by Bartholomew

More from the blog