For Loops

Loops

A loop is a tool that allows us to instruct Python to repeat a task several times, usually with slight variations each time. We will consider two types of loops in this course: for loops and while loops.

We will begin with for loops, but before we can do so, we need to introduce the concept of a range.

For Loops

We can use for loops to ask Python to repeat a task a certain number of times. The syntax for constructing a for loop is as follows:

for i in range(a,b): 
    # Body of for loop

This code asks Python to execute the body of the for loop 5 times - once for each integer in the specified range. The variable i is referred to as a loop counter. The counter will have a value of a during the first iteration of the loop, and will increment at the start of each new iteration.

Looping over Lists

names_list = ['Alex', 'Beth', 'Chad', 'Drew', 'Emma']

for name in names_list:
    print(f'Hello, {name}!')
Hello, Alex!
Hello, Beth!
Hello, Chad!
Hello, Drew!
Hello, Emma!

Ranges

The range(a, b) function generates a list of integers beginning with a and continuing up to, but NOT INCLUDING b.

A range is not a list, but can be converted into a list.

my_range = range(3,8)
print(type(my_range))
print(my_range)
print(list(my_range))
<class 'range'>
range(3, 8)
[3, 4, 5, 6, 7]
for i in range(0,5):
    print('The value of i is', i)
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4

The code in the cell above is equivalent to that in cell below. Notice that using a for loop for this task is much more concise.

i = 0
print('The value of i is', i)
i = 1
print('The value of i is', i)
i = 2
print('The value of i is', i)
i = 3
print('The value of i is', i)
i = 4
print('The value of i is', i)
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4

Note the following five points regarding for loops:

  1. We can use any variable name that we wish for the loop counter.

  2. We can use the counter in arithmetic operations, or as a list index.

  3. The range over which we are looping does not have to begin with 0.

  4. We can include multiple lines of code within a for loop.

  5. Each line in the body of a loop must be indented one level beyond the for statement.

These ideas are all illustrated in the following example, which prints the squares of the first 10 positive integers.

for n in range(1,11):
    message = "The square of " + str(n) + " is " + str(n**2) + "."
    print(message)
The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.
The square of 6 is 36.
The square of 7 is 49.
The square of 8 is 64.
The square of 9 is 81.
The square of 10 is 100.

We can also use loops to alter variables that are defined outside of the loop. This is useful for calculating running totals. The code in the cell below calculates the sum of the squares of the first 100 integers. That is, it calculates \(1^2 + 2^2 + 3^2 + ... + 99^2 + 100^2\).

# Sum the first 100 positive integers
total = 0
for i in range(1,101):
    total += i**2

print(total)
338350