Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Visualizing mutable data types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now we have this rough sense of what variables and objects look like. There's one final connection to make. We saw that we could create things
0:10 like person one and set their name to Sara they're valued to Sara we could create person2 set their value to Michael then we change that
0:17 well then it just changes what piece of data that variable, that name points to. But not everything in Python is like that. Many things are.
0:26 Many primitive basic types like numbers and strings and so on, they're like that. But some of them are not.
0:33 Let's create a little example here and explore that. If we're going to create a list again a list is just a collection of pieces of data
0:41 that are in order don't worry to much about that we're going to talk a lot about list later just a simplest example we have to work with.
0:49 So we're going to come up with some numbers like these. Let's go with other data it's currently going to be equal to VA data.
0:58 So what we're saying is we have two variables VA data and other data and they both happen to share point to the same object
1:06 the same piece of data in memory. Let's just look at that row quick. So we can run the first line and whew that thing blew up what happened?
1:14 We have our list our list here and then we have this is our list, we're pointed out and then each one of this each part, each entry in the list
1:23 actually points to something else in memory that is a value. So it has a bunch of numbers these are all stored separately
1:29 so we have a 1, a 2, a 3, a -1 and 20 and the 7. That might not be the picture that you expected
1:34 but it's pretty straight forward to go from here to there once you know that this is what happens. What's going to happen when we go this next line?
1:42 Well, it's going to define another variable but it's also just going to point over here like this watch that.
1:48 Here we go, that's a pretty straight forward. It's nice. It means it's very fast but also has some interesting implications.
1:54 So let's go and check that out. So this list has some really cool features. What we can do is we can come to them. Just do the first one.
2:03 We can say sort and if the things in the list has some sense of order like numbers or strings like which will be sorted alphabetically
2:11 numbers in smallest to largest things like that we can just do this and it will automatically sort this data force. Now let's run this and see
2:19 and that small change makes a pretty big difference. So, whew it blows up there's our first bit of data
2:25 when it come along, we're going to define the other list or the other variable which also points at the same list
2:30 remember you're going to see right here and there point at the same object. Once that concept of them actually sharing the same thing in memory
2:37 gets in your mind you can predict what's going to happen in line four. Okay, because this can be confusing it's pretty straight forward here
2:45 'cause it's so close together there's only two lines but in real programs this relationship is not always obvious.
2:51 So what happens when we call the data that sort? If we try to print out or otherwise look at other data let's actually go ahead and make it
3:00 print that out in the end print other data there we go so when we call sort watch what happens. Because they actually share the same object
3:10 the same list in memory when we call sort on one the effect is to exactly calls sort on the other because, as you can see right here
3:19 does same thing in memory. So watch this when I call sort. Boom, see the order changes. Now that goes -1, 1, 2, 3, 7, 20.
3:26 Perfect ordering but if we print out this notice the output the value of other data is also sorted. That's because when we do relationships like this
3:38 they actually share the same piece of data and what we do in operation one obviously because there's only one of them certainly the same shared one
3:45 it has effect on both of them.


Talk Python's Mastodon Michael Kennedy's Mastodon