We will discuss how lists are stored so that we can understand what happens when they are copied. The ideas of aliasing and references will be useful more generally. # To prepare for lecture: ### To read/watch before lecture: - This video does a good job of explaining how lists are stored and what aliasing is, then giving an example of code that is incorrect because of aliasing: <a href="https://www.youtube.com/watch?v=7m_cw30tyr0">Python - Episode 7 - Aliasing</a> - The first 3:40 minutes of the following video gives another explanation of how lists are stored and then gives some ways to get a shallow copy (the rest of the video does talk about shallow/deep copies, but does so using objects/classes, which we haven't talked about yet (but will in 2 weeks)): <a href="https://www.youtube.com/watch?v=DenIgu8oimI">Shallow Copies versus Deep Copies: What you need to know to work with lists effectively and bug-free</a> - Finally, the first 1:20 minutes of this video says a little more about copying: <a href="https://www.youtube.com/watch?v=VUP9idz027o">How to copy a list in Python</a>. (Yes, the entire video is only a minute and a half long; the last 10 seconds looks at how to do this in Python 2 as opposed to Python 3.) - Read sections 10.10, 10.11, and 10.12 in <a href="https://runestone.academy/runestone/books/published/thinkcspy/index.html">How to Think Like a Computer Scientist: Interactive Edition</a> - Keep going on part 2 of assignment 8. ### Worksheet: We'll be going over questions from the worksheet <a href="./exercises28.pdf">exercise28.pdf</a>. - The first 3 questions look at how lists (and objects, more generally) are stored. We will talk about aliasing and references in order to understand why the exercises print what they do. - Questions 4, 5, and 6 look more closely at how .copy() works and highlights the distinction between a shallow copy and a deep copy. - Question 7 is deliberately confusing and meant to be a challenge question. Feel free to skip it . . . I may well end up skipping it. - Question 8 is an open-ended question that we will talk through as time permits. It will motivate the next topic in the course. # Notes from after lecture: - We went through exercises 1-6 from the worksheet, discussing references, aliasing, and the distinction between a shallow copy and a deep copy. I drew a lot of diagrams, so thankfully I correctly saved the marked up version of the worksheet and it's available <a href="./exercises28_markup.pdf">here</a>. The code is <a href="./lec28code.py">here</a>. - The video is on Box and available <a href="https://pomona.box.com/s/qp0nx5978a7s3an89ptiw4nea623yxb7">here</a>. I've editted out the quiet time during the two polls (exercises 2 and 3) and the breakout room (for exercise 5).