- Hello! Welcome to the Path to Programming; the audio supplementary materials for introduction to computer science! - I am a former student that took this introductory course and today I want to talk to you about Lists and Memory Diagrams! - If you remember the very first episode we talked about the memory model, and how imperative it was when it came to understanding how programming works on a basic level. We talked about how the memory model helps you understand variable assignments and by extension how a program runs. - You can think of a Memory Diagram as an extension of this model, as they will better help you understand how lists work, and what goes on behind the scenes in your computer when you code a list. - Last episode we mentioned how a list is a data type but we didn’t get into the specifics of what a list is or how it works. We touched on the fact that a list is also a type of sequence, which has indices. - What separates a list from other types of sequences like strings is the fact that they are mutable. - You might be wondering what exactly this means, and no it doesn’t mean you can silence a list! - Instead it means that you can change the contents of a list even after creating it. - Once you write a string, it’s final, if you made a typo there’s no going back, you would have to write a new string with the correct spelling, you can’t go back and individually edit the characters. - But with lists you can manipulate them after creating them, you can change individual items or the length of a list that you have created, and you can do this without any new variable assignments. - Lists can store elements of different types (e.g., integers, booleans, strings). - They are usually homogeneous, meaning that all of the elements in a list are of the same type, but Python allows heterogeneous lists too. - A list with no elements is called an empty list. - You initialize a list by using brackets! - Like we have always done, you need to assign a list that you create to a variable so you can access it later on. - There are a lot of keywords for operations in lists you will need to learn so that you can use the data type of lists in the most efficient way possible. - An important keyword you should know is “in”, this operation allows you to check whether an element is in your list. - If you had created a list of numbers from 1 to 10 and you wanted to see if it contained the number 15, you would use “in” and get a Boolean value, and in this case it would be false since the list you created only goes up to 10. - Another important keyword that you will be frequently using is “append”. - Append is a method that allows you to add an element to a list that you have created. - It has a single parameter, in which you enter the element you want to add to the list. - Append is very useful and powerful to use in the body of a loop so elements are added to a list on each iteration of a loop you can even append a list you've created to another list! - Append is like a command that states “add this to the end of the list”- make sure you memorize this! - The most used list method is append, because it is used to create new lists and add elements to them. - Another very important keyword is “pop.” - Pop allows you to remove an item from a list that you have created. - You enter an index as the parameter and the element at that index is then returned to you and it is also removed from the list. - If you don’t give pop a parameter, Python will assume the element at the last index should be removed. - The final method you should know and memorize is “insert”. - Insert has two parameters, the first is the index and the second is the element that you want to insert before that index. - Because it is more specific compared to append (remember append just adds to the end of the list that’s why it is super useful when iterating) we don’t use it as often but nonetheless it is very useful. - Like we talked about last week, lists have indices as they are a type of sequence. - Therefore lists have the ability to do indexing and slicing. You can use the same operations we talked about last week, to for example get specific elements from a list using indexing, or getting only a subset of a list using slicing. - Now let's start talking about memory diagrams. - As we mentioned in the very first week, visual tools can be really helpful to use when you are trying to understand how a program works behind the scenes. - Memory diagrams are a visual tool as they represent lists and help us understand how each of the methods we described work on lists. - It wouldn't be much help to you if I just sat here and described what a memory diagram looks like, my tip would be for you to search it up and familiarize yourself with it. - Make sure that you draw a memory diagram the first few times you encounter a problem or code that uses lists. Every time you draw a memory diagram you will get one step closer to internalizing how lists and the operations work. - Memory diagrams will help you understand which elements are contained within each index. They will become increasingly useful as you start encountering lists that have lists within them and have to use multiple indices. - Please make sure to utilize this tool as much as you can, until it becomes second nature to you! As a former student it definitely helped me really understand how indices and specifically lists work and it made using lists very enjoyable to me! - An important thing the memory diagrams can help you realize is the difference between aliases and twins. So what exactly are aliases and twins? - An alias implies that you have assigned the same list to two different variables. So you could have a variable a and a variable b pointing to the same list. When you access the list using the name a and make a change, the list will also reflect that change when you access the list using variable b. - Basically when you have an alias when you make a change you make using one variable also appears as a change in another variable. In terms of a memory diagram it implies that the two arrows from the variable names are pointing to the same thing, so when you mutate either there’s only one operation going on behind the scenes. - To make things more clear let’s think of a real life example that reflects this situation. There is a special agent named Joe, for his work he uses the name Bond. When Joe goes to the tattoo store and gets a tattoo so does the alias Bond because they are actually the same person, even though they use different names when needed. - Another scenario that arises from naming variables is the twins/clones example. - You can think of this scenario as the opposite situation of aliases. Twins arise when you assign different lists to different variables but the lists are the same. When you mutate one list that is technically identical to the other, the other one doesn't change because they have different variables assigned to them. So even though the lists contain the exact same elements, because they were created separately and accessed using different variables, mutating one doesn't mutate the other. - Following the tattoo example we used if Joe had an identical twin named James (so they have the same DNA) if Joe got a tattoo it wouldn't mean that James got a tattoo. The fact that they’re identical to start with doesn't mean a change happens to both of them. - Memory diagrams help make this clear: in a twins situation, two separate variables point to two separate lists that have the same exact content, so you can see that if one were to change, the other wouldn't. - We mentioned that lists are special because they are mutable, you can alter a list even after creating it. As a counter-example we said that strings are immutable. - Another type of immutable objects is a tuple. What are tuples? Tuples are essentially like lists but they are immutable. - The name comes from "quadruple", "quintuple," etc.: those words end with 'tuple,' so the general form with an unspecified number of elements is called a "tuple." - Tuples are still a form of sequence that contain elements, which are separated by commas and contained in parentheses. So when initializing a list if you recall you would use square brackets, but when initializing a tuple you would use regular parentheses. Like lists the elements in a tuple are separated by commas. - You can use operations we’ve used on strings like indexing and slicing since those operations don’t change a sequence, they just access information. But you can’t use the new operations we learned in this episode like append and pop on tuples because tuples are immutable and you can’t change a tuple after you’ve created one. - One last thing I want to quickly touch on in this episode is nested loops. This is a complicated topic that we will hopefully expand up on but I wanted to tie it into this episode because just like we mentioned that there can be a list inside a list, there can also be a loop inside a loop! - These types of loops are usually the for loop type we talked about and they’re called nested loops. The loop that is inside the body of the for loop is called…. As you might have guessed, the inner loop. - The entire inner loop is executed each time the other loop is iterated, just like the rest of the commands in the outer loop body. - I would suggest that you experiment with loops yourself as they can be complicated to understand without trying them on your own! They are a very useful tool and can be used in combination with lists inside of lists. - The outer for example may be used to access a single list from a list of big lists and the inner loop may be used to iterate through the elements within the list accessed. Obviously hearing that is very mind boggling so I would urge you to try it on your own! - We've come to the end of another episode of path to Programming! - This week we talked about lists and memory diagrams and touched on nested loops as a concept. - Lists are very important and they are a type of sequence, what makes lists special is that they can be mutated, which means that they can be changed after they have been created. Some operations to change lists after creation that we touched on are append, pop and insert. - A way to understand how lists work and operate behind the scenes is to use memory diagrams. So as we said please make sure you use them to better understand lists and their operations! We gave an example to the complications that can be resolved by using memory diagrams with the alias and twin example. - And finally today we touched on the concept of for loops and how they can be useful. - I hope you enjoyed this week’s episode. Talk to you next time!