for loops

Try the Jupyter notebook version.

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:

In [5]:
print("99 bottles of Coke on the wall,")
print("99 bottles of Coke.")
print("Take one down, pass it around,")
print("98 bottles of Coke on the wall.")
print()

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

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

98 bottles of Coke on the wall,
98 bottles of Coke.
Take one down, pass it around,
97 bottles of Coke on the wall.

97 bottles of Coke on the wall,
97 bottles of Coke.
Take one down, pass it around,
96 bottles of Coke on the wall.

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

In [4]:
for numbottles in range(99,0,-1):
  print(numbottles, "bottles of Coke on the wall,")
  print(numbottles, "bottles of Coke.")
  print("Take one down, pass it around,")
  print(numbottles-1, "bottles of Coke on the wall.")
  print()
99 bottles of Coke on the wall,
99 bottles of Coke.
Take one down, pass it around,
98 bottles of Coke on the wall.

98 bottles of Coke on the wall,
98 bottles of Coke.
Take one down, pass it around,
97 bottles of Coke on the wall.

97 bottles of Coke on the wall,
97 bottles of Coke.
Take one down, pass it around,
96 bottles of Coke on the wall.

96 bottles of Coke on the wall,
96 bottles of Coke.
Take one down, pass it around,
95 bottles of Coke on the wall.

95 bottles of Coke on the wall,
95 bottles of Coke.
Take one down, pass it around,
94 bottles of Coke on the wall.

94 bottles of Coke on the wall,
94 bottles of Coke.
Take one down, pass it around,
93 bottles of Coke on the wall.

93 bottles of Coke on the wall,
93 bottles of Coke.
Take one down, pass it around,
92 bottles of Coke on the wall.

92 bottles of Coke on the wall,
92 bottles of Coke.
Take one down, pass it around,
91 bottles of Coke on the wall.

91 bottles of Coke on the wall,
91 bottles of Coke.
Take one down, pass it around,
90 bottles of Coke on the wall.

90 bottles of Coke on the wall,
90 bottles of Coke.
Take one down, pass it around,
89 bottles of Coke on the wall.

89 bottles of Coke on the wall,
89 bottles of Coke.
Take one down, pass it around,
88 bottles of Coke on the wall.

88 bottles of Coke on the wall,
88 bottles of Coke.
Take one down, pass it around,
87 bottles of Coke on the wall.

87 bottles of Coke on the wall,
87 bottles of Coke.
Take one down, pass it around,
86 bottles of Coke on the wall.

86 bottles of Coke on the wall,
86 bottles of Coke.
Take one down, pass it around,
85 bottles of Coke on the wall.

85 bottles of Coke on the wall,
85 bottles of Coke.
Take one down, pass it around,
84 bottles of Coke on the wall.

84 bottles of Coke on the wall,
84 bottles of Coke.
Take one down, pass it around,
83 bottles of Coke on the wall.

83 bottles of Coke on the wall,
83 bottles of Coke.
Take one down, pass it around,
82 bottles of Coke on the wall.

82 bottles of Coke on the wall,
82 bottles of Coke.
Take one down, pass it around,
81 bottles of Coke on the wall.

81 bottles of Coke on the wall,
81 bottles of Coke.
Take one down, pass it around,
80 bottles of Coke on the wall.

80 bottles of Coke on the wall,
80 bottles of Coke.
Take one down, pass it around,
79 bottles of Coke on the wall.

79 bottles of Coke on the wall,
79 bottles of Coke.
Take one down, pass it around,
78 bottles of Coke on the wall.

78 bottles of Coke on the wall,
78 bottles of Coke.
Take one down, pass it around,
77 bottles of Coke on the wall.

77 bottles of Coke on the wall,
77 bottles of Coke.
Take one down, pass it around,
76 bottles of Coke on the wall.

76 bottles of Coke on the wall,
76 bottles of Coke.
Take one down, pass it around,
75 bottles of Coke on the wall.

75 bottles of Coke on the wall,
75 bottles of Coke.
Take one down, pass it around,
74 bottles of Coke on the wall.

74 bottles of Coke on the wall,
74 bottles of Coke.
Take one down, pass it around,
73 bottles of Coke on the wall.

73 bottles of Coke on the wall,
73 bottles of Coke.
Take one down, pass it around,
72 bottles of Coke on the wall.

72 bottles of Coke on the wall,
72 bottles of Coke.
Take one down, pass it around,
71 bottles of Coke on the wall.

71 bottles of Coke on the wall,
71 bottles of Coke.
Take one down, pass it around,
70 bottles of Coke on the wall.

70 bottles of Coke on the wall,
70 bottles of Coke.
Take one down, pass it around,
69 bottles of Coke on the wall.

69 bottles of Coke on the wall,
69 bottles of Coke.
Take one down, pass it around,
68 bottles of Coke on the wall.

68 bottles of Coke on the wall,
68 bottles of Coke.
Take one down, pass it around,
67 bottles of Coke on the wall.

