A loop repeats a block of code. Instead of writing the same three lines fifty times, you write them once and tell the computer how many times to run them.
for number in range(3):
print("Hello", number)That prints three lines. Change 3 to 300 and the code does not get any longer.
For loops: when you know what you are looping over#
A for loop walks through a collection, one item at a time.
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print("I like", fruit)Read it as: “for each fruit in fruits, do this”. The variable fruit holds a different value on each pass. See arrays and lists explained for more on collections.
To repeat a fixed number of times, loop over a range of numbers:
for i in range(5):
print(i) # 0 1 2 3 4range(5) gives you 0, 1, 2, 3, 4 — five numbers starting at zero, not ending at five. Counting from zero surprises everyone once, then never again.
JavaScript has a similar loop:
const fruits = ["apple", "banana", "mango"];
for (const fruit of fruits) {
console.log("I like " + fruit);
}While loops: when you do not know how many times#
A while loop keeps going as long as a condition stays true.
total = 0
while total < 100:
total = total + 25
print(total)Use while when the stopping point depends on something that happens inside the loop — a user typing “quit”, a file running out of lines, a score reaching a target.
Choosing between them#
| Situation | Use |
|---|---|
| Every item in a list | for |
| Exactly N times | for with a range |
| Until the user quits | while |
| Until a condition is met | while |
If you can answer “how many times?” before the loop starts, use for.
break and continue#
break leaves the loop immediately. continue skips the rest of this pass and starts the next one.
for number in range(10):
if number == 3:
continue # skip 3
if number == 6:
break # stop entirely
print(number) # 0 1 2 4 5Counting and collecting#
Two patterns you will use constantly. Set up a variable before the loop, and change it inside.
prices = [120, 80, 200, 45]
total = 0
for price in prices:
total = total + price
print("Total:", total) # 445names = ["ada", "sam", "kai"]
capitalised = []
for name in names:
capitalised.append(name.title())
print(capitalised) # ['Ada', 'Sam', 'Kai']Looping with the index#
Sometimes you need the position as well as the value:
for position, fruit in enumerate(fruits):
print(position, fruit)Reach for this only when you actually need the number. Looping over the items directly is easier to read.
Questions people ask#
Why does range(5) stop at 4?
Because it starts at 0 and produces five numbers. It is consistent with how positions in lists work — the first item is at position 0 — which makes the two fit together neatly once you are used to it.
Can I loop inside a loop?
Yes. A nested loop runs the inner loop completely for every pass of the outer one, so 10 by 10 means 100 passes. Useful for grids and tables, but the work multiplies quickly, so watch out with large data.
My loop only runs once — why?
In Python, check your indentation. Code indented less than the loop body sits outside the loop and runs only after it finishes. That is the single most common cause.
What is a list comprehension?
A compact Python way to build a list from a loop: [n * 2 for n in numbers]. It does the same job as a loop with append. Learn the loop version first — the short form is easier to read once you already know what it replaces.
Where to go next#
- Functions explained — naming a chunk of work
- Python for loops in more depth
- Build a number guessing game — a loop doing real work
- Python while loops in more depth