Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

mutability-copying

Mutability and copying

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Mutability and copying: the syntax, patterns, and safety habits you need before advancing in Python.

Teams still ship Mutability and copying in Python codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Mutability and copying in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true). Also remember default arguments with mutable defaults are a common bug.

When you can explain the previous lesson's ideas without copying starter code.

Assignment binds names to objects—it does not copy. Shallow copies duplicate the container but share nested objects; deep copies recurse—critical when mutating nested lists or dicts.

Aliasing vs copy

a = [1, 2, [3]]
b = a          # alias
c = a.copy()   # shallow
import copy
d = copy.deepcopy(a)

Nested mutation pitfall

a = [[1], [2]]
b = a.copy()
b[0].append(99)
print(a)  # [[1, 99], [2]] — inner lists shared

Contrast with value semantics for small structs in C#—Python always passes object references.

Important interview questions and answers

  1. Q: Shallow vs deep copy?
    A: Shallow copies top-level container; deep copy clones nested mutable objects recursively.
  2. Q: Default argument list pitfall?
    A: Shared mutable default persists across calls—use None sentinel.

Self-check

  1. Does b = a copy a list?
  2. When do you need copy.deepcopy?

Pitfall: .copy() is shallow—nested lists still alias until copy.deepcopy.

Interview prep

Shallow vs deep?

Shallow copies container; nested objects shared—deepcopy clones the full tree.

Assignment vs copy?

b = a aliases; b = a.copy() new outer list with shared innards if nested.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Shallow vs deep?
  • Default arg trap?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump