DEV Community

Feddy Mwanjumwa
Feddy Mwanjumwa

Posted on

Python Loops: How I Learned to Repeat Tasks Without Repeating Code

One of the first things I noticed when writing Python programs was that I sometimes wanted to do the same thing several times.

I could write the same line of code again and again, but that obviously wasn't a good way to program.

That's where loops came in.

The idea is simple, tell Python to repeat something for me.

for loops

I use a for loop when I want to go through a collection of items or repeat something a specific number of times.

For example, suppose I have some products:

Instead of writing four separate print() statements, Python goes through the list for me.

That was one of the first things that made loops click.

Using range()

I can also use range() when I want to repeat something a certain number of times.

For example:

12

This prints:

1

2

3

4

5

So range(1, 6) gives me numbers starting from 1, but stops before 6.

while loops

A while loop works a little differently.

It keeps running as long as a condition is true.

For example, I can keep asking for a PIN until the correct one is entered:

12

The loop keeps asking until the condition becomes false.

This helped me understand that while is useful when I don't necessarily know how many times something needs to repeat.

break

Sometimes I want to stop a loop completely.

That's what break does.

For example:

13

The loop stops when it reaches 6.

So even though the loop was supposed to go up to 10, break tells Python:

"Stop here."

continue

continue is different from break.

Instead of stopping the whole loop, it skips the current iteration and moves to the next one.

For example:

14

The output is:

1

2

4

5

The number 3 is skipped, but the loop continues.

That's the difference I had to remember:

break → stop the loop

continue → skip this round

enumerate()

I found enumerate() useful when I wanted both the item and its position in a list.

For example:

15

The result is:

1 Bread

2 Milk

3 Sugar

Without enumerate(), I would have to create and manage the counter myself.

A practical example

I can put some of these ideas together in a small shopping program.

Suppose I have a list of products and want to display them with numbers:

products = ["Bread", "Milk", "Sugar", "Soap"]

for number, product in enumerate(products, start=1):

print(f"{number}. {product}")

Now Python handles the numbering for me.

This is much cleaner than manually writing:

print("1. Bread")

print("2. Milk")

print("3. Sugar")

print("4. Soap")

What I understood

The main thing that finally made loops make sense to me was thinking about the question:

"Do I need to repeat something?"

If I want to go through items in a list, I can use a for loop.

If I want something to keep running while a condition is true, I can use a while loop.

If I want to stop the loop, I use break.

If I want to skip one iteration, I use continue.

And if I need the position of each item, enumerate() makes it easier.

Final thoughts

Loops seemed confusing at first because there was a lot of new syntax.

But once I saw that their main purpose is simply to avoid repeating the same code manually, they became much easier.

Instead of telling Python the same thing ten times, I can write it once and let the loop handle the repetition.

Top comments (0)