Python Loops

In Python, and most other languages, the program flow is from top to bottom. The code you write gets executed line by line, starting at line 1 and ending at your last line. The programming flow doesn't go back up again, in the normal course of events. It is, however, very useful to go back up and execute lines repeatedly. A loop is a way to do that.
The first loop we'll look at is called a for loop.

TECHNICAL NOTE: If you've come from another language then Python loops are different in that they don't have an iterator. You don't go from a to b by incrementing with a++, for example.

Before we get on to the for loop, it will help if we explore the range() function.

 

Python range()

The range function in Python is used to get a range of numbers. In between the round brackets, you type how many numbers you want.

range(10)

The above line would get you the numbers 0 to 9.

You can specify a starting number in your range. The two are separated by a comma.

range(1, 10)

The above line would get you the numbers 1 to 9.

You don't have to go up in units of 1. If you wanted to go through your numbers in anything other than one, you type another comma and then the step number:

range(1, 10, 2)

The above line would get you the numbers 1, 3, 5, 7, 9.

 

Python Loops

Now back to loops. We'll print out the numbers 0 to 9, ten numbers.

The for loop looks like this:

for loop_variable in sequence

You start with the keyword for, which is the type of loop you want. Next, after a space, you need a variable name. You'll see what this does in a moment. After your variable name, you need the keyword in. Then, after the keyword in, you need the sequence you want to loop through. This can be a sequence of numbers, like the 0 to 9 we're going to loop through, or things like lists and dictionaries (which you haven't covered yet).

To see how for loops work, start a new project. Add a new Python file to your project. Now add the following line:

for current_range_number in range(10):

Now add a print statement below your for loop line:

print(current_range_number)

Your code should look like this:

A Python for loop with a print statement

To understand this, start at the end:

in range(10)

We're saying, "Loop round some numbers in the range 0 to 9 (10 numbers)". Python will then extract each number in your range. But it needs to store each number somewhere. That 'somewhere' is inside of the variable name we've come up, which is current_range_number. Each time round the loop, then, the variable called current_range_number will hold a different number. It will first store 0, then 1, then 2, etc.

Try it out. Run your code and you should see this in the output window:

The output of a Python for loop

Now change the numbers in your range function to 1 and 11:

for current_range_number in range(1, 11):

Run your program and see what it prints out.

If you were surprised at what was printed out, just remember that the range function goes to one less than your largest number.

Instead of printing out the current range number, let's use that number to create a times table program.

The whole point of a loop is so that you can execute a line or lines of code repeatedly. For a times table program, you want to do this:

1 times 2 = 2
2 times 2 = 4
3 times 2 = 6
4 times 2 = 8
5 times 2 = 10
6 times 2 = 12
7 times 2 = 14
8 times 2 = 16
9 times 2 = 18
10 times 2 = 20

You could obviously do that without a loop. But it would be a long and unnecessary process. Instead, you only need two lines of code in a loop. The first line of the loop works out the answer to the times table and the second line prints it out. The loop forces the program back up to do it all over again. Let's see how.

Set up a variable at the top of your code, just before the loop:

multiplyBy = 2

To calculate the times table, you can multiply the current range number by whatever is in the variable we've called multiplyBy:

answer = current_range_number * multiplyBy

The variable current_range_number changes each time round the loop, remember. The variable multiplyBy stays the same. We multiply these two together and store the result in a new variable that we've called answer.

As the second line in the loop, we can either just print out answer:

print(answer)

Or use concatenation to print out something more useful:

print(str(current_range_number) + " times " + str(multiplyBy) + " = " + str(answer))

Notice that when you're concatenating, you need to convert your numbers to strings. You do this with the str() function.

str( current_range_number )

Your code should look like this, though:

A Python loops for a times table program

Run your program now. You'll see this printed to the output window.

The output of a Python times table program

With just a few lines of code, then, we've got the 2 times table, right up to ten. If you wanted the 2 times table right up to 100 then you only need to make one change to the range function:

range(1, 101)

That's the power of loops!

Python Exercise

Print out the 7 times table from 1 to 20.

In the next lesson, you'll see how loops and lips work in Python

Python loops and lists >