Questions / Quant development, Python correctness
Augmented assignment can preserve aliases
What sum is printed?
a = [3, 4]
b = a
a += [7]
print(sum(b))
Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.
Show the answer and worked solution
Answer: 14 value
- a and b initially refer to the same list. Extending a in place makes b contain 3, 4, and 7, whose sum is 14.
Interview shortcut. list += mutates in place; aliases see it.
In the app this has a follow-up where one thing about the setup changes. Try the follow-up.