What’s a For Loop For? Python!

What’s a For Loop For? Python!

You know what’s not fun? Doing the same thing over and over again. You know what is fun? Telling a computer to do it for you! That’s when you need a for loop.

What’s Iteration?

while loops are useful when you need something to run until a condition is met. Sometimes you know that condition at the start, but you need to repeat a process a certain number of times. Boring.

Did you hear this adage growing up?

If at first you don’t succeed, try, try, try again.

I did and it drove me crazy. But it’s true.

When you are learning a new skill, such as riding a bike or tying your shoe, you probably won’t get it on your first try. So you’ll try again, learning from your earlier experiences. Eventually, after enough attempts, you’ll get it.

That’s iteration. You repeat a process until you achieve a desired result.

How Does a For Loop work?

Say you’ve got a group of ducks and you want to tag them as geese. In Python, for loops looks like this:

We can write that out in plainer language. It helps to read this with a ridiculous British accent (unless you have a ridiculous British accent):

And round and round we go until all of the ducks are goosed. In professional terms, each duck is an element and the goosing is the execution of the block. Let’s see this in practice, starting with one of my favorite things, strings.

For Loops and Strings

Let’s run a loop over a string.

We can write that out in pseudocode, like this:

Looping over a string will even process spaces, because spaces are considered characters:

Why did I change the iterator to char? You will often see in code the use of char, or c, in reference to a character in a string. It’s a convention that you should be familiar with.

We can do much more than print each character.


Home, Home on the Range… Function

The range() function let’s you set, well, a range of numbers to process. Like chocolate syrup on vanilla ice cream, it tastes great with for loops. Try this:

So far, the for loop examples we looked at process small ranges. But for loops are most useful when crunching large collections of elements and big numbers. Do you know the parable of the rice and the chessboard?

If a chessboard were to have wheat placed upon each square such that one grain were placed on the first square, two on the second, four on the third, and so on (doubling the number of grains on each subsequent square), how many grains of wheat would be on the chessboard at the finish?

In IDLE, create a file called rice.py:

Press F5 or select Run. Hope you’re hungry!

What’s a For Loop For? Python! was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid