For Loops

In Python, the for statement allows us to write programs that implement iteration. As a simple example, let’s say we have some friends, and we’d like to send them each an email inviting them to our party. We don’t quite know how to send email yet, so for the moment we’ll just print a message for each friend.

The general structure of a numerical for loop is

for variableName in range(startValue, tailValue):
    indented block of commands, called the loop "body"

As usual, the body block can be multiple lines long, as long as all of those lines are indented by the same amount. First the loop body is executed with variableName set to startValue. Then it repeats with variableName set to startValue+1, then again with startValue+2, et cetera. This continues until variableName has value tailValue-1, and afterwards the loop stops.

For Loop Structure

num in the for loop above is called the loop variable or, alternatively, the iterator variable.

Line 2 is the loop body. The loop body is always indented. The indentation determines exactly what statements are “in the loop”. The loop body is performed one time for each number from 1 to 10, 10 not inclusive.

A colon is required at the end of the line.

After the keyword in, and before the colon, is an expression that must evaluate to a sequence (e.g, a string or a list or a tuple — which we haven’t covered yet). It could be a literal, or a variable name, or a more complex expression. In this case, the sequence is created by the range() function. The sequence is [1, 2, 3, 4, 5, 6, 7, 8, 9].

Flow of Execution

As a program executes, the interpreter always keeps track of which statement is about to be executed. We call this the control flow, or the flow of execution of the program. When humans execute programs, they often use their finger to point to each statement in turn. So you could think of control flow as “Python’s moving finger”.

Control flow until now has been strictly top to bottom, one statement at a time. We call this type of control sequential. Sequential flow of control is always assumed to be the default behavior for a computer program. The for statement changes this.

Flow of control is often easy to visualize and understand if we draw a flowchart. This flowchart shows the exact steps and logic of how the for statement executes.

Loops may not seem to be necessary when you’re iterating over a few items, but they are extremely helpful when iterating over lots of items. Imagine if you needed to change what happened in the following code block. On the left, when you use iteration, changing the code is easy. On the right, when you have hard coded the process, changing the code is more difficult.

Coding Exercise 1

Given a number N, print all positive odd numbers less than N. For example, if given the number 16, your loop should print

1
3
5
7
9
11
13
15

For Loop Odd Exercise

Coding Exercise 2

The square numbers are the integers of the form K × K, e.g. 9 is a square number since 3 × 3 = 9. Write a program that reads an integer n from input and outputs all the positive square numbers less than n, one per line in increasing order. For example, if the input is 16, then the correct output would be

1
4
9

For Loop Square Exercise