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