Check your understanding of the previous reading: the Count, Sum and Accumulation patterns, and what goes inside the body of a loop.
After the third iteration of the loop, what is the value of sum?
sum = 0
values = [4, 2, 6]
for value in values:
sum = sum + value
sum starts at 0, and the three iterations add 4, 2 and 6: 0 + 4 = 4, 4 + 2 = 6, 6 + 6 = 12. The answer is 12.
What is the value of any after the following Boolean Accumulation is executed?
conditions = [False, False, True, False]
any = False
for condition in conditions:
any = any or condition
print(any)
any starts as False and stays False through the first two conditions. The third condition, True, makes any or condition True, and True or False keeps it True. The answer is True.