 Hello everybody. So we're going to continue our discussion of Python under the hood today, and we're going to focus on data itself variables and types and Last time we introduced a new way of thinking about Python programs, which was situating them in the context of the greater computer itself Now we said every computer has basically followed this architecture For the since the 40s and 50s Which is that you have your input and your output devices and then in the middle you have a brain Which is the central processing unit that does all the computations and it you have memory where the data resides that these computations are done upon right so the Python itself you need to think of it as a machine a program that's sitting there running and It sends instructions to the CPU to do things like add and subtract and do Boolean comparison and assign variables And then you have memory which is where those values the data values live over extended periods of time and You know the fact that Python is like a Program and is running inside this computer is really what Alan Turing is famous for he came up with a computer and A definition of what it means to be a computer and what made his definition so special is it's a computer that can run Any sort of program right as long as that program is in a specific format and has certain properties And that's what's so magical about computers themselves. It makes them better than calculators They can run any sort of program as long as that program fits certain properties Okay, but anyway, we also introduced a new way of looking at Python programs in terms of what variables are And how they relate to memory And what we said was when you create have a variable assignment like this Remember variable assignments and assignment statements in Python evaluate right to left So we do the right-hand side of the equal sign first So Python creates a value called 42 Somewhere in memory in the physical memory. It turns on those ones and zeros that encode the number 42 as an integer that Python understands and Then it assigns the variable the answer to that and what the answer really is Is it's a named bookmark and that bookmark points to the address in memory where this value lives Okay, and then we also did a tracing exercise where we're trying to make sure that we really kind understand when Variable values are created and destroyed So today we're going to explore Variables and data and memory a little bit more and we're going to switch over to pie charm to do that Okay, so we're I'm going to walk you through these steps So let's switch over to pie charm now and get started So I already have pie charm open here. I mean make it a little bit bigger so you all can see it, okay? And the first thing I'm going to do actually before I even go to pie charm. I'm going to go to my desktop and Here I have my handy-dandy CSE 231 folder which everyone else should have whoops and Inside of it. I will make a new folder and I'll call this folder lab zero one You can call it whatever you want Of course Windows is going to go slow on me for some reason probably, you know, it knows I'm recording it knows I'm on the spot. Okay, so I've got a new folder here. I'll call it lab zero, okay? You can call yours lab whatever you want Okay, let's go back to pie charm now and I am going to open up that folder. I just created Okay, so mine is in my desktop CSE 231 and You may have noticed when I first got here lab zero one wasn't showing if you know that you made the folder And you know you're in the right spot click this little refresh button up at the top It'll make sure that it appears. Okay, so I'm going to open up the lab zero one folder Pie charm asks me do I want to do it in this window or a new window? Either one is fine. I'm going to do this window All right, so I got my blank pie charm project here nothing in it Let me go ahead and create a new Python file. So I right-click on the folder new Python file and Let's just call it lab zero one dot pie Okay, so here we are. I'm in a blank Python folder Let's do some fun stuff here, right? So if I go back to my Slides you can grab this code At least this code here on the left hand side and paste it in there if you want, okay, or you can type it So I'm actually going to go to some To my notes here, I'm going to grab This code from slide number five. I'm just going to paste it into pie charm Let me get back to my pie charm screen Okay, so I grabbed the code from slide five and pasted it into pie charm and you can do that too Now our focus here is going to be Examining this type function Type is a built-in function of Python and it returns a value specifically it returns a string and all I'm doing is I'm printing the type of these various pieces of data these are values that are going to be created in memory and I Asked Python. What is the type of each of these values and it prints them out? So you've probably seen this function before and you've probably seen the outputs of it before 42 is class integer 3.14159 that's a float True is a bool. It's a Boolean and Alice here is a string. Okay, so when we print out the type it says class in Int okay integer float boolean string, but there's also this class thing Okay, we'll get back again to what that means in just a second Okay, so now next slide Let's look at another function, which is the id function. So I'm going to manipulate this a little bit Okay, so I just copied and pasted my code and instead of type. I'm going to use the id function here Okay, it's also a find and replace feature. I could have used Alright, so let's run this code Alright, so ID ID is another built-in function. And what do I get down here? Well, I get some numbers when I print these things out Now what are these numbers? These numbers if you're running basic Python that you downloaded from Python.org These numbers are the address in memory of these values that were created Okay, so remember whenever you run Whenever you create data it gets put into memory Well, this id function gives you the address in memory of data that was created Pretty cool. Huh? Let's run it again Okay, and I want you to notice something notice something on my screen and then also notice something on your screen as you run it Note the value here of Alice Okay, I'll mine your numbers are going to be different and that's to be expected. Okay, that's fine But I want you to note your value for Alice write it down somewhere Okay, mine is actually I'll put mine in a comment right here Okay, let's run this code again Now if you remember you can right-click anywhere in your script and click run I like to use the little keyboard shortcut for Windows as control shift F10 Hmm, something's different, right? Now this ID The location in memory is 44887520 Okay, that's different than it was before That's because every invocation of Python that you do every time you start up the Python interpreter and run the script And then it shuts down It is grabbing new places in memory new locations in memory These values are volatile. They are not consistent But maybe let's just take a look at this guy. What's the ID of 42? Okay, for me, it's this for you. It'll be something different. Whatever it is Copy it Note it Let's rerun Hmm This value stays the same. Well, that's a little weird Okay, well, that's because there's an optimization that Python does Python knows that the numbers I believe it's negative 15 through 250 something like that are all Reused those numbers appear a lot so as soon as Python runs like as soon as Python dot exe starts before it even gets into your script It puts these values these special values in a place in memory and just reuses them over and over and over and over Instead of creating and destroying them over and over as well same thing for true true will be in the same location Okay, so that's kind of neat what I want you to take away though We've got the type function that will tell you what kind of data you're dealing with and you've got the ID function Which will tell you where in memory it resides now usually however We don't think of memory addresses as integers usually they are referred to using Hexadecimal right, so let's hex is another function that is built into Python So what I'm gonna do is wrap this value in a hex Ooh, okay, so what are we doing here? We've got the ID function. These are nested function calls okay, and Recall so the parentheses operator in this context is a function call parentheses operators, you know, they're grouping you do the inside first and then work your way out just like you do in Algebra So it evaluates this Which gives you an integer, okay? So it's going to give you an integer like this and then it's going to call hex on this integer and that's going to give you something else it's going to give us a whole bunch of stuff that looks like this and Then we print it Okay, so when we don't have to store the return value Right, we could do something like The ID gets ID 42 or 43 and then we could do Hex value gets hex The ID and then we can finally print hex value We could do that But we don't need to and actually it's kind of wasteful to do that because Generally, this is a general rule of thumb something to remember You only need a variable to put a value in a variable if you reference it from more than one place Okay, so I'm referencing the ID only in one place. It's kind of wasteful. I don't need it I can just replace the value of the ID With it's computed value Same thing for hex value. I don't need this variable Right. I only need the value that comes back from this function All right, so it's a little optimization to keep in mind, right? So that's what I'm doing here a nested function call Let me wrap the rest of these in the hex function Because again, this is the way that Memory addresses are conventionally referred to they are conventionally Reference using hexadecimal format and you should have learned what hexadecimal format is. It's base 16, right? So here's what mine look like, right? This is just my integer values converted into this hexadecimal format Now I believe I must be running 32-bit Python if you see a value that is much bigger than this If you have a much longer string, that's okay. That means you're running 64-bit Python Don't worry about it. The difference isn't terribly important right now, okay? But this is how we normally view memory addresses All right, so cool cool We're just reinforcing the idea here that let me get this out of the way and let me Get back to my presentation screen We're just reinforcing the idea here that Variables values exist in memory and you can get the memory address if you want to okay, so Lot of code. Let's take a look at a different actual visualizer of this thing So what I want you to do is open up a web browser and go to this site here python tutor dot com okay, and Let me get my web browser up Bring it over here How we looking on the screen not too bad all right? So this is python tutor dot com it was written by Philip Guo when he was at Stanford. It's very smart guy Very cool thing. I Encour this is a really great tool and especially if you're a visual thinker I encourage you to use this website Especially if you're stuck on something and values aren't coming out the way you want and you just need a break from looking at code And it would be helpful to look at something visual check this place out so python tutor dot com I'm going to click on start visualizing your code now and That'll take me to this screen now before you do anything I want you to go to the live programming mode. So click this button down here live programming mode Right, you're gonna see something that looks similar, but it'll say here at the top. This mode is experimental All right. Now what I'm gonna do is I'm gonna put some information in here At sorry before I do that Look down scroll down a little bit Down here. There's a couple of drop-down boxes Change this one in the middle that says render all objects on the heap make sure yours says that Make sure it says render all objects on the heap. The default is something else Okay, render all objects on the heap Okay This one. All right. So now we're in some code I am going to if you switch over to slide number nine There's some code in there and I'm gonna grab it and paste it in here Okay All right, so this is code that you can find it on slide nine you can type it in go grab it paste it into this editor So what is my code doing? I am printing a string I'm saying print the address of value 42 at hex ID 42, right? So 42 the data value 42 lives at some integer memory address convert that to hex and now print it okay Now I Assign, let's let's run this in real time. Okay down at the bottom You have some controls here. Click where it says first Okay, you see a little arrow here. It's kind of like our tracing exercise It's getting ready to execute this first line of code and you can think of this as the instruction memory It's getting ready to execute it and after it executes. It's going to show you the value of all variables in memory Okay, so let's go next So What do we see? We've got the printed output, but you'll notice there's nothing in memory Right. No variables were created The implication is that this 42 was kind of created It was used and Python forgot about it. Right. It's gone. Nobody. I didn't assign a variable value to it, right? All right, so we've just executed this line line one. That's green. We're getting ready to execute line two So let's do that click next down here All right, so what has happened? I got this thing on the left called the frame Okay, the frame think of the frame as an address book The global frame is the address book for the variables that are in the main part of your program I've created a variable X and it is pointing To the value 42 in memory Okay All right, cool No output has been produced yet But this is what happened draw this link in your brain when you do a variable assignment X gets 42 we're connecting The variable X to the place in memory where the thing lives How do we know that check out this next line of code line three address I'm going to print out the address referred to by X print ID X and convert it to hexadecimal. So I'm getting the ID of the variable X. What do you expect to see? Well, X is pointing to the location the address of the value 42 in memory Right and what is the value of X? Okay, so when you do this Print the value of X and you just give a variable value, right? Here I'm printing a variable if you do addition X plus Y division X 5 by Y if you put a variable in a string What you are really doing? This is so important for you to understand What you are really doing is taking this variable Going to its address in memory and Seeing what the value there in memory is Okay, the value of X is 42 Because that's the digits. That's the bits. That's what's in memory at this address Okay, so so so important that you understand this Right X right here says go to the address in memory pointed to by X and Get the value that's there. Okay X itself is not a value It holds a memory address Right, let's do another example so go to slide 10 in the slides and You'll see this code, right here I'm not going to step through it's the exact same code except I'm dealing with a float 3.14159 right Actually if I go back and I run it I should get a slightly different address to Can walk forward and backward kind of cool Okay, so again, I've created a variable called I've created a value 3.14159 I assign pi to 3.14159 so Python once it creates a data value It's kind of smart enough to reuse it if it's already in memory if it knows it's in memory somewhere Hex ID of pi pi is pointing to this thing, but you'll note as well Python knows this thing as a float That's very important Python knows that the thing living here is a float if you go back to the old code with the integers Can I just undo here? I think nope can't undo and pi tutor at least only limited amounts if you go back and you put the code from a couple minutes ago This will say int right when we were dealing with 42 All right now Really important really important maybe the most important thing of today, okay? Go grab the code from slide number 11 and paste it in here Okay Go ahead and pause while you're doing that and once you've done it once you've done it go back to the beginning Restart click the first button. Okay? All right, so you should have the code from slide number 11 and you should have started over again Okay So let me step over here. We're gonna walk through this line by line Just printing out the address of the value 42. Okay, no problem. I Assign the variable x to point to that address All right, cool So I've got the value 42 and I've got the variable x pointing to it Now this statement line number seven What happens? What do you think happens? I've got y gets x What's gonna happen? What's gonna happen over here? in this block Well, what's gonna happen is I create a new variable in my global frame my address book and I'd say point to The same place x points to why I'm gonna create y and point to This x look up right here Variable value by itself returns a memory address okay X point why I want you to point to x Okay, so now x and y are pointing to the same address in memory there. There is only 142 in memory Okay, and you can see that go ahead and run these prints if we print The address of x and we print the address of y they are the same one data value in memory Two variables pointing to the same location Okay, very interesting very important a lot of ramifications to that Okay So and we'll see some of those ramifications at a later date, but very important What's gonna happen now if I do x gets three point one four one five nine What do you think's gonna happen? What's gonna happen over here? What happens to x in what happens to y? Let's see boom We have a new float that was created in memory and x now points to it but Why still points to 42 okay? Look at their memory addresses. They're different x is pointing somewhere new y points to the old place Okay, super important. Okay, very interesting Now you may have heard the terms mutable and immutable data types Integers floats strings. These are all immutable Immutable is a 10 cent word that means you can't change it Can't change it Mutable data types are things like lists and dictionaries Mutable meaning you can change the value. We're talking about values not data, right? so When we did x gets three point one four one five nine x was pointing to 42 But 42 is an immutable data type. You cannot change it You cannot change the value at that address location. So python says oh This guy he is immutable. I can't just replace him here Integers are immutable. I can't replace this value what I have to do is python talking is create a new value and Then point x to it. That's what happens If we did we're working with lists here. This would be a different ballgame Okay lists are immutable, but this is the essence of immutability in python You cannot change the value. You can reassign variables all you want. That's fine But you cannot change values. Okay values live in memory Variables point to values in memory. You have to now make that distinction Okay, you have to draw that line before variables and values Wishy washy same, but they're not the same draw that distinction. Okay Big concept let it digest in your brain All right, I'm gonna switch back to My slides here for a minute if I can There we go All right, so recap here data are Bits that represent something like the number 42 or 3.14159 These are data or values. Okay. Think of values data are the same thing data and values When you create data in python it puts these bits in Computer memory somewhere. Where in memory? Where did these things go? That's an interesting question Python does it for you. You don't have to worry about it But the way it works is remember our notional machine We had this thing called heap memory Python's got this big pile of memory just sitting on a plate next to it and whenever it needs to create a value It says hey, give me a bit it grabs something off the plate and Scribbles in it says 42. You're here But that little something it grabbed that little box has an address and that's how it knows right so the heap Python says I need a new value and it says heap I need some place to put it and the heap says here's a place in memory You can put it and that's the address the variables then is Python saying all right x. I'm going to name this address x so I can look at it later, okay Super important that you have that View of variables data values and memory moving forward Okay, I want to look at one more thing kind of one more trick in Python and That is sys dot get size of so I'm going to go over here to Pie charm again. I'm going to be back in pie charm I'm going to go ahead and delete the stuff I have before and I'm going to use first of all I'm going to add this statement called import sys to the top So you may have seen this for maybe not Probably have Import and then this is what is known in Python as a module Modules are related groups of functions that you might find useful. You probably use the math module before What I can do is I've created a variable and a value here I've created a value more precisely and assigned a variable to point to it and sys dot Get size of this is a function. I Probably need to do something like print that Okay, and what this gives me back is the number of bytes That are required to represent This number three point one four one five six. Okay, I'm not sure why that's six But anyway, three point one four one five six I Got to change it. It's driving me. It's going to drive me bonkers, right? There we go. Thank goodness So this is the number of bytes remember eight bits in a bite that it takes to represent this number in memory That's a lot right takes 16 bytes 16 times 8 right bits to represent this number Let's try let's do something else. Let's do 42 right 42 takes 14 bytes Let's do a Million a million takes 16 bytes. Let's do. Oh can't do that Let's do this that gonna work. Oh, I still got an L in there. Don't worry. Just mash a bunch of numbers 26 bytes so the bigger my integers get The more bytes it takes to Represent them right just typing in some integers here and you can kind of see this growing You may see different numbers and that's okay. That's okay How about a string all right, let's do print sys dot get size of the string Alice Alice takes 30 bytes to represent Wow, right Alice five characters this Number I don't even know how to describe it only took 26 bytes Alice took 30 Whoa, well computers are really good at representing numbers the way that it Python stores Strings very complicated right very complicated Let's make the string bigger Alice in Wonderland 44 bytes, okay decent amount So I want to show you one more kind of interesting thing Let's try sys dot get size of let's do a different float. Let's do 34.4 They're both 16 All right, let's do another one. I just duplicated this line So if you hit control D or command D, it'll duplicate the line your cursor zone could be very useful It's type of much bigger float like this Still 16 interesting Let's do a really small float Yours might be 32. That's okay All these are the same right Turns out the way that the data that Python represents floating points is consistent and it doesn't matter You know floating point their name derives from the fact of Where this decimal point is represented inside the computer? Okay, just a fun fact You would learn if you're taking CSE 242 computer organization. You'll learn how floats are represented inside a machine It's kind of interesting, but you know just realize that yeah floats are fixed size in data land But integers and strings in Python they can grow All right, so Let's switch to Where I'll leave off with all of this is that just be aware that the different data types You know 42 whether it's an integer whether it's a Boolean whether it's a string. Don't worry about typing this Just be aware that these things occupy different amounts of memory. That's the important thing to take away here All right, so one last thing one last but holy cow a very important thing I'm gonna go back to my Python tutor in live mode and Let's grab a string. Okay, so if you go to slide number 15 slide number 15 grab that code and paste it in here Okay, so I've got some code here that is going to it just creates a string That looks like this. I've got Alice with some white space around her She's got some blanks tab characters some new lines and you can see when I print it It's got some gunk in there. Let me expand this a little bit okay But nonetheless, you know, I can get the memory address of that this variable points to I can convert it to hex I See here I'm printing out the type and I'm seeing here that the type of name The type of data value that name variable points at is a class string Okay, well, let's manipulate this string a little bit. Okay, let's let's do the following Let's do x gets name dot strip, right? We talked about Don't like that and let's print x Okay, I don't like that it keeps resizing on me, but that's okay Remember in ours go if you don't recognize the strip method or strip function go back to the last week's module and check out the the string functions, but what name dot strip does is it removes leading and Trailing white space But strings are immutable remember it was one of our immutable data types. So The string here name or Alice is not getting changed itself instead Name dot strip returns a copy of Alice with all this white space removed okay Same if we were to do like Why gets name dot? Let's do why gets name dot upper and then print why So here why gets x dot upper we took x which points to this string and told it to go uppercase that's what upper does and That result is assigned to why and we print it Okay, so I've got three different copies of kind of my original string But they've been modified in a different way because string is immutable but All right, let's let's Take a deep breath because this part is really really really important. Okay Let's take a deep breath shake it off get up walk around for a minute grab a glass of water take a take five Okay, because this is gonna be really important. What's coming up next? Hey my deep breath Name dot strip Okay, what is that? Specifically what is this weird little dot that we have seen you've probably seen this You know, you see it when you deal with lists you've seen it when you've dealt with strings What in the heck is this weird little dot? Right, by the way, you don't have to use this dot on just You don't have to use this dot on a variable you can use it on a Literal on the value itself you can do Alice Dot lower Right take the string Alice and dot lower it give me the result of this computation You get little Alice back, right? What in the heck is this dot thing, right? What is it? Okay So what it is is that This thing that is put into memory, right those bytes that we just looked at You know the 14 bytes for the integer and so on It's more than just the data and the value There are also Operations that you can perform on that data right, okay Recall when you type type Right, you type a variable or type of value you get something that says class and Then string or inter float class Okay, what does this mean? What this is really telling you is that Alice this string here is more than just a value All programming languages have a concept of data types Data types are consisting of two things the data. Okay, so the characters Alice that's the data and Operations the things you can do to or with that data Okay so This thing here, right it says class string What does this mean? Classes are a Python language feature that provide the means of bundling together data Value and the operations the things you can do that value together Defining a class creates a new data type in Python. Okay Data types are data and operations classes Are how Python creates data types classes actually a keyword? We'll see it next time So the Python stir class this guy Holds the data that make up the letters and several operations Which are technically called methods for manipulating the data a Method is the same thing as a Python function Except that it is defined on a particular class okay, if you go to the string the definition of a Python string right there are many many many string class methods What that little dot is is The way of accessing the operations okay, so before we've been thinking of variables We've been thinking of classes. We've been thinking of values We've been thinking of these things that we create a memory as the values But they are more than that they are more they are also operations and That is what those little dot blah blah blah things are sis dot get size of string dot split list dot append those are Operations and in Python those are called methods. Okay methods are a lot like functions But they belong to something they belong to a particular data type Wow, okay, that's kind of crazy right so when you create data What you are making are multiple instances of a class With different data, but the same functions Or same methods is what it should really say same operations. Okay, so Suppose you've got x gets Alice y gets Bob z gets Chuck Okay, you will see these are all instances of a string string String string class string class string class different instances But they have different values. Okay, however You can do dot upper On x you can do dot upper on y you can do dot upper on z and it will all do the same thing to the data Okay, so the in your classes in your data types for Python Each time you create a value you are creating an instance of a data type and Those instances have different values, but you can do the same things to Different instances of the same type So all of these guys know what upper means they all know what lower means they all know what strip means they all know What split means? cool Different classes can do different things Right, so if you have an integer class I've got two integers 42 and negative one if you do 42 minus 1 No problem the integer data type knows specific now remember we're Data typing classes are kind of interchangeable terms. They are at least in Python So if I say data type you think class if I think class you think data type They're the same thing just turns out that data type is a more General concept class is the Python specific word for it. Okay Different classes can do different things so integers for example know how to subtract from one another but strings Do not know how to subtract from one another and if you do this go type it in pie charm You will see something that says type error type as in data type type error unsupported operand types for The different sign the subtraction string and string. This is Python's very non-intuitive way of saying Strings don't know how to subtract from one another. Sorry. You can't do it Okay, because I went into the data type for string and I said hey subtract yourself from this other guy and Python said Nope, don't know how to do that. Okay So data types again Different data types have different operations they can do All right, so instances of classes go back to our visualization here instances of classes are Generically referred to as objects. Okay objects in memory Everything in Python is an object every variable you have ever created every Value you have ever computed is stored in memory as an object. Okay, so say it with me everything Everything in Python is an object The number 5 and number 42 the float 3.1416 the Boolean true the string Alice They are all objects in memory These objects are instances of different classes. They are instances of different data types But collectively when they're living in memory, we say they are objects in memory Heady stuff heady stuff. Okay So let me Wrap up on this slide This is probably the most important lecture of the whole semester So if you have to watch it five times until you get it, please do ask me questions. Okay Data types appear in every programming language They are comprised of the data they store and the operations that can be performed on the data Okay, this is a step up in our understanding of what Python does and how it works Creating a class in Python defines a new data type class string class bool class int and Python has many built-in classes and you've been using them already, but maybe not fully appreciating What's going on with them? Okay when you declare or use a variable Python creates an object in computer memory That is an instance of a class This class instance holds particular values it holds particular data, but All the different instances of the same class know the same operations upper split Strip all strings can do it The location of the object in memory is given by its memory address okay Lot of big time concepts study study study know the difference Everything that's underlined on the screen. It's vocabulary know what they are okay Use the Python tutor on maybe some of your old programs to really Concretize what these different things mean, okay? It's important that you have a good visual model of them So so far in your career You have used pythons built-in classes things that Python ships with int string float list program Next week next time we are going to learn how to make our own data types using our Using pythons class feature Okay Talk to you next time again study all this stuff and hit me up with the questions you have good luck