 In this video, we are going to learn about the concept of object orientation. So let's first create a new notebook and rename it into Objects. And let's make a nice title, Object Orientation. So what is object orientation? Well, every programming language has its own paradigm on how it manages the memory inside of a computer. And Python's memory management is following the object-oriented paradigm. So that is why we have to learn about objects and some other ideas. So let's do that with a couple of examples. First, we are going to create a variable A and assign to it the number 42. Then we create a variable B and assign to it the number 42.0. So know the difference. This will be a different number, even though it has the same value. The third example will be a variable C and here we will model textual data and we will put in there a message, Python rocks. Let's execute this code cell. And let's realize here that there is no output below the code cell here. So previously we have had many different code cell examples and some of them had some output below them and some of them didn't. And now in this chapter, in this video, we are going to look into why this is the case, why some of them have output and others don't. So in order to understand that, we first have to understand how a computer manages its memory. So to do so, I will try to teach you the very basics of a computer's memory by making a couple of nice drawings here. So here is a piece of paper. It's just paper as you would use it in your math lecture, for example. So this is what you should think of, what a computer's memory looks like conceptually. So it is a big thing that has many small squares in them. They are all the same size. And now each of these squares can hold one of two values. It could either hold, let's say, a zero or maybe a one. And why this is the case? Of course, we are not going into very detail here, but I give you the basics. So the idea is that in order to build a computer, you need to do something with a hardware. And hardware-wise, a hard drive, but also the memory of a computer, can only distinguish between two states of the world. Either energy is flowing or energy is not flowing. So either something is turned on or something is turned off. So there are always two states. And computer scientists introduced the idea that zeros and ones basically mean yes or no or true or false, or two states in a world. Now computer science is basically all about how can we give meaning to the zeros and ones that are somewhere in a computer's memory modeled with electricity and energy. So let's go and take this a step further. So when your computer starts, then it is the job of your operating system to manage the memory. And let's say you open a program on your machine. Let's say you open a web browser. What your computer will do is it will go ahead and it will allocate a certain area of its memory, let's say to a web browser. And whenever you do something in your web browser, then the data will be stored inside here. This is the entire area of the program that is running the web browser. And as far as the program that is running the web browser is concerned, it doesn't even know the outside world exists. It only knows its inside world here. And similarly, if in JupyterLab we open a new notebook file, what happens in the background of your computer is a new Python process is being started. So let's say here, Python process, let's say, let's say Python process one. And then as the Python process is running, all it knows is this part of the memory. It cannot look outside. And if we open a second notebook in JupyterLab, what that means is in our operating system, somewhere else, will that be a second area with the Python process number two? And inside there, we see the little square brackets. And there are, of course, also only ones and zeros here, nothing but ones and zeros. Okay. So at the end of the day, your computer's memory is nothing but a big area with only zeros and ones. And the operating system chunks that up and assigns individual parts of the memory to different programs. And then the programs can manipulate the zeros and ones inside them. Okay. This is essentially what software does. Okay. Now another concept is each of these cells has an address. So think of it like this. The upper left-hand corner starts with the address number one for here. The second cell would be the number two. The next cell would be the number three and so on. And when we reach the end of the first line here, then we will simply go to the second line and move on. Okay. So in other words, if a piece of code asks the operating system, hey, can you tell me what is the state in the cell number one million? Then the operating system will go to the one million cell and will read if there is a zero or one. And then it will report the zero or one basically to the program. And then the program can do something with it. Okay. This is basically how that works. And now what we are going to do is we don't want to study computer science here. We don't want to understand how a computer works or how operating system works. We are only concerned with how Python works. So we are now going to look inside one of these Python processes. Okay. So that is what is going to come up next. And for that, I have prepared a couple of templates that look like this. So on the left hand side, inside the Python process, this is the part where we will put all the names of variables that exist and other names. And on the right hand side, we will put so called objects. Okay. So that is where the name object orientation comes from. So let's go back to our example. To our example here that we just executed. And let's do each of these three lines one by one by hand. And for each of these lines, I will show you in the picture what is going to happen in memory. So Python goes ahead and executes this code cell from top to bottom. So it will execute the first line first. And here, because it's an assignment statement, we are going to execute the right hand side first. Okay. So that's the only thing that's going on. So in the picture, what that does is, as Python reads the number 42, what it does, it goes ahead and it creates a back here. And this is a back that of course holds nothing but 0's and 1's. But the 0's and 1's somehow have the meaning of the number 42. And then Python goes ahead and it will label the box. It will put like a tag to the box and it will put in there int. Why? Because this is an integer box. Okay. So the data that is inside this box is of type integer. But really this is just 0's and 1's. Okay. I just draw whatever that means to us in the example. That is the right hand side of the example. And then once the right hand side is done, what Python does it, it executes the left hand side. Okay. And what this does is, it means Python goes on the left hand side here in the name section and checks if there is a variable called a. And it does not find one. So therefore it creates a new name called a. And then it makes a reference the object with the 42 in it. Okay. That is the first line of code. The error, in my concept here, it basically can go anywhere to the object. But really it goes to the beginning of the object. So wherever the beginning is, this is checked many 0's and 1's that live next to each other in memory. And the name a points at the beginning of this box. And then of course there is a certain end to it. Okay. Let's continue in the code. So then the second line will be executed and the right hand side goes first. And that means if another box will be created and this box will be bigger. And the reason why is because this box has to model more information. So this would be for us as humans, this box would have the meaning of the number 42.0. And also this box is going to receive a tag. And let's call it called it float. Because the data type is called a floating point number. That is basically a default way in Python to model numbers with decimals. Okay. Another minor detail that in future videos I will probably skip. But now I will once and for all basically try it. So using another color, now red, a reference to the beginning of this object will be given back to the left hand side. This is what I do with the dotted line here. And now in my code example, the left hand side of the second line will be executed. That means let's create the variable named B. Okay. So Python does that. It goes on the left hand side. It checks if there is a name called B. It does not find one. So it creates one. And because the reference was given back to it, it takes the reference going to the object and stores it as a reference going here. And then of course, the red arrow dot here, we can simply remove it. So just conceptually, I drew it here just to indicate that once the object here is created, the reference to this object will be given back. And then it depends on us what we are going to do with the reference. And in the example here, we are taking the reference and we are basically storing the reference as a name on the left hand side. Referencing some object on the right hand side. So let's finish that example. The third line will be evaluated from right to left as well. So now we are going ahead and we will create an even larger box because now we are looking at textual data. And we will write in there Python rocks. And this will also receive a tag. And let's write this here str, the shortcut for string. This is the main data type to model textual data. And then the left hand side will be executed. And this will simply create a variable called c that is going to reference this box. Okay. So this is what has happened inside the Python process so far. And now let's go ahead and write down some names, some technical terms. So each of these bags that contains ones and zeros, we call that objects. So that is why the right hand side in the memory diagram is called an object or objects. On the left hand side, the names here, these are the names that are called names. That is why we have names up here. And then if we only look at the error here maybe, this is what we refer to as or what we mean as a reference. So reference is just an address in memory that we can jump to. So that is whenever in my diagrams, I draw arrows. That is what typically is a reference. And then the combination of a name and the reference is what we mean as a variable. Okay. So the concept of the name and the concept of the offer variable is slightly different. A name is just a name on the left hand side. And together with a reference, a name becomes available. Okay. There will be references without names. Okay. So there will be further diagrams that will look a little bit different. But these are the technical terms that are important to observe here. And now let's go back into our code example. And now let's quickly talk about a couple of properties that every object has. Okay. Every object, no matter what we are modeling. So the first one is, so let's maybe write it here. Every object has three properties always. The first property is the identity. Okay. And the identity is basically, so maybe let's give it a second, an alternative title, the identity is basically the memory location. Okay. How could we figure out that? Well, there is a built-in function called ID, which gives us the ID, the memory ID of an object. So if I ask Python, what is the ID of A, it gives me some very long number. This number has no meaning whatsoever. This is simply the address where the object that A is referring to starts. Okay. So let's assume the upper left-hand corner of this object here is the beginning of the object. So this would be the cell with this number that we see here in the Troubutanoid book. But other than that, this number has no meaning. And then of course, we could also ask the question, what is the ID of B and what is the ID of C? These numbers again have no real meaning. However, if two different variables refer to the same object, then what we have is, yeah, they would have the same address. So what we could do here is we can do the following. Since both A and B are the number 42, we can of course compare them to each other. So let's maybe compare A to B. The value of A will be compared to the value of B. And the answer will be true. And that makes sense because the integer 42 and the decimal number 42.0 for us as humans is basically the same value. So they should compare equal. However, there is a second operator that is the so-called identity operator. So we could also go ahead and ask the question, is A the same as B? And then the answer is false. So is it an example of an operator again? And we call it the identity operator. So what do we learn from this? Not every operator in Python is a symbol. The word is is also an operator. This may look a little bit weird in the beginning, but this is just another operator, just like the double equal symbol, which is the comparison operator. So identity and equality are two different concepts. That is what we should learn from here. Maybe let's summarize that here. Equality and identity are two different concepts. So that is the first property that every object has. It has an identity. The second property every object has is what we refer to as the so-called data type or simply type for short. And there's also an alternative title for that. We could refer to that as the behavior of an object. So how can we figure out the type of an object? There's another built-in function called type. And if you call the type function and path to it A, we get back int. If we pass the variable B to the type function, we get back float. And if we pass C to the type function, we get back str for string. And this is why in the text that you see in the upper left corner of the three objects, I wrote int, float, and string here. And what does a different data type mean? A different data type means that the sizes of the object is different. So there is a different number of zeros and ones inside the object. But also what we can do with an object is different. Or we can say that an object may behave differently. So I will give you a short example for that to illustrate a point. So if I refer to B and I get back the number 42.0, the object B or the object to which the name B refers to comes with very specific functionalities that we can execute. So for example, there is a function which is called its integer that exists on every float object. So how do we access this function? We use the so-called dot notation. So I say B dot is integer. And because its integer is a function, I call it with the call operator. And this will give me back as an answer true. Why? Well, mainly because the number B has only zeros as its decimals. That is why we get back a true here. So let's also summarize this. The dot here is what is formally known as the so-called attribute access operator. Sometimes also simply referred to as the dot operator. And this allows us to access an attribute on the B object, on the float object that the name B refers to. And then we'll say any function that lives, let's say, on an object is called a method. So a method is nothing but a function, just an ordinary function that only works for objects of a given type, of a certain type. So let's do a second example. Let's introduce a variable called double B. And let's assign to it the number 42.87. And let's go ahead and ask the question, is double B an integer? And the answer is, of course, source because the decimals are not only zeros. And now comes a tricky question. So what if I go ahead and I look at the object to which the variable a refers to and I ask this object if it is an integer? Well, we could guess the answer. We could guess that the answer is true. However, if I go ahead and I say is integer and I call this method on the a object, I get an attribute error. And the reason why is because for integer object it does not make any sense to ask if they are an integer because we know that to begin with. So we know that because for every object we know it's data type and for the inter-object we know it's an integer. So asking the question that whatever is inside the object is an integer does not even make sense because we know the answer to begin with because it has to be an integer. Otherwise it couldn't be in an integer box. So that is what we mean by saying that different data types imply different behavior for objects. I also give you another example. So the C object is the text which represents the message Python rocks and this also has a couple of specific functionality. For example, it has a method called lower. So we say C dot lower and we execute that and what that does is it gives us back a new text object in all lower case and this does not make sense for numbers. So this operation or this method only makes sense given that we are working with textual data. Similarly, we could also say C dot upper and then we get back text in all upper case. So this is what we mean by behavior. This is the second big property every object has, the data type. The third and last one is what we are going to refer as simply the value and oftentimes I actually refer to use the name the mantik value and this is basically whatever the contents of an object mean for us as humans. So that's right here alternatively just the meaning. And the meaning is quite simple. For the objects A, B and C is just what is inside the object. So the meaning of the first object is the number 42. The meaning of the second object here is also number 42. It's just modeled in a different way or it's stored with more ones and zeros but it still means the same number to us as humans. And the text Python rocks for us humans this has just a meaning but for a computer these characters have no meaning whatsoever. So let's summarize. Everything in Python is an object. Objects may have names that reference them. Every object will always have a data type. Basically implies what we can do with an object and then whatever is inside the object the concrete zeros and ones that are inside the object that is what we mean with an object's value. So this is the basics of the object oriented paradigm and that's just the paradigm by which Python models the memory and other programming languages. So let's say maybe you do have some experience with some other programming language. Other programming languages may work differently in the memory and it is important that you don't confuse this with some other concepts. So for this course just stick to the memory diagrams how I draw them in this course. See you in the next video.