Skip to main content

Section 2.2 For Loops

For loops repeat a block of code a set number of times. This is useful when we know ahead of time how many times we want to repeat something. For loops are often used with lists and ranges.

Subsection 2.2.1 Lists

A list is a collection of thing, in a specific order. In python, we can create lists using square brackets, with commas between each item in the list. The following are each lists.
myList = [2,4,6,8,10]
people = [Abdul, Maria, Joseph]
For loops allow us to go through each item in a list, one at a time. One way to do this is to name the elements of the list and use the word "in."
This code runs through each item in "myList" and prints it one at a time. This is different than printing the whole list at once. As you can see below, just printing "myList" gives the entire list as one output.

Subsection 2.2.2 Ranges

The range function in python creates a list of numbers, in order, starting from 0 and going up to one less than the number you give it. For example, the code below creates a list of numbers from 0 up to 9. Notice we have to add a "list" function around the range function.
You can start a range from any value by giving it two numbers. The first number is where the list will start, and the second number is where it will stop - remembering it will stop one before that number.
Ranges combined with for loops allow us to repeat a block of code a set number of times. Imagine if we wanted to take a variable "number," and add 5 to it ten times. We can do this using a range with a for loop. Since we are not making a list, we do not need to use the list function.
The variable i is a placeholder that takes on each value in the range as the loop runs. However, since we are not using i in the code block, it does not affect the outcome. After running this code, the value of number is 50, since we added 5 to it ten times. Even though the range stops at 9, the loop still runs ten times, since it starts counting from 0.
We would get the same result writing "range(0,10)" or "range(1,11)." In the first case, the value of i would go from 0 to 9, and in the second case, it would go from 1 to 10. But in both cases, the loop runs ten times.
What is useful about having a placeholder like i is that we can use it within the code block. This is even more useful, as we can refer to items in a list by their index. The index is the location of an element in the list. It starts at 0 for the first item. For example, in the list [2,4,6,8,10], the index of 2 is 0, the index of 4 is 1, the index of 6 is 2, and so on.
To refer to an item in a list this way, we write the name of the list, followed by square brackets with the index inside. The notation "myList[2]" represents the element of myList with index 2. Remembering the index starts at zero, that would be 6.
This indexing is useful when combined with for loops and ranges. If we wanted to add 5 to each element in a list, we could use this indexing method. The code below does this.
This code starts at myList[0] and then adds 5 to it. Then, it goes ot myList[1] and adds 5 to it, and so on, until it has added 5 to each item in the list.

Subsection 2.2.3 Counting by More than One

The range() function can take a third number like range(0,10,2). This third number tells the range what to count by. So this code would start at 0 an count by 2 up to 8 - remember the last number is not included!

Exercises 2.2.4 Exercises

1.

Write a for loop that prints the numbers from 1 to 10.
Solution.
You could, but don’t need to, create a list here. You could use the range() function instead and print "as you go." Your range should start at 1 and go up to 11, since the last number is not included.
for i in range(1,11):
    print(i)

2.

Write a for loop that adds 3 to each element in the list [1,2,3,4,5] and prints the new list.
Solution.
First, create the list. You can name it anything you like. Here, I call it simply "theList."
theList = [1,2,3,4,5]
Now, create a for loop that goes through each item in the list. This is where you want to use the indexing method. There are 5 items in the list, so you want to repeat the code block 5 times. You can do this using range(5).
for i in range(5):
    theList[i] = theList[i] + 3
Finally, print the new list.
print(theList)
All together, the code looks like this:
theList = [1,2,3,4,5]
for i in range(5):
    theList[i] = theList[i] + 3
print(theList)

3.

Write a for loop that prints the even numbers from 0 to 20.
Hint 1.
You do not need to create a list. You can use the range() function directly.
Hint 2.
Count by 2 in the range list.
Solution.
Using the range() function, you can create a for loop that prints every other number from 0 to 20.
for i in range(0,21,2):
    print(i)
Notice that we go up to 21. The range function stops one before the last number, so to include 20, we have to go one higher.