Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1def add_item(items): items.append(5) | Acopy = items does not make a copy. Both names point to the same list, so the original is still changed. Use items[:] instead. | |
| 2copy = items[:] | Bdef clear_first(items): items.pop(0) nums = [1, 2, 3] clear_first(nums) | |
| 3To keep the original list safe, copy it inside the function and return the changed copy | CMakes a new separate list with the same values, so changes to the copy do not affect the original | |
| 4Passing a list to a function and changing it inside means the caller's list is also changed | DChanges the original list in place, because append works on the same list that was passed in | |
| 5def add_item(items): copy = items copy.append(5) return copy | Edef add_bonus(scores): new_scores = scores[:] new_scores.append(10) return new_scores | |
| 6def add_item(items): items.append(5) nums = add_item([1, 2]) | FThe function changes the list in place and returns nothing, so nums is None. Either return the list or use the original list after the call. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
copy = items[:] — A function can change a list you pass in, altering the original in place. Make a copy with [:] first if you need to keep the original list unchanged.def bonus(marks):
marks.append(10)
print(marks)def safe(items):
copy = items[:]
copy.append(1)def extend(items):
new = items[:] + [9]
return newscores = [7, 8]
grow(scores)
print(scores)backup = scores[:]
grow(scores)
print(backup)