for loops

motivation

The main purpose of a for loop is to save typing and avoid repitition. As an example, which would be easier to type? This:

print("99 bottles of beer on the wall,")
print("99 bottles of beer.")
print("Take one down, pass it around,")
print("98 bottles of beer on the wall.")

print("98 bottles of beer on the wall,")
print("98 bottles of beer.")
print("Take one down, pass it around,")
print("97 bottles of beer on the wall.")

print("97 bottles of beer on the wall,")
print("97 bottles of beer.")
print("Take one down, pass it around,")
print("96 bottles of beer on the wall.")

and so on... (imagine 96 more print stanzas!). Or this:

for numbottles in range(99,0,-1):
  print("%d bottles of beer on the wall," % (numbottles))
  print("%d bottles of beer." % (numbottles))
  print("Take one down, pass it around,")
  print("%d bottles of beer on the wall.\n" % (numbottles-1))

The trick to for loops is being able to recognize the repeating patterns, and use the for loop variable to somehow create whatever is changing, each time through the loop. In the above example, the number of bottles decreases by one each time, so a simple counter variable (numbottles) and a decreasing range() will do the trick.

DRY = Don't Repeat Yourself.

syntax

Here's the for loop syntax, where UPPERCASE words and to be filled in by you:

for VARIABLE in SEQUENCE:
  do this line
  and this one
  and all of this indented code block
  as many times as there are items in the sequence

VARIABLE is just a normal python variable (you pick the name), which takes on all values in the SEQUENCE, one at a time. The SEQUENCE is any valid python sequence -- could be a python list, or a string of characters, or each line in a file.

The indented lines are all executed once for each value in the SEQUENCE.

examples

This is the simplest example I can think of:

>>> for ch in "abcd":
...   print(ch)
... 
a
b
c
d
>>>

In the above example, ch is the loop variable, and "abcd" is the sequence. The first time through the loop, ch = "a", and the code block is executed (print(ch)). The second time through the loop, ch = "b", the next value in the sequence, and so on. Since there are 4 values in the sequence, the code block is executed 4 times, and we get 4 characters printed.

Here's another example:

start = int(raw_input("start: "))
for i in range(start,0,-1):
  dots = "." * i
  print("%d%s" % (i,dots))
print("blastoff!")

Running the above, here's one possible outcome (user enters 8):

start: 8
8........
7.......
6......
5.....
4....
3...
2..
1.
blastoff!

Here we use the range(start,0,-1) function to generate a python list from whatever start is down to one. That list is the SEQUENCE in our for loop. The loop variable, i, takes on all values in the SEQUENCE, one at a time. And for each value in the sequence, we print the value and that many dots.

challenge

Can you write a for loop that displays the following, given an integer (n) from the user?

n: 13

1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010
1111111111111111111111
121212121212121212121212
13131313131313131313131313

And if the user enters a different number for n:

n: 4

1
22
333
4444

CS21 Topics