Lists, Aliasing, g, Mutability Kelly Rivers and Stephanie Rosenthal - - PowerPoint PPT Presentation

lists aliasing g mutability
SMART_READER_LITE
LIVE PREVIEW

Lists, Aliasing, g, Mutability Kelly Rivers and Stephanie Rosenthal - - PowerPoint PPT Presentation

Lists, Aliasing, g, Mutability Kelly Rivers and Stephanie Rosenthal 15-110 Fall 2019 Announcements Homework 3 Check-in 2 is due Monday at 12-noon! Start Early! Homework 3 full is also due in 2 Mondays. Learning Objectives To define


slide-1
SLIDE 1

Lists, Aliasing, g, Mutability

Kelly Rivers and Stephanie Rosenthal 15-110 Fall 2019

slide-2
SLIDE 2

Announcements

  • Homework 3 Check-in 2 is due Monday at 12-noon!

Start Early!

  • Homework 3 full is also due in 2 Mondays.
slide-3
SLIDE 3

Learning Objectives

  • To define mutable and alias
  • To distinguish mutable types from immutable types.
  • To trace the values of variables by understanding their mutability and

aliases

  • To create 2D lists and iterate through them
slide-4
SLIDE 4

Lists

Lists represent sequences, in order, of data We can define, add, remove, combine, edit, iterate over lists. X = [1,2,3,4,5,6,7] What is happening in memory? How can we keep adding/removing?

slide-5
SLIDE 5

Lists and Memory ry

X = [1,2,3,4,5,6,7] A variable points to a large place in memory designated for the list

1 2 3 4 5 6 7 Memory Variable List X

slide-6
SLIDE 6

Lists and Memory ry

X = [1,2,3,4,5,6,7] A variable points to a large place in memory designated for the list This space allows it to add and remove values without running out

1 2 3 4 5 6 7 Memory Variable List X

slide-7
SLIDE 7

Lists and Memory ry

X = [1,2,3,4,5,6,7] Y = [1,2,3,4,5,6,7] #new list, new memory Python assumes you’ll want to edit them separately

1 2 3 4 5 6 7 1 2 3 4 5 6 7 Memory Variable List X Y

slide-8
SLIDE 8

Lists and Memory ry

X = [1,2,3,4,5,6,7] Y = X+[] #new list, new memory Python assumes you’ll want to edit them separately

1 2 3 4 5 6 7 1 2 3 4 5 6 7 Memory Variable List X Y

slide-9
SLIDE 9

Aliasing

X = [1,2,3,4,5,6,7] Y = X Setting another variable equal just points to the same address in memory

1 2 3 4 5 6 7 Memory Variable List X Y

slide-10
SLIDE 10

Modify fying Lists with Aliasing

We can directly add or remove an item to/from a list! L = [1,2,3,4,5,6,7,8,9]

L.append(10) #10 gets inserted at the end of the list L.remove(5) #5 gets removed from the list L.remove(5) #nothing happens, there is not a second 5 L.insert(4,5) #insert 5 at index 4 of the list

slide-11
SLIDE 11

List Behavior with Aliasing vs Separate Copy

X = [1,2,3,4,5,6,7] Y = [1,2,3,4,5,6,7] #new list, new memory Z = X X.remove(3) print(X) print(Y) print(Z) What wil ill l the li lists lo look li like?

slide-12
SLIDE 12

List Behavior with Aliasing vs Separate Copy

X = [1,2,3,4,5,6,7] Y = [1,2,3,4,5,6,7] #new list, new memory Z = X

1 2 3 4 5 6 7 1 2 3 4 5 6 7 Memory Variable List X Y Z

slide-13
SLIDE 13

List Behavior with Aliasing vs Separate Copy

X = [1,2,3,4,5,6,7] Y = [1,2,3,4,5,6,7] #new list, new memory Z = X X.remove(3)

1 2 3 4 5 6 7 1 2 4 5 6 7 Memory Variable List X Y Z

slide-14
SLIDE 14

Checking Memory Locations

