PRACTICE / UNDERSTAND / REPEAT
Python practice question
Practice functions with self-contained questions. Check your answer, then use the explanation to understand the reasoning.
1 questionAnswers explainedFree · no login
Try this question
What does `print(add(3))` display after the first two calls?
def add(value, items=[]):
items.append(value)
return items
add(1)
add(2)
print(add(3))
Answer & explanation
Answer: [1, 2, 3]
The default list is created once when the function is defined, so values appended by earlier calls remain in it.