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.
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.
'Tony?' contains a question mark, so taking becomes False. From then on nothing is appended, not even 'Bucky'.if branch, which only sets taking to False: it is never appended.taking is False it stays False, so 'Bucky' is not appended either.known_names, not a single name.known_names, not its last name.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
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.
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.
lowest value and replaces it when it is lower: the Min pattern (here starting from 100 instead of the first element).taking variable, and nothing stops the loop from looking at the later grades.lowest is replaced, not increased.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.
None: the Find pattern (find-last variant).last_dog is replaced by a matching animal, not combined with every animal.Based on the description, determine the most appropriate loop pattern OR list operation.
| Description | Pattern 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. |
Every pattern or operation is used exactly once. First ask whether you need a loop at all:
| Description | Pattern or operation |
|---|---|
| I need to find the last value in a list of numbers. | List indexing (numbers[-1]) |
| I need to get the first five elements in a list of numbers. | List subscripting (numbers[:5]) |
| I need to get the last even number in a list. | Find pattern |
| I need to find all the even numbers in a list. | Filter pattern |
| I need to triple the value of all the numbers in a list. | Map pattern |
| I need to determine how many numbers are in a list. | Count pattern |
| I need to add up all the numbers in a list. | Sum pattern |
| I need to get all the numbers in a list until we encounter zero. | Take pattern |
| I need the lowest number in a list. | Min pattern |
| I need the highest number in a list. | Max pattern |