id(X) will return the memory location of the variable X If id(X) == id(Y) then the variables are aliased Otherwise, the values of the data are in separate memory locations

slide-15
SLIDE 15

Aliasing Takeaways

Be careful if you are using lists and creating aliases vs copies You may expect a list to change and it doesn’t or vice versa Use the one that is correct for your algorithm/application

slide-16
SLIDE 16

Why are copies different than aliases?

Lists are mutable le – you can change the value of a list without changing its memory location Insert, Append, and Remove all change the list in its location Aliases point to the same location in memory, so they edit the same list. Copies point to different locations in memory, so the lists are not the same.

slide-17
SLIDE 17

Why are copies different than aliases?

Lists are mutable le – you can change the value of a list without changing its memory location Insert, Append, and Remove all change the list in its location Aliases point to the same location in memory, so they edit the same list. Copies point to different locations in memory, so the lists are not the same. Not all data types are mutable. The opposite of mutable is im immutable le.

slide-18
SLIDE 18

Are Strings Mutable?

S = "15" print(id(S)) S = S+"-110" print(id(S)) Do they print the same value?

slide-19
SLIDE 19

Are Strings Mutable?

S = "15" print(id(S)) S = S+"-110" print(id(S)) Strings are immutable. Each string is stored in a separate location in memory.

1 5

  • 1

1 1 5 S Before + After +

slide-20
SLIDE 20

Are In Integers and Floats Mutable?

S = 15 print(id(S)) T = S S = S+1 print(id(S)) print(id(T)) print(S,T) Do they have the same id? Do they have the same value?

slide-21
SLIDE 21

Are In Integers and Floats Mutable?

S = 15 print(id(S)) T = S S = S+1 print(id(S)) print(id(T)) print(S,T) Numbers are im

  • immutable. There is a separate location in the program for each.

S Before + After + 15 16 T Always

slide-22
SLIDE 22

Swapping Variable Values

S = 5 T = 10 Temp = S S = T T = Temp

S 10 5 T

slide-23
SLIDE 23

Swapping Variable Values

S = 5 T = 10 Temp = S S = T T = Temp

S 10 5 T Temp

slide-24
SLIDE 24

Swapping Variable Values

S = 5 T = 10 Temp = S S = T T = Temp

S 10 5 T Temp

slide-25
SLIDE 25

Swapping Variable Values

S = 5 T = 10 Temp = S S = T T = Temp

S 10 5 T Temp

slide-26
SLIDE 26

Swapping List Values

L = [1,2,3,4,5,6,7] Temp = L[1] L[1] = L[2] L[2] = Temp

L 1 2 3 4 5 6 7

slide-27
SLIDE 27

Swapping List Values

L = [1,2,3,4,5,6,7] Temp = L[1] L[1] = L[2] L[2] = Temp

L 1 2 3 4 5 6 7

slide-28
SLIDE 28

Swapping List Values

L = [1,2,3,4,5,6,7] Temp = L[1] L[1] = L[2] L[2] = Temp

L 1 2 3 4 5 6 7 Temp

slide-29
SLIDE 29

Swapping List Values

L = [1,2,3,4,5,6,7] Temp = L[1] L[1] = L[2] L[2] = Temp

L 1 2 3 4 5 6 7 Temp

slide-30
SLIDE 30

Swapping List Values

L = [1,2,3,4,5,6,7] Temp = L[1] L[1] = L[2] L[2] = Temp

L 1 2 3 4 5 6 7 Temp

slide-31
SLIDE 31

2D Lists

Now that we understand what’s going on, it’s easier to understand different ways that we can use lists. Example: We can make lists of lists

L 1 2 5 6 7 Indexes represent row numbers Indexes represent column numbers

slide-32
SLIDE 32

It Iterating through 2D Lists

for i in L: for j in i: print(j) for i in range(len(L)): for j in range(len(L[i])): print(L[i][j])

L 1 2 5 6 7 Indexes represent row numbers

Note: L[i] is a list L[i][j] is an index in L[i]’s list