1
00:00:00,000 --> 00:00:03,990
Objects in Python may be mutable or immutable.

2
00:00:04,000 --> 00:00:08,990
Mutable objects may change value; immutable objects may not.

3
00:00:09,000 --> 00:00:12,990
Some immutable objects at times may look like they are changing their values.

4
00:00:13,000 --> 00:00:15,990
They are not actually changing their values though. The distinction is visible

5
00:00:16,000 --> 00:00:18,990
using the id function.

6
00:00:19,000 --> 00:00:23,990
Integers, container objects, strings, things like these may appear to be

7
00:00:24,000 --> 00:00:25,990
changing values when they are not.

8
00:00:26,000 --> 00:00:31,990
For example, here in the Python shell, we can create an object, and we'll call it x,

9
00:00:32,000 --> 00:00:34,990
and we'll assign a number to it, 42.

10
00:00:35,000 --> 00:00:38,990
Integers are immutable objects in Python, and if we look at the value, we see that it's 42.

11
00:00:39,000 --> 00:00:43,990
If we look at its type, we see that its type is int.

12
00:00:44,000 --> 00:00:50,990
If we look at the id, we see that it has this unique identifier.

13
00:00:51,000 --> 00:00:55,990
Now if I change the value of x, and remember it's an immutable object so I cannot

14
00:00:56,000 --> 00:00:59,990
change the value of an integer, but I assign x = 43,

15
00:01:00,000 --> 00:01:06,990
what I've actually done is I've changed this variable x to refer to a different integer.

16
00:01:07,000 --> 00:01:09,990
Variables in Python are references to objects.

17
00:01:10,000 --> 00:01:13,990
So this reference is now referring to a different object.

18
00:01:14,000 --> 00:01:18,990
If I type id of x, you'll see I get a different identifier.

19
00:01:19,000 --> 00:01:20,990
If I type x, I see that it's 43.

20
00:01:21,000 --> 00:01:29,990
If I assign x back to 42 and take its id, you'll see that this id is now exactly

21
00:01:30,000 --> 00:01:32,990
the same as the one up there.

22
00:01:33,000 --> 00:01:38,990
So I did not actually change the immutable value. I simply changed the variable

23
00:01:39,000 --> 00:01:40,990
to refer to a different object.

24
00:01:41,000 --> 00:01:44,990
This is an important distinction, and it's one that may take a while for you

25
00:01:45,000 --> 00:01:48,990
to get your head around, but we'll cover some of those as we get into this in more detail.

26
00:01:49,000 --> 00:01:53,990
There will be times when something that you want to do, like change the value of

27
00:01:54,000 --> 00:01:59,990
a string or to insert objects in a tuple, you may find that you need to use a

28
00:02:00,000 --> 00:02:04,990
different type, a mutable type instead of an immutable type, in order to be able

29
00:02:05,000 --> 00:02:07,990
to accomplish where it is you are trying to accomplish.

30
00:02:08,000 --> 00:02:12,990
In fact, most fundamental types in Python are immutable. Numbers, strings, and

31
00:02:13,000 --> 00:02:16,990
tuples are the fundamental types that are immutable.

32
00:02:17,000 --> 00:02:20,990
Mutable objects include lists, dictionaries and some other objects, depending

33
00:02:21,000 --> 00:02:22,990
upon their implementation.

34
00:02:23,000 --> 00:02:26,990
So as you create your code in Python, simply be aware that there are some

35
00:02:27,000 --> 00:02:28,990
types that are immutable -

36
00:02:29,000 --> 00:02:32,990
and these are typically the more fundamental types - and other types that are mutable.

37
00:02:33,000 --> 00:02:35,990
And as we get into the details of these types, we'll talk about the things you

38
00:02:36,000 --> 00:02:46,000
can and cannot do with mutable and immutable objects.

