Loops (con't) and Nested Loops¶

In [ ]:
for i in [1, 2, 3]:
    print(i)
In [3]:
i = 2
i in [1, 2, 3]
Out[3]:
True
In [28]:
print("Hello")
print()
print("World")
Hello

World
In [29]:
for i in [6, 1, 3, 90, 4, -12435, 0, 10]:
    print(i)
6
1
3
90
4
-12435
0
10
In [32]:
list(range(10))
Out[32]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The range function¶

A typical for loop does not use an explicit list:

for i in range(5):
  ... body ...

range(5): cycles through [0, 1, 2, 3, 4]

  • 5 is the upper limit (exclusive)

range(1, 5): cycles through [1, 2, 3, 4]

  • 1 is the lower limit (inclusive)

range(1, 10, 2): cycles through [1, 3, 5, 7, 9]

  • 2 is the step (distance between elements)
In [5]:
for i in range(5):
    print("Happy Birthday!")
Happy Birthday!
Happy Birthday!
Happy Birthday!
Happy Birthday!
Happy Birthday!
In [9]:
for i in range(1, 6):
    print(i)
1
2
3
4
5
In [10]:
for i in range(1, 10, 2):
    print(i)
1
3
5
7
9
In [13]:
for i in range(10, 0, -1):
    print(i)
10
9
8
7
6
5
4
3
2
1

Some Loops (Review)¶

In [ ]:
# Sum of a list of values, what values?
result = 0
for element in range(5):
  result = result + element
print("The sum is: " + str(result))

# Sum of a list of values, what values?
result = 0
for element in range(5, 1, -1):
  result = result + element
print("The sum is:", result)

# Sum of a list of values, what values?
result = 0
for element in range(0, 8, 2):
  result = result + element
print("The sum is:", result)

# Sum of a list of values, what values?
result = 0
size = 5
for element in range(size):
  result = result + element
print("When size = " + str(size) + " result is " + str(result))
In [ ]:
for n in range(10):
    print(n)

Nested Loops¶

what is the output?

In [14]:
for i in [1, 2, 3]:
  print("Before j loop i is", i)
Before j loop i is 1
Before j loop i is 2
Before j loop i is 3
In [15]:
for j in [50, 100]:
    print("j is", j)
j is 50
j is 100
In [18]:
for i in [1, 2, 3]:
  print("Before j loop i is", i)
  for j in [50, 100]:
    print("j is", j)
Before j loop i is 1
j is 50
j is 100
Before j loop i is 2
j is 50
j is 100
Before j loop i is 3
j is 50
j is 100
In [21]:
for _ in range(10):
    print("Hello")
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

How many statements does this loop contain?

What is the output?

In [ ]:
for i in [0, 1]:
  print("Outer", i)
  for j in [2, 3]:
    print(" Inner", j)
    print("  Sum", i + j)
  print("Outer", i)
  • "Outer" loop body: 3 statements
  • "Inner" loop body: 2 statements

Understand loops through the transformation approach¶

Key idea:

  1. Assign each sequence eleemnt to the loop variable
  2. Duplicate the body
In [ ]:
# Original loop

for i in [0, 1]:
  print("Outer", i)
  for j in [2, 3]:
    print(" Inner", j)
In [ ]:
i = 0
print("Outer", i)
for j in [2, 3]:
  print(" Inner", j)
i = 1
print("Outer", i)
for j in [2, 3]:
  print(" Inner", j)
In [ ]:
i = 0
print("Outer", i)
j = 2
print(" Inner", j)
j = 3
print(" Inner", j)
i = 1
print("Outer", i)
j = 2
print(" Inner", j)
j = 3
print(" Inner", j)

Test your understanding of loops¶

In [ ]:
# Puzzle 1
for i in [0, 1]:
    print(i)
print(i)
In [ ]:
# Puzzle 2
i = 5
for i in []:
    print(i)
In [ ]:
# Puzzle 3
for i in [0, 1]:
    # Outer Loop Body
    print("Outer", i)
    for i in [2, 3]:
        # Inner Loop Body
        print(" Inner", i)
    print("Outer", i)

In Puzzle 3, you may notice that we reused the loop variable i in both the outer and inner loops. Make sure not to do this in your own code!

Some More Loops¶

In [ ]:
for size in [1, 2, 3, 4]:
  print("size is " + str(size))
  for element in range(size):
    print("element is " + str(element))

Even More Loops¶

what happens if we move result = 0 to be the first line of the program instead?

In [ ]:
for size in [1, 2, 3, 4]:
  result = 0
  for element in range(size):
    result = result + element
  print("size=" + str(size) + " result=" + str(result))
print("We are done!")
print("result is", result)

Fix this loop¶

  1. What does it actually print?
  2. How can we change it to correct its output?

Hint: Watch out for "edge conditions" (beginning or end of loop)

In [22]:
for n in range(1, 51):
    print(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
In [26]:
# Goal:  print 1, 2, 3, …, 48, 49, 50
for tens_digit in [0, 1, 2, 3, 4]:
  for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(tens_digit * 10 + ones_digit)
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
21
22
23
24
25
26
27
28
29
31
32
33
34
35
36
37
38
39
41
42
43
44
45
46
47
48
49

Some Fixes¶

In [ ]:
for tens_digit in [0, 1, 2, 3, 4]:
  for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(tens_digit * 10 + ones_digit + 1)
In [ ]:
for tens_digit in [0, 1, 2, 3, 4]:
  for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
    print(tens_digit * 10 + ones_digit) 
In [ ]:
for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(ones_digit)
for tens_digit in [1, 2, 3, 4]:
  for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(tens_digit * 10 + ones_digit)
print(50)

Loops over Strings¶

In [ ]:
for letter in "hello":
  print(letter)
In [ ]:
my_string = "CSE 160"
for letter in my_string:
  print(letter)
In [ ]:
count = 0
for letter in my_string:
  count = count + 1
print(count)