1
00:00:04,660 --> 00:00:09,860
Let's talk about mutable objects now. So
a mutable object is one whose value can

2
00:00:09,860 --> 00:00:15,349
be changed. Python has the following
mutable objects built in: list, dict,

3
00:00:15,349 --> 00:00:20,150
set and byte array. We'll be looking at
dictionaries and sets in the next

4
00:00:20,150 --> 00:00:24,230
section in the course. So we can change
the value of mutable objects, without the

5
00:00:24,230 --> 00:00:29,610
object being destroyed and re-created.
Alright, so let's see how our previous

6
00:00:29,610 --> 00:00:32,580
example works when we're dealing with a
mutable object. I'm going to be using the

7
00:00:32,580 --> 00:00:38,400
shopping list that we saw in a previous
section, and create a new Python file.

8
00:00:38,400 --> 00:00:44,610
This time I'm going to call the file
mutable.py. I'm going to paste in the

9
00:00:44,610 --> 00:00:47,910
definition for shopping list so grab
that from the previous section. You don't

10
00:00:47,910 --> 00:00:52,260
want to do our typing in here. Now this
is future Tim putting an edit in this

11
00:00:52,260 --> 00:00:55,860
program. I wondered whether you just
noticed the mistake I made when I created

12
00:00:55,860 --> 00:01:01,380
mutable.py. So let's open the project
pane again. Notice the file mutable.py

13
00:01:01,380 --> 00:01:05,900
doesn't exist in there, and that's
because I had the .idea folder selected,

14
00:01:05,900 --> 00:01:09,240
and the file actually went in there when
I open it. So that happens - it's actually

15
00:01:09,240 --> 00:01:12,020
the wrong place to put it in. Now
obviously, it'll still work -

16
00:01:12,020 --> 00:01:15,180
you can see that we haven't got any
errors. To move it back, though, just

17
00:01:15,180 --> 00:01:18,750
select the file, drag it back up to the
top of the project - Sequences, in this

18
00:01:18,750 --> 00:01:22,740
case. You'll get a warning here telling
you, basically, are you sure you want to

19
00:01:22,740 --> 00:01:25,830
move it, in which folder. We're going to
go to the folder that I selected, click

20
00:01:25,830 --> 00:01:29,520
on OK, and you can see mutable.py
now exists in the right place and it's

21
00:01:29,520 --> 00:01:34,940
been taken out of the .idea folder. Alright, so let's go back to coding now.

22
00:01:34,940 --> 00:01:39,510
We're going to add some code to this and
start by typing another, or under that,

23
00:01:39,510 --> 00:01:46,110
another_list is equal to shopping_list, and we'll do the

24
00:01:46,110 --> 00:01:49,200
same thing - we're gonna print the IDs. So print id left

25
00:01:49,200 --> 00:01:52,950
parenthesis and shopping_list,
and the next line we're going to do the

26
00:01:52,950 --> 00:01:56,520
same, this time for another_list - very similar to what we did in the

27
00:01:56,520 --> 00:02:02,790
last video. Run the program - checking
the IDs, you can see not surprisingly,

28
00:02:02,790 --> 00:02:07,440
we get the same id for both variables.
Alright, so with that in mind, let's now

29
00:02:07,440 --> 00:02:11,280
attempt to mutate our list. Now I'm going
to be going through all the things you

30
00:02:11,280 --> 00:02:16,019
can do with lists, shortly. For now, though,
I'll use augmented assignment to concatenate

31
00:02:16,019 --> 00:02:20,190
another list to the end of this one.
That's very similar to concatenating one

32
00:02:20,190 --> 00:02:28,880
string onto another. So I'm going to type
shopping_list plus equals

33
00:02:28,880 --> 00:02:34,800
and left square brackets, double quotes
cookies. Just the one entry in the list there.

34
00:02:34,800 --> 00:02:38,600
Then we're going to print the
shopping list.

35
00:02:40,090 --> 00:02:45,440
Run this, and I've printed out the shopping
list there because we haven't seen lists

36
00:02:45,440 --> 00:02:51,110
being concatenated before. So notice that
I put a list, after the plus equals on

37
00:02:51,110 --> 00:02:54,800
line 12. We have to enclose the string
cookies in square brackets, to make it a

38
00:02:54,800 --> 00:02:59,120
list, as we've done there. And as you can
see by output, cookies is now in the list.

39
00:02:59,120 --> 00:03:03,230
Alright, so we've successfully mutated
our list - we've added cookies to it.

40
00:03:03,230 --> 00:03:09,290
Let's check the ID to see what's
happened. So I'm going to take a copy of

41
00:03:09,290 --> 00:03:16,370
line 9 and paste that below line 13, so
on line 14, in other words. So before we

42
00:03:16,370 --> 00:03:21,020
actually run this, what do you think will
happen? Will the ID remain the same, or

43
00:03:21,020 --> 00:03:27,820
will we get a new ID after changing the
list? Well let's see. So I'm going to run the program.

44
00:03:27,820 --> 00:03:29,750
Notice here now, that the ID remains

45
00:03:29,750 --> 00:03:35,210
unchanged, and that's because lists are
mutable and Python was able to add a new

46
00:03:35,210 --> 00:03:40,520
item to the end of the list, without creating a new list.

47
00:03:40,520 --> 00:03:44,319
So now that you've seen the same thing done
with a mutable object, and have seen the

48
00:03:44,319 --> 00:03:48,610
different behavior, the previous video on
immutable objects, should hopefully make

49
00:03:48,610 --> 00:03:54,099
more sense. Strings are immutable. When we
tried to change a string, Python created

50
00:03:54,099 --> 00:03:59,739
a new object - a new string - and re-attached
the name to it. Lists are mutable - they

51
00:03:59,739 --> 00:04:03,970
can be changed. When we appended a new
item in this code, Python was able to

52
00:04:03,970 --> 00:04:09,320
change the contents of the list, without
creating a new one.

53
00:04:09,320 --> 00:04:13,940
So that's the difference between mutable
and immutable objects. An immutable

54
00:04:13,940 --> 00:04:18,260
object can't be changed. You can create a
new object, and use the same variable

55
00:04:18,260 --> 00:04:21,410
name for it, but you can't change the value of an

56
00:04:21,410 --> 00:04:25,790
immutable object. Mutable objects, such as lists, can be changed, and I'm showing you an

57
00:04:25,790 --> 00:04:29,840
example of that in this video. So walk
through the code from this video, and

58
00:04:29,840 --> 00:04:33,110
perhaps the last one again, to check the
different behavior. When you're happy

59
00:04:33,110 --> 00:04:37,040
with both videos, we'll have a look at
one implication of this difference.

60
00:04:37,040 --> 00:04:40,000
See you in the next video.

