Skip to content
Utah Community Learning

for loops: doing a thing a set number of times

About 20 minutes

for loops: Doing a Thing a Set Number of Times

Okay so here's the thing. Last lesson we talked about why loops exist, the whole rename-one-file-then-four-hundred idea. Today we actually write one.

The for loop is your tool for "do this exact thing a known number of times." Not "keep going until something happens," that's a different loop for a different day. A for loop is when you already know the count. Print something 5 times. Go through a list of 12 photo names. Count from 1 to 100. You know how many, up front.

The basic shape

``python for i in range(5): print("Hello") ``

Run that. You'll get "Hello" printed five times. Let's break down what's happening, piece by piece, because every piece matters.

range(5) gives you a sequence of numbers, 0, 1, 2, 3, 4. Five numbers. Yes, it starts at 0, not 1. That trips up basically every beginner including me, still, sometimes, at 11pm. Python counts from zero. Just believe it and move on.

i is a variable that grabs each number in that sequence, one at a time, as the loop goes around. You could name it anything, banana would work fine, but everybody calls it i out of habit and you should too, so your code looks like everyone else's code.

The indented line underneath, print("Hello"), is the part that actually runs each time through. That indentation isn't decoration. Python uses it to know what's "inside" the loop and what isn't. Get the indenting wrong and you'll get an error, or worse, code that runs but does the wrong thing.

Type it yourself

Don't copy and paste this one. Type it out, run it, watch it print five Hellos. Then change the 5 to a 3. Run it again. Change it to a 10. This is the boring-but-important part where your fingers learn the shape of the thing. I'm firm on this, actually typing beats copy-pasting every time, because your eyes will skim right past a mistake that your fingers would have caught.

Using the number

Here's where it gets useful. i isn't just there to make the loop go, you can actually use it.

``python for i in range(5): print("This is loop number", i) ``

That'll print "This is loop number 0" all the way through "This is loop number 4." If you want it to feel more human, count from 1:

``python for i in range(1, 6): print("This is loop number", i) ``

Notice that's range(1, 6), not range(1, 5). Range stops before the second number, it doesn't include it. This is the single most common "why is my loop off by one" mistake beginners make, and honestly I still make it sometimes too. When your output looks like it's missing the last one, or has an extra one you didn't expect, check your range numbers first.

Loops and file names, together

Since we started this whole loops conversation with renaming photo files, let's actually build toward that.

``python for i in range(1, 6): filename = "photo" + str(i) + ".jpg" print(filename) ``

Run it. You should see photo1.jpg through photo5.jpg print out, one per line. That's the whole idea behind batch renaming, right there, four lines of code. You're not actually touching any files yet, we're just building and testing the logic first, on a screen, where nothing can go wrong.

And that matters more than it sounds like. Okay so here's the thing, my actual first-ever script was supposed to rename 400 photo files by date. I wrote it, felt pretty good about myself, and ran it straight on the real folder. It renamed all 400 files to the exact same name. Which meant, one by one as the loop went around, 399 of them got overwritten and gone. Poof. I didn't have a backup.

So two lessons out of that, both real. One, always test your loop logic on something you can afford to lose, print statements, a scratch folder, whatever, before you point it at real files. Two, back up before you run anything that touches a bunch of files at once. Not because loops are dangerous exactly, they're not, they just do exactly what you tell them, as many times as you tell them to. That's the whole feature. It'll happily delete 400 files as fast as it renames one, if that's what your code says to do.

Looping over a list directly

You can also skip range() entirely and loop straight over a list of things you already have:

``python names = ["Alex", "Jordan", "Sam"] for name in names: print("Hi,", name) ``

No counting needed here, Python just walks through the list, one item at a time, and hands you each one as name. This is actually the more common way you'll loop once you're working with real data, range() is for when you need a plain count, and looping over a list is for when you already have the actual things.

A note on scale

Start small. Test your loop with range(5) before you try range(4000). This is the same "rename one file, then four hundred" idea from last time, and it applies here too. Get the logic right on a small number where you can actually check the output with your own eyes, then scale it up once you trust it.

Before next time

Write a loop that prints your name 10 times, then a second loop that counts from 1 to 20. Small, boring, exactly what we did today, just on your own.