Computer Science I / Variables, Expressions, and Types
Practice question · True or false

In Python, a = [1,2,3] then b = a then b.append(4) leaves a with three elements.

Hints
  1. Does assignment copy the list or name it?
  2. Both names refer to one object.
Show the answer

False

Why

False. b = a binds a second name to the same list, so a has four elements afterwards. Only an explicit copy — a[:], list(a), copy.deepcopy for nested data — makes an independent object. This is the single most common source of surprise in Python and the reason is differs from ==.

Read the lesson: Variables, Expressions, and Types →

Practise Variables, Expressions, and Types

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Variables, Expressions, and Types