Try again! The value of z should be 4 in order to return "True".
x = -2, y = 1, z = -1
Correct!
x = 50, y = 41, z = 94
Try again! The value of z should be 91 in order to return "True".
Checkpoint5.34.4.
Q-4: How many times would "Hello world!" print?
for i in range(4):
print("Hello world!")
3
Try again! The in range function has an inclusive end value.
4
Correct!
0
Try again! The in range function has an inclusive end value.
5
Try again! The in range function has an inclusive end value.
Checkpoint5.34.5.
Q-5: How many times does the following code print and in what pattern?
lst1 = [1, 3, 5, 7]
lst2 = [2, 4, 6, 8, 10]
for x in lst1:
for y in lst2:
print(x * y)
It prints 19 times and it skip counts by the current value in lst2.
Try again! For loops include the last element.
It prints 20 times and it skip counts by the current value in lst1.
Try again! This code counts by the second list.
It prints 19 times and it skip counts by the current value in lst1.
Try again! For loops include the last element and count by the second list.
It prints 20 times and it skip counts by the current value in lst2.
Correct!
Checkpoint5.34.6.
Q-6: What does the following code print?
out = ""
for i in range(2):
for j in range(2):
out += "i: " + str(i) + " j:" + str(j) + ", "
print(out)
i: 0 j: 0, i: 1 j: 1,
The inner loop will loop twice for each value of i.
i: 0 j: 0, i: 1 j: 1, i: 2 j: 2,
The values of i and j will range from 0 to 1.
i: 1 j: 1, i: 1 j: 2, i: 2: j: 1, i:2: j:2,
The values of i and j will range from 0 to 1.
i: 0 j: 0, i: 0 j: 1, i: 1: j: 0, i:1: j:1,
Correct! The values of i and j range from 0 to 1 and the inner loop executes twice each time i changes.
Checkpoint5.34.7.
Q-7: What does the following code print?
l1 = [1, 2]
l2 = [3, 4]
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
[4, 6]
This would be true if there was a single loop using an index to loop though both lists
[4, 5, 5, 6]
It adds l1[0] and l2[0], then l1[0] and l2[1], then l1[1] and l2[0], then l1[1] and l2[1].
[1, 2, 3, 4]
This would be true if the two loops were one after the other instead of nested and it just added the value in each list
[13, 14, 23, 24]
The + adds the numbers together, it does not concatenate them.
Checkpoint5.34.8.
Q-8: What does the following code print?
l1 = [1, 2, 3]
l2 = [4, 5]
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
[5, 7]
This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest.
[5, 6, 7, 6, 7, 8]
This would be true if it looped through l2 and then l1 in the inner loop.
[5, 6, 6, 7, 7, 8]
For every value in l1 it loops through all the values in l2.
[14, 15, 24, 25, 34, 35]
The + adds the numbers together, it does not concatenate them.
Checkpoint5.34.9.
Q-9: What does the following code print?
l1 = [1, 2, 3]
l2 = [4, 5]
out = []
for val1 in l2:
for val2 in l1:
out.append(val1 + val2)
print(out)
[5, 7]
This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest.
[5, 6, 7, 6, 7, 8]
For every value in l2 it loops through all the values in l1.
[5, 6, 6, 7, 7, 8]
This would be true if looped through l1 and then l2 in the inner loop.
[41, 42, 43, 51, 52, 53]
The + adds the numbers together, it does not concatenate them.
Checkpoint5.34.10.
Q-10: What does the following code print?
l1 = ['1', '2', '3']
l2 = ['4', '5']
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
[5, 7]
This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest and the items were numbers.
[5, 6, 7, 6, 7, 8]
This would be true if it looped through l2 and then l1 in the inner loop and the list items were numbers.
[5, 6, 6, 7, 7, 8]
This would be true if it looped through l1 and then l2 in the inner loop and the list items were numbers.
['14', '15', '24', '25', '34', 35']
Since the list items are strings the + will concatenate the values.