CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

for loops

A for statement in Python has this form:

for variable in range(expression):

Consider this little program as an example.

sum = 0
for k in range(4):
  sum = sum + k

Obviously the first line associates the variable sum with the number 0. Then we have a line beginning with for, followed by one or more indented lines — in this case just one — that constitute the loop's body. Notice how a “4” appears inside range(4): This tells the Python system to execute the body 4 times, once with k as 0, another time with k as 1, then with k as 2, and finally with k as 3. After it has executed “sum = sum + k” these four times, it has completed the for statement and then continues to the next unindented statement: “print(sum)”.

Of course, that statement displays the value of sum. But what is this? Of course, each execution of “sum = sum + k” changes the variable sum's value. The following table illustrates

iterationstatement expressionnew sum
before loop:sum = 0 00
k = 0:sum = sum + k 0 + 00
k = 1:sum = sum + k 0 + 11
k = 2:sum = sum + k 1 + 23
k = 3:sum = sum + k 3 + 36

So when we get to “print(sum)”, that statement will display 6.

Accumulator variables

In our earlier example with sum, we saw the statement “sum = sum + i”. This means something very different from its meaning in mathematics, where you might be tempted to subtract sum from each side and conclude that i is 0. Instead, in Python it means to add the current values of sum and i, and change sum to be that number.

This type of statement is very common. In fact, it is so common that we might go far as to give a name to such a variable: an accumulator variable. As an alternative example, suppose we want to compute the sum of 10 positive numbers entered by the user.

total = 0
for i in range(10):
    user_num = int(input())
    total = total + user_num

In this case as well, we have an initial value for total, but total is updated in terms of itself with each iteration.

Statements of the form “var = var + expression” are common enough that Python provides a shortcut for them: ‘+=’. We can abbreviate such a statement as “var += expression”. (Though used less frequently, there are similar shortcuts in ‘-=’, ‘*=’, ‘/=’, ‘%=’, ‘//=’, and ‘**=’.)

The range function

As we're using the for loop for now, it will always contain an invocation of the range function. This function can take a variety of argument configurations that can potentially be useful.

range(end) Counts from 0 up to but excluding end
range(start, end) Counts from start up to but excluding end
range(start, end, step) Counts from start by steps of step, stopping on reaching end

As an example, the following little program counts down from 10 to 1.

for i in range(100-1):

This says to execute the body with i as 10 first, but then to add −1 so that it then goes to 9, then 8, then 7, and so forth until it reaches 1. The “ending” number is always excluded, so it does not execute the body with i as 0.