Want to make creations as awesome as this one?

Transcript

Initially, there are no variables and no objects

a = [1, “apple”] b = [2, 3] a.append(b) print(a[2][0], b[0]) b[0] = “banana” print(a[2][0], b[0]) b = 0

Variable Names

Objects in Memory

Memory address

Step 0

a = [1, “apple”] b = [2, 3] a.append(b) print(a[2][0], b[0]) b[0] = “banana” print(a[2][0], b[0]) b = 0

Variable Names

Objects in Memory

a

Memory address

Step 1

We created a variable named a which references a new list We also create an integer and a string The list contains references to the integer and string We can access the integer and string by providing an index to the list For example, a[0] accesses the value 1

[0]

[1]

int 1

list

str "apple"

Memory address

Step 2

We created a list named b which references two new integers

a = [1, “apple”] b = [2, 3] a.append(b) print(a[2][0], b[0]) b[0] = “banana” print(a[2][0], b[0]) b = 0

Variable Names

Objects in Memory

list

[0]

[1]

str "apple"

int 1

a

[0]

[1]

list

int 3

int 2

b

Memory address

Step 3

The append method of the list causes a reference to the list referenced by b to be added to the end of aa[2] and b are now both ways to access the same list

a = [1, “apple”] b = [2, 3] a.append(b) print(a[2][0], b[0]) b[0] = “banana” print(a[2][0], b[0]) b = 0

Variable Names

Objects in Memory

list

[0]

[1]

str "apple"

int 1

a

list

[0]

[1]

int 3

int 2

b

[2]