 In this video we are going to look into more detail in how the memory works. In particular we are focusing on how list objects are represented in memory. So let's start a new file and let's call it who am I and how many. That is the name I gave to this example in the book and so now let's do it. So what I'm going to do in the following is I will give you one example and I will do the example twice using different data types and I do that so that you can see a difference and yeah this is in my opinion the best way of how you learn the following concepts. So let's start with a very simple example. So let's go ahead and create a variable let's call it a and assign it the number 42. Okay so now what happens in memory is the following. As we saw in a previous video first the right-hand side is going to be evaluated that creates a new object with the number 42. This is of course an integer and then after the right-hand side is done on the left-hand side in the name section in memory we are going to create a variable called a and we make the variable reference the new object. Okay so this is nothing new so let's continue with something we have not seen before. Let's go ahead and assign to a new variable b whatever a is and to people that are new to programming this may already be confusing because many people feel this as kind of an equation. So they say okay b is equal to a okay and they think that this must be the case for all times now and this is not the case. So in programming when you use the assignment statement this only assigns the right-hand side to the name on the left-hand side in this in in this second so to say. So later on a and b may be different the objects however let's see what happens in in a computer's memory. So first the right-hand side is evaluated so we follow on the left-hand side we look up the name a then we follow the reference to the object here and then we get back as the result a reference to the 42 object and then we create a new variable call it b and we make that reference the return value which is the same object. So now this is the first time in this course where we have an example where we have one object that has more than one references going to it. Okay and now non-trivial if I ask Python what is a it simply tells me well it's 42 if I ask Python what is b it also tells me it's 42 okay that is almost trivial I would say but now let's go ahead and assign to a the number 87 okay let's execute this and let's see what happens in memory. So in memory on the right-hand side the right-hand side is again evaluated first so this creates a new object with the number 87 in it also an integer and so and then what we do is on the left-hand side we're going to follow the a or follow all the names here until we find in the name a and then what we're going to do is we're going to remove the reference starting at a and we make a reference the new object. Okay so now if we that if that is what it's going on in memory if we now go ahead and ask Python hey what is a it will non-trivially tell us it's 87 okay it is trivially and then if we ask a b we get back 42 okay and that is what I meant when I said that some people that are new to programming may be confused by a line like this like where we set b to whatever a is but this only regards this moment in time so below here we see that both a and b refer to different objects and even though at some point they refer to the same object okay so that is a that is the simple version of this example so maybe let me give that a nice title let's call it working with quote simple objects okay so now we are going to do the same example but we're using a different data type okay so let's correspondingly call this section working with quote complex objects whatever that is no definition yet it's just the same example the same idea of the example with just a different data type so let's create a new variable and let's call it x and x is going to be set to a list with the numbers one two and three in it so let's keep that simple okay and let me execute this cell so whatever but that does it it is going to create a list object so now we have to ask ourselves how are list objects represented okay so I will tell you how this works so first on the right hand side Python is going to it's going ahead and we'll create a big object and I usually draw it like this and it will put so-called slots in it so what are slots these are basically areas that are all of the same size and they can all store the same kind of data the same number of zeros and ones and then this object is also of a certain data type namely of the list data type okay and we will see that soon in in the code cell okay so now that is only now so far we only have evaluated the practice so we only created a list so now Python will go from left to right inside the list expression here and we'll create the numbers one by one so first we're going to create the number one so what that is it's going to be like this just as before and as above in this video we're going to create a new object and put the number one in there and it's also going to be an integer object and now because this object is going to be inside the list object well what we are going to do we go from the first slot in the list object and we will make a reference go to the one object here okay and then Python will go to the next number which is the number two and we'll create the number two maybe here so this just to illustrate that we have no idea where in memory these objects are going to be and then Python will go ahead and will create a reference going here and then last but not least Python will create a third object a third number and let's say we put it right here and this of course also going to be an integer object and the reference will follow or we'll go down from here okay so the first thing we learn from this is that Python does not store the numbers inside the list really so the only thing that is ever stored inside the list object are the references okay and references are just memory addresses so big numbers the ones we saw are using the ID function in a previous video okay so now this is the right hand side okay the right hand side has now been evaluated and now we will go ahead and we will evaluate the left hand side which basically simply creates a name X and makes X reference the list okay so here on the left hand side we are going to create X and X will reference the list object okay so what I mean when I say we are dealing with a so to say complex object means it is complex because we have a reference to an object which then which itself contains further references to other objects and there's nothing that would prevent us from having other list objects inside the list object that also reference further objects so we can have a very complex structure here in memory okay this is just one layer structure so to say one level structure so now in the code cell let's briefly ask Python hey what is the type of X and it will confirm to us it is list so you could have guessed this before we haven't we haven't seen this in the other video that introduced the idea of object orientation and data types but yeah this is just a data type here and now let's do the analog of what we did above so above here I created a variable called a and I created a second variable B and I made it point to whatever a points to okay so let's do the same thing here let's simply call it why and let's assign why to whatever X points to okay so in memory all that does here is the following is going to evaluate the right hand side which is simply X so what that means is we follow the X variable to the object and then the return value of the of the right hand side is going to be the reference to the beginning of the list object and this is given back to me and what am I going to do with the reference to list object well I'm going to assign it to a new variable let's call it why and make why point here as well okay so what means is when I set why to whatever X whatever X is that means this does not copy anything in memory okay the only thing that happens in memory is we create a new name which references an object that already existed before okay so we are not going to copy anything here and now let's briefly review what we can do with lists so first trivially X and Y are of course both the lists one two three and now as we briefly saw before when we introduced the first example of a Python program in this course was that we could also index into a list so let's say I want to index into the X list I use the indexing operator and in order to obtain the first element we have to index with the number zero okay so we learned that Python is a zero based programming language so whenever we have to count in Python then we start to count at zero and not at one okay so this is just the rule let's execute that and we get back to number one so if Python sees an expression like this with the indexing operator what happens in memory is as follows we will go ahead and we look up the name X we follow the reference and then inside these slots we will go into the first slot the slot number zero so sometimes what you will see me do is we could put in a different color just the index numbers right here okay so but usually I will leave that away because it's usually it's clear what the indexes are so we are going to follow the first index so in other words the first reference going to this object the object with the number one and then a reference a direct reference to the number one object is going to be returned so let me show that with a red dotted line okay so this is basically what we just did here when I evaluated this so here again in the color red here we see we get back to return value one and this is nothing but a reference to the object to the integer object one okay so let's do the following let's go ahead and change this object so let's go ahead and assign to the first index in the X list a new number let's keep it simple let's assign to it the number 99 okay let's execute this so this is now the first time we see the assignment statement being used and we don't have a clear and a name only on the left hand side we have something other than just the name on the left hand side so we are assigning to something other than just a name so we will in a bit we will see how that works but now as before we have to evaluate the right hand side first and what that means is we simply go ahead and we create a new object with the number 99 this is an integer and then what is going to happen is now just the right hand side is evaluated it's done so now we have to assign the reference to the 99 to whatever is on the left hand side and as we see here we are assigning to the zero index in the X list and what that means is we go to the X list up here we follow the X here go into the first slot and now because there is already a reference here what we are going to do is we are going to remove this reference and we will place in there a new reference and we will make this point to the number 99 and now what is going to happen is because the number one has no further references it has a reference count of zero this is going to be garbage collected okay and now let's go back let's go back to the code cells here and now if I ask you or if I ask Python hey what is X well Python would tell me it's a list with the numbers 99, 2 and 3 okay and if I now go ahead and ask Python hey what is the Y list this is probably a sort of confusion because without without the memory diagram many people especially beginners would say well Y should still be 123 okay because I only changed X here I did not touch Y here I only changed X so Y should Y change but if we ask Python what is Y we also get back the list 99, 2 and 3 and in the memory diagram we can easily see why this has to be the case because in total we only work with one list there is never a second list object as I said before there is no copy of the list okay so we just have two different names for the same list so in a way we can say that this object has like a primary name and kind of a nickname okay we just have two ways of referencing the same list object and that is an important idea okay so before that we never had an object that had more than one reference in this video for the first time we are dealing with objects that have more than one reference going to them and this is what we have to understand okay and let me finish this video with another discussion of an important concept so here in the list data structure here what we see is the following after I created the list in the beginning I had an initial set of references inside them the first three references okay and then later on after the list has already been created we changed part of the list okay so I have a list object and I changed a part of it I didn't change the entire list I did not create a new list I only changed the very first slot the reference in the very first slot here and all data types in Python but also in other programming languages that allow that that allow that after they have been created they can still be changed so the ones and zeros inside this box here can still be changed after we created it we call these data types mutable okay so let's maybe write this right here the word mutable and data types that cannot be changed after they are created for example the 99 here but also the three and also the two they are what we would call immutable okay so in other words as we saw in a previous video when we for example take two numbers and add them we will always get a new object back okay so the ones and zeros inside an object of type integer are never going to change once the object is created they will never change however for a list data type this is possible okay so after we change after we created it we can still change the ones and zeros inside them and that is a big distinct a big idea all right also so this is a concept that will apply also for other data types and in this video lecture in in or in this course we will discuss all the common data types that exist in Python and one dimension along which we are going to classify them is going to be whether or not they are mutable or not and right now I'm not going to into detail of what the advantages and disadvantages are for now let's just understand this idea that mutability exists and then in the future video we will learn what are the pros and cons of using mutable versus immutable data types okay so I see you soon