← Back

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

  1. Evaluate the rightmost expression once; that single object is what every name receives.
  2. Draw the arrows: each name in the chain points at the one object.
  3. Apply mutations to the object (all names see them); apply rebindings to one name only (the others keep the old object).
  4. For comparison chains, expand pairwise: a == b == c means (a == b) and (b == c).

Common wrong fixes

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.

Solve it in the interactive editor β†’

🐍 Learning path: Python Basics

Problem 12 of 12 in this path. Work them in order β€” each one builds on the rule before it.

← Comparison chains🏁 Path complete β€” review it β†’

More functions problems

Keep going