Academy

Loops

The Problem With Repetition

Suppose you wanted to print every item in a list. You could do it manually:

fruits = ["apple", "banana", "cherry", "mango"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
print(fruits[3])

This works — but only because you know there are exactly four items. What if there were forty? Or four hundred? What if the number of items changes each time the program runs?

The answer is a loop — an instruction that tells Python: repeat this block of code, once for each item.

The for Loop

The for loop is Python's most common loop. It moves through a sequence — a list, a string, or a range of numbers — and runs a block of code once for each item.

Read this aloud: "For each fruit in fruits, print it." That's almost exactly what the code says.

Each time through the loop, the variable fruit holds the current item. First loop: "apple". Second: "banana". Third: "cherry". Fourth: "mango". Then the list ends and the loop stops.

The name fruit is your choice — it's just a variable. But naming it as the singular of your list name (fruit for fruits, city for cities) is a widely followed convention that makes loops instantly readable.

Looping With range()

Often you don't want to loop over a list — you just want to repeat something a certain number of times. range() generates a sequence of numbers for exactly this purpose.

range(5) produces: 0, 1, 2, 3, 4 — five numbers, starting from zero. The number you give it is the count, not the last value.

You can also give range() a start and stop:

range(1, 6) produces 1, 2, 3, 4, 5. As with list slicing — the start is included, the stop is not.

And a third argument sets the step size:

range(0, 20, 5) counts up in steps of 5: 0, 5, 10, 15.

Doing Something Useful in the Loop

The loop body — the indented block — can contain any code you like. Not just print().

total starts at zero. Each time through the loop, the current price is added to it. After the loop ends, total holds the sum of everything.

Notice that print("Total:", total) is outside the loop — it's not indented. It runs exactly once, after the looping is completely done.

The while Loop

The for loop repeats a fixed number of times. The while loop repeats as long as a condition is True.

Each time through: Python checks count <= 5. If True, it runs the block, then checks again. The moment the condition becomes False, the loop stops.

The line count = count + 1 is essential. Without it, count never changes, the condition stays True forever, and the loop never ends. This is called an infinite loop — one of the most common beginner mistakes.

The sandbox will time out if this happens, so don't worry — your browser won't freeze. But always double-check that your while loop has something inside it that eventually makes the condition False.

break — Stopping Early

break exits a loop immediately, regardless of where in the sequence you are.

The loop goes through the list. The moment it hits "Admin", it prints a message and exits completely. "Meera" and "Rohan" are never reached.

continue — Skipping One

continue skips the rest of the current iteration and jumps to the next one.

Everything prints except 4. When i is 4, continue fires — skipping the print() — and the loop moves on to 5.

break stops the loop entirely. continue skips one item and keeps going.

Looping Over a String

A string is also a sequence — of individual characters. A for loop moves through them one by one.

Not always needed, but useful to know — strings and lists behave similarly in many situations.

What You Have Learned

Loops are how programs handle repetition without repeating code.

The key ideas:

  • for item in sequence: runs the block once for each item in the sequence
  • range(n) generates numbers from 0 to n-1; range(start, stop, step) for more control
  • while condition: keeps running as long as the condition is True
  • Always ensure a while loop has something that eventually makes the condition False
  • break exits the loop immediately
  • continue skips the current iteration and moves to the next
  • Strings are sequences too — for can loop through their characters

In the next lesson, you'll learn how to wrap code into reusable blocks called functions — so you can write something once and use it anywhere.