Mark Redd

17 - Loops

We are about to introduce another fundamental aspect of programming that will be used over and over again in programming (no pun intended). We will, as always, introduce the subject in an example script.

# loops.py

# we need to print numbers 1 - 5 with each
# number on its own line so:

print(1)
print(2)
print(3)
print(4)
print(5)

# there is a better way of doing this

# first, put an empty line between each example
print("") 

n = 1
while n <= 5:
    print(n)
    n += 1

# better! that only took 4 lines of code
# but here is an even better way
print("")

for n in range(5):
    print(n + 1)

Here is what should happen

$ python loops.py
1
2
3
4
5

1
2
3
4
5

1
2
3
4
5
$

What is happening here?

Here we see the concept of a loop. We just did the same thing (i.e. print numbers 1-5 with each number on its own line) 3 different ways. We will briefly touch on the pros and cons of each way of doing this particular task:

while-loops

I consider while-loops to generally be more simple than for loops. The while-loop follows the following syntax:

while truth_expression:
    do_things1
    do_things2
    ...
# end of the indented block

This can be read as “While truth_expression evaluates to True, do_things or else end the loop.” The while-loop proceeds as follows:

truth_expression is sometimes known as the loop exit condition. For the while-loop we did in loops.py, we wanted to loop a particular number of times so we initialized n to 1 and then incremented n each time before the loop started again.

for-loops

The built-in function range produces a range object which produces a sequence of integer numbers starting with 0. (Remember: Python counting always starts with 0.) The sequence increments by integers up to but not including the number passed as an argument. In our case, that means that range(5) produced the sequence: 0, 1, 2, 3, 4. This is a common way to say that we want to do something 5 times (as we see the sequence is 5 elements long) or to count up to from 0 to 4.

A range object is a type of iterable object. In Python, an iterable object is one that can produce a sequence of objects. (We will cover iterables more deeply later on.) A for-loop steps through the sequence the given iterable produces and assigns each element to a variable.

For-loops follow the following syntax:

for i in iterable:
    statements
    ...
# end of the indented block

Where i is the temporary variable. A for-loop will only repeat as many times as the iterable fed to it allows. (Again in loops.py, this was 5 times.) It also has the feature that, before each time that the loop runs, it defines a variable (which, in the example above, is i but you can name it whatever you want) based on the current element of the iterable. Therefore, i gets redefined for every iteration of the loop based on the next element of iterable.

Other than that, the statements executed in the indented block are done normally and an un-indentation indicates that the loop block is done and the loop can continue. Once the iterable has run out of values to give to i, the loop terminates and code continues normally.

In my estimation, for-loops are one of the most powerful features in programming and, as we discussed above, are closely connected with iterables. In the next lesson we will talk more in depth about iterables and how to make and use them.

Hone Your Skills