Check your understanding of the previous reading: what Take and Find loops produce, which pattern a piece of code uses, and which pattern or list operation fits a task.

Question 1

After this code is executed, what is printed?

names = ['Tasha', 'Bruce', 'Tony?', 'Steve?', 'Bucky']
known_names = []
taking = True
for name in names:
    if "?" in name:
        taking = False
    elif taking:
        known_names.append(name)

print(known_names)

Select one answer, then check.

  • Correct. 'Tony?' contains a question mark, so taking becomes False. From then on nothing is appended, not even 'Bucky'.
  • Not quite. The name that matches the condition goes to the if branch, which only sets taking to False: it is never appended.
  • Not quite. That is what a Filter would give. Once taking is False it stays False, so 'Bucky' is not appended either.
  • Not quite. The program prints the list known_names, not a single name.
  • Not quite. The program prints the whole list known_names, not its last name.
  • Not quite. The Take pattern keeps the elements before the condition is first met, not the ones from that point on.

Question 2

After this code is executed, what value is stored in my_value?

my_value = None
values = ["Hi", "there", "my", "friend"]
for value in values:
    if value.upper() == "MY":
        my_value = value

Show worked solution

Only "my" becomes "MY" when it is upper-cased, so the Find pattern stores that element: my_value holds the string "my". It stores the original element, not its upper-cased version.

Question 3

Which pattern does this code most resemble?

lowest = 100
grades = [90, 64, 72, 50]
for grade in grades:
    if lowest > grade:
        lowest = grade
print(lowest)

Select one answer, then check.

  • Correct. Each grade is compared with the accumulated lowest value and replaces it when it is lower: the Min pattern (here starting from 100 instead of the first element).
  • Not quite. A Filter builds a new list of the items that pass a condition; this loop keeps a single value.
  • Not quite. There is no taking variable, and nothing stops the loop from looking at the later grades.
  • Not quite. Nothing is added up: lowest is replaced, not increased.
  • Not quite. The conditional compares each value with the accumulated value, which is what makes this the Min/Max pattern rather than a plain Accumulate.

Question 4

Which pattern does this function implement?

def get_dog(animals: list[str]) -> str:
    last_dog = None
    for animal in animals:
        if "dog" in animal.lower():
            last_dog = animal
    return last_dog

Select one answer, then check.

  • Correct. It walks through the whole list and keeps the last animal that matches the condition, starting from None: the Find pattern (find-last variant).
  • Not quite. A Filter would append every matching animal to a new list; this keeps only one.
  • Not quite. Nothing stops the loop from taking elements: every animal is checked.
  • Not quite. The condition doesn't compare each animal with the stored value; it only asks whether the animal is a dog.
  • Not quite. last_dog is replaced by a matching animal, not combined with every animal.

Question 5

Based on the description, determine the most appropriate loop pattern OR list operation.

DescriptionPattern or operation
I need to find the last value in a list of numbers.
I need to get the first five elements in a list of numbers.
I need to get the last even number in a list.
I need to find all the even numbers in a list.
I need to triple the value of all the numbers in a list.
I need to determine how many numbers are in a list.
I need to add up all the numbers in a list.
I need to get all the numbers in a list until we encounter zero.
I need the lowest number in a list.
I need the highest number in a list.