67 bottles of Coke on the wall,
67 bottles of Coke.
Take one down, pass it around,
66 bottles of Coke on the wall.

66 bottles of Coke on the wall,
66 bottles of Coke.
Take one down, pass it around,
65 bottles of Coke on the wall.

65 bottles of Coke on the wall,
65 bottles of Coke.
Take one down, pass it around,
64 bottles of Coke on the wall.

64 bottles of Coke on the wall,
64 bottles of Coke.
Take one down, pass it around,
63 bottles of Coke on the wall.

63 bottles of Coke on the wall,
63 bottles of Coke.
Take one down, pass it around,
62 bottles of Coke on the wall.

62 bottles of Coke on the wall,
62 bottles of Coke.
Take one down, pass it around,
61 bottles of Coke on the wall.

61 bottles of Coke on the wall,
61 bottles of Coke.
Take one down, pass it around,
60 bottles of Coke on the wall.

60 bottles of Coke on the wall,
60 bottles of Coke.
Take one down, pass it around,
59 bottles of Coke on the wall.

59 bottles of Coke on the wall,
59 bottles of Coke.
Take one down, pass it around,
58 bottles of Coke on the wall.

58 bottles of Coke on the wall,
58 bottles of Coke.
Take one down, pass it around,
57 bottles of Coke on the wall.

57 bottles of Coke on the wall,
57 bottles of Coke.
Take one down, pass it around,
56 bottles of Coke on the wall.

56 bottles of Coke on the wall,
56 bottles of Coke.
Take one down, pass it around,
55 bottles of Coke on the wall.

55 bottles of Coke on the wall,
55 bottles of Coke.
Take one down, pass it around,
54 bottles of Coke on the wall.

54 bottles of Coke on the wall,
54 bottles of Coke.
Take one down, pass it around,
53 bottles of Coke on the wall.

53 bottles of Coke on the wall,
53 bottles of Coke.
Take one down, pass it around,
52 bottles of Coke on the wall.

52 bottles of Coke on the wall,
52 bottles of Coke.
Take one down, pass it around,
51 bottles of Coke on the wall.

51 bottles of Coke on the wall,
51 bottles of Coke.
Take one down, pass it around,
50 bottles of Coke on the wall.

50 bottles of Coke on the wall,
50 bottles of Coke.
Take one down, pass it around,
49 bottles of Coke on the wall.

49 bottles of Coke on the wall,
49 bottles of Coke.
Take one down, pass it around,
48 bottles of Coke on the wall.

48 bottles of Coke on the wall,
48 bottles of Coke.
Take one down, pass it around,
47 bottles of Coke on the wall.

47 bottles of Coke on the wall,
47 bottles of Coke.
Take one down, pass it around,
46 bottles of Coke on the wall.

46 bottles of Coke on the wall,
46 bottles of Coke.
Take one down, pass it around,
45 bottles of Coke on the wall.

45 bottles of Coke on the wall,
45 bottles of Coke.
Take one down, pass it around,
44 bottles of Coke on the wall.

44 bottles of Coke on the wall,
44 bottles of Coke.
Take one down, pass it around,
43 bottles of Coke on the wall.

43 bottles of Coke on the wall,
43 bottles of Coke.
Take one down, pass it around,
42 bottles of Coke on the wall.

42 bottles of Coke on the wall,
42 bottles of Coke.
Take one down, pass it around,
41 bottles of Coke on the wall.

41 bottles of Coke on the wall,
41 bottles of Coke.
Take one down, pass it around,
40 bottles of Coke on the wall.

40 bottles of Coke on the wall,
40 bottles of Coke.
Take one down, pass it around,
39 bottles of Coke on the wall.

39 bottles of Coke on the wall,
39 bottles of Coke.
Take one down, pass it around,
38 bottles of Coke on the wall.

38 bottles of Coke on the wall,
38 bottles of Coke.
Take one down, pass it around,
37 bottles of Coke on the wall.

37 bottles of Coke on the wall,
37 bottles of Coke.
Take one down, pass it around,
36 bottles of Coke on the wall.

36 bottles of Coke on the wall,
36 bottles of Coke.
Take one down, pass it around,
35 bottles of Coke on the wall.

35 bottles of Coke on the wall,
35 bottles of Coke.
Take one down, pass it around,
34 bottles of Coke on the wall.

34 bottles of Coke on the wall,
34 bottles of Coke.
Take one down, pass it around,
33 bottles of Coke on the wall.

33 bottles of Coke on the wall,
33 bottles of Coke.
Take one down, pass it around,
32 bottles of Coke on the wall.

32 bottles of Coke on the wall,
32 bottles of Coke.
Take one down, pass it around,
31 bottles of Coke on the wall.

31 bottles of Coke on the wall,
31 bottles of Coke.
Take one down, pass it around,
30 bottles of Coke on the wall.

30 bottles of Coke on the wall,
30 bottles of Coke.
Take one down, pass it around,
29 bottles of Coke on the wall.

29 bottles of Coke on the wall,
29 bottles of Coke.
Take one down, pass it around,
28 bottles of Coke on the wall.

28 bottles of Coke on the wall,
28 bottles of Coke.
Take one down, pass it around,
27 bottles of Coke on the wall.

27 bottles of Coke on the wall,
27 bottles of Coke.
Take one down, pass it around,
26 bottles of Coke on the wall.

26 bottles of Coke on the wall,
26 bottles of Coke.
Take one down, pass it around,
25 bottles of Coke on the wall.

25 bottles of Coke on the wall,
25 bottles of Coke.
Take one down, pass it around,
24 bottles of Coke on the wall.

24 bottles of Coke on the wall,
24 bottles of Coke.
Take one down, pass it around,
23 bottles of Coke on the wall.

23 bottles of Coke on the wall,
23 bottles of Coke.
Take one down, pass it around,
22 bottles of Coke on the wall.

22 bottles of Coke on the wall,
22 bottles of Coke.
Take one down, pass it around,
21 bottles of Coke on the wall.

21 bottles of Coke on the wall,
21 bottles of Coke.
Take one down, pass it around,
20 bottles of Coke on the wall.

20 bottles of Coke on the wall,
20 bottles of Coke.
Take one down, pass it around,
19 bottles of Coke on the wall.

19 bottles of Coke on the wall,
19 bottles of Coke.
Take one down, pass it around,
18 bottles of Coke on the wall.

18 bottles of Coke on the wall,
18 bottles of Coke.
Take one down, pass it around,
17 bottles of Coke on the wall.

17 bottles of Coke on the wall,
17 bottles of Coke.
Take one down, pass it around,
16 bottles of Coke on the wall.

16 bottles of Coke on the wall,
16 bottles of Coke.
Take one down, pass it around,
15 bottles of Coke on the wall.

15 bottles of Coke on the wall,
15 bottles of Coke.
Take one down, pass it around,
14 bottles of Coke on the wall.

14 bottles of Coke on the wall,
14 bottles of Coke.
Take one down, pass it around,
13 bottles of Coke on the wall.

13 bottles of Coke on the wall,
13 bottles of Coke.
Take one down, pass it around,
12 bottles of Coke on the wall.

12 bottles of Coke on the wall,
12 bottles of Coke.
Take one down, pass it around,
11 bottles of Coke on the wall.

11 bottles of Coke on the wall,
11 bottles of Coke.
Take one down, pass it around,
10 bottles of Coke on the wall.

10 bottles of Coke on the wall,
10 bottles of Coke.
Take one down, pass it around,
9 bottles of Coke on the wall.

9 bottles of Coke on the wall,
9 bottles of Coke.
Take one down, pass it around,
8 bottles of Coke on the wall.

8 bottles of Coke on the wall,
8 bottles of Coke.
Take one down, pass it around,
7 bottles of Coke on the wall.

7 bottles of Coke on the wall,
7 bottles of Coke.
Take one down, pass it around,
6 bottles of Coke on the wall.

6 bottles of Coke on the wall,
6 bottles of Coke.
Take one down, pass it around,
5 bottles of Coke on the wall.

5 bottles of Coke on the wall,
5 bottles of Coke.
Take one down, pass it around,
4 bottles of Coke on the wall.

4 bottles of Coke on the wall,
4 bottles of Coke.
Take one down, pass it around,
3 bottles of Coke on the wall.

3 bottles of Coke on the wall,
3 bottles of Coke.
Take one down, pass it around,
2 bottles of Coke on the wall.

2 bottles of Coke on the wall,
2 bottles of Coke.
Take one down, pass it around,
1 bottles of Coke on the wall.

1 bottles of Coke on the wall,
1 bottles of Coke.
Take one down, pass it around,
0 bottles of Coke on the wall.

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 are to be filled in by you:

for VARIABLE in SEQUENCE:
  do this line
  also this one
  also this indented code block
  after this start the loop again

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 -- it could be a python list, the output of the range function, a string of characters, or each line in a file. We will see the string and file examples later in class.

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

examples

Here is a simple example:

In [7]:
for ch in ["a", "b", "c", "d"]:
  print(ch)
a
b
c
d

In the above example, ch is the loop variable, and ["a","b","c","d"] is the sequence. In particular, this sequence is a python list.

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 for you to run.

In [14]:
timer = 8
for count_up in range(timer):
  count_down = timer - count_up
  dots = "." * count_down
  print(count_down, dots)
print("blastoff!")
8 ........
7 .......
6 ......
5 .....
4 ....
3 ...
2 ..
1 .
blastoff!

We use the range() function to generate a sequence of numbers. In particular, range(timer) starts at 0 and counts up until just before it reaches timer. The range function generates the SEQUENCE in our for loop. The loop variable, count_up, takes on all values in the SEQUENCE, one at a time. And for each value in the sequence, we calculate count_down, create the string of dots, and then print count_down and dots.

challenge

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

Enter the value for 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:

Enter the value for n: 4

1
22
333
4444
In [ ]: