Home βΊ Problems βΊ Chained assignment
Chained assignment
π§ Predict the output Topic: functions Difficulty: easy Free
What this problem practises
Function bugs are mostly about names and values: positional argument order, default parameters evaluated once at definition time, local versus global scope, and the implicit None returned whenever a function has no return.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: easy β the fix or the trick is normally a single line, and a careful read should be enough.
The challenge
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
a = b = c = 10
a += 5
print(a, b, c)Understand the rule
a = b = [1, 2] assigns the SAME object to both names, right to left: the list is created once, then bound to b and to a. For immutable values (numbers, strings) this is harmless. For mutable ones it means a.append(3) is visible through b β the classic chained-assignment surprise. Comparison chains like a == b == c are a separate feature that also appears in these puzzles.
How to trace it
- Evaluate the rightmost expression once; that single object is what every name receives.
- Draw the arrows: each name in the chain points at the one object.
- Apply mutations to the object (all names see them); apply rebindings to one name only (the others keep the old object).
- For comparison chains, expand pairwise: a == b == c means (a == b) and (b == c).
Common wrong fixes
- Believing a = b = [] creates two independent empty lists.
- Confusing later rebinding (b = [9]) with mutation β rebinding detaches b without affecting a.
- Reading 1 < x < 5 as (1 < x) < 5 with a boolean in the arithmetic, instead of the chained pairwise test.
Rule to remember
Chained assignment evaluates the right side once and shares that object across all names. With mutable objects, sharing means every name sees every mutation.
π Learning path: Python Basics
Problem 12 of 12 in this path. Work them in order β each one builds on the rule before it.
More functions problems
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every functions problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.