Skip to lesson
0/174 complete
LEARN THE CONCEPTFree forever

Mutability, references, and copying

Topic 5 of 7 · Programming foundations

What is a program?

A program is a set of instructions a computer follows to work with data. A variable gives a name to a value, a condition chooses what to do, a loop repeats work, and a function groups instructions you can reuse. We will introduce each of these before using them in an algorithm.

Example: To add three marks, store [70, 80, 90], visit each number, and add it to a running total. The final total is 240.

INTERACTIVE WALKTHROUGHPredict · step · explain
SHARED CHAPTER LAB · ARRAY

Start with a real element, not an invented zero.

04
11
27
32
1 / 4

Step 1 of 4 · best = 4

FoundationsWorked exampleTry it yourself
Self-paced learning

Understand the idea

Mutable objects can change after creation. Assignment shares a reference; a shallow copy creates a new outer collection while retaining references to nested objects. Aliasing explains many surprising updates.

Work through an example

a=[[1],[2]]; b=a.copy(); b[0].append(9) changes a[0] too. But b.append([3]) changes only b. Both lists reference the same inner [1,9].

Time and space complexity

A shallow list copy is O(n) time and O(n) extra references. A deep copy depends on all reachable data.

Why it works

Shared objects stay shared until a new object is deliberately created.

Common mistakes

  • [[0]*3]*2 aliases one row twice.
  • Mutating a caller's list is observable and must be part of the contract.

Chapter lab · Python implementation

Our running task is to find the largest value in a sequence. A program turns a precise input/output contract into small state changes. Read the example from the top, predict each update, and run its assertions. Python examples use ordinary built-in types; statements inside an indented block belong to that block.

This shared lab connects the chapter's topics. Follow the current lesson's worked example first, then trace the complete implementation. Additional algorithms described in topic exercises are variations you can implement independently.

def largest(values):
    if not values:
        return None
    best = values[0]
    for value in values[1:]:
        if value > best:
            best = value
    return best

def largest_without_slice(values):
    if not values:
        return None
    best = values[0]
    for i in range(1, len(values)):
        best = max(best, values[i])
    return best

Run and test the lab

Copy the implementation and these assertions into a Python 3 file and run it. An assertion failure means the actual result differs from the expectation. Add the current topic's exercise and boundary tests to deepen your understanding.

assert largest([]) is None
assert largest([-8, -3, -5]) == -3
assert largest_without_slice([4, 1, 7, 2]) == 7
assert largest_without_slice([0]) == 0
CHECK YOUR UNDERSTANDING

Does b=a make an independent list?

Further reading

Original documentation for the implementation details used in this chapter.

Python data structures
Course outline
Software Engineer
DSA
THE SOFTWARE ENGINEERING PATH

Data Structures
& Algorithms

Build intuition. Develop a pattern.
Make it your own.

Free course30 chapters
Your progress0%

0 of 174 published lessons complete