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:

factor = int(raw_input("factor: "))
for i in range(1,13):
  answer = factor * i
  print("%d x %d = %d" % (factor,i,answer))

Running the above, here's one possible outcome:

factor: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36

Here we use the range(1,13) function to generate a python list ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) that 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 calculate factor times that value, then print the result.

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