Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Visualizing variables
Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this next short section, couple sections actually we're going to visualize some of the code that we're working with.
0:08 Now you've seen it written, you've seen it executed. And we've been able to ask questions like hey, what data type does it have?
0:15 But I want to make sure you have a really clear understanding of what these values and what these variables mean.
0:20 It turns out, getting a good grip on these things is actually one of the key steps and one of the cornerstones of becoming good at programming.
0:28 Though there's this cool bite here called Python Tutor over at Pythontutor.com. Now, it's not entirely clear how you get started.
0:36 But what we want to do is we want to visualize our code or get live help. We don't actually care about live help.
0:41 We're just going to write some Python code. We could write other languages but we're just going to write Python code.
0:48 Now we're just going to work with one data type and all we want to see is how different variables and values interact.
0:54 So when we say something like person1 = 'Sarah' and let's have another person person2 = 'Michael' The things on the left, person1 and person2
1:07 these are just, think of them as names. And those names can refer to data that exists out in memory values that exists with a type
1:16 though the string Sarah and the string Michael or later maybe we write down the age it could be the integer of 42
1:23 or whatever it was we put in that section. We can also take the values of variables one variable like person2 and assign it to something else.
1:32 So what if we want to express somehow that Sarah's friend is Michael we could rewrite Michael but it makes much more sense
1:40 to take this variable and assign it because if this gets changed somewhere along the way you know, you want to be able to work with this
1:46 much more dynamically, not just be super, super explicit. So let's go ahead and just visualize what this looks like
1:53 so far, then we'll make a few more changes. Now in order to do that we're going to click visualize execution
1:59 but to get the true picture we're going to pull down and say render all objects on the heap because hey, that's how Python actually works.
2:07 So let's click on visualize execution. Now, look over here, it doesn't just take all the code and run it, what it's going to do is it says
2:14 we're going to run one line at a time and then hit Next, Next, Next, if you want to run them all
2:19 you can hit last, you could even move this little slider to zoom around through your code, okay? What we're going to do is we're going to run this
2:26 and we're going to take this variable you know, basically create a name and then we're going to create Sarah which is an object out in memory.
2:35 So here you'll see frames and objects. Frames are basically the things that hold the variable definitions. And then objects are the actual data
2:43 with types out in memory. Though let's hit next. There's different kinds of frames don't worry about what this is just means it's not in a function
2:50 or a class or something like that. Now, we have our variable person1 and currently this variable refers to Sarah which is a string out in memory.
3:01 We're going to do the same for person2 hit Next, and we have another variable or another piece of data in memory called Michael
3:06 and another variable called person2. Now here's where it gets interesting. When we say, the friend of Sarah is person2
3:16 instead of creating a copy of Michael, remember when we assign the value it says Well, what is person2 point at
3:22 it points at this piece of data in memory right there. Well, guess what, when you do this line four that just means a friend also points to, refers to
3:31 has that thing as its data, as its object also called Michael. Let's see a few other things we can do here. Well, we could come up with a list
3:42 we get a list of all people. And don't worry about how we work with list yet we'll talk in depth about this.
3:48 But this list lets you hold on too many things at once. And we could go to these people and we could say I want to put those two people, person1 in
3:58 and I want to put person2 in. Let's run that. Oh, down there they are, now we're going to create this object
4:05 this list, you can see an empty list out in memory that's what people points at. And then when we assign it just like when we did this friend
4:13 we're going to say, well, one of the things in this list is that piece of data called Sarah. So notice now the list also points at Sarah
4:22 and then it also points at Michael. Pretty cool, right? So we have our variables here. And then this particular variable is a list
4:30 a complex type, that itself knows about other pieces of data, and basically has pointers or variables in here that talk about
4:38 hey, Sarah is my first piece of data and Michael is the second. Let's do one final thing here. Let's add a third person. Mike or something like that.
4:52 Here, we can run this up to almost the same spot. Now we have the same setup we've had those two pieces of data
4:59 the two variables that directly pointed at them the friend who we said also now is a point there and then the list where we put them both in
5:06 and also knows about them. So now we just do one more step and say there's some other piece of data that's not related to this over here.
5:13 I hope this gives you a decent sense of what variables are and how they're different from the data or the values that they actually have, right.
5:21 So the variables are just names that at some moment and point out here, right, and that we saw before here, like the people pointed at the list
5:31 but the list didn't happen to point at Sarah or Michael just yet. Though here's a quick visualization
5:37 of variables and their values as objects in Python.