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