1
00:00:03,740 --> 00:00:08,360
I finished the last video by saying that
strings are immutable,

2
00:00:08,360 --> 00:00:14,780
whereas lists are mutable. When an object
is described as immutable, that means it

3
00:00:14,780 --> 00:00:18,050
can't be changed.
The following immutable types are built

4
00:00:18,050 --> 00:00:24,619
into Python: int, float, bool - which is True
or False, a subclass of int, string or str,

5
00:00:24,619 --> 00:00:32,029
which is a string, tuple, frozenset and
bytes. So it should be obvious that an

6
00:00:32,029 --> 00:00:37,550
int like 5 can't be changed. 5 is always
5. You can add another value to it - for

7
00:00:37,550 --> 00:00:44,480
example, we can add 1 to 5 and we get a
new number 6: However, we can't change 5.

8
00:00:44,480 --> 00:00:51,980
The same's true of floats. 3.14159 will
always be 3.14159. Once again, we can

9
00:00:51,980 --> 00:00:56,690
perform arithmetic with a float, and get
a new float value, but we can't change

10
00:00:56,690 --> 00:01:01,610
the value of 3.14159. So stay with me on
this, because there might be an obvious

11
00:01:01,610 --> 00:01:05,330
question that you're asking.
I'll explain that next, but at the moment,

12
00:01:05,330 --> 00:01:13,900
trust me that these values are immutable.
bool values are also immutable, and so are strings.

13
00:01:13,900 --> 00:01:17,300
The question you're asking is
probably becoming disbelief now.

14
00:01:17,310 --> 00:01:20,350
So let's type in some code now, that proves this.

15
00:01:20,350 --> 00:01:26,860
So I'm going to create a new Python file
here, called immutable, for these examples.

16
00:01:34,740 --> 00:01:39,860
So let's start by typing result equals
True - True with a capital T,

17
00:01:39,860 --> 00:01:48,380
another_result is equal to result,
print on the next line, left parenthesis id

18
00:01:48,380 --> 00:01:53,200
left parenthesis result then two right
parantheses. Then on line 4, print left

19
00:01:53,200 --> 00:01:57,130
parenthesis id, a left parenthesis
another_result and we've

20
00:01:57,130 --> 00:02:01,420
got our two right parentheses to end
the line. Alright, so here, we've got two

21
00:02:01,420 --> 00:02:07,090
variables; result and another_result. They're both bound to the value

22
00:02:07,090 --> 00:02:12,680
True. On lines three and four, are
printed out the ID of those variables.

23
00:02:12,680 --> 00:02:18,800
Now it's worth checking the documentation for the ID function, so I'll just briefly do that.

24
00:02:21,420 --> 00:02:27,360
So as you can see there, id
returns the identity of an object.

25
00:02:27,379 --> 00:02:31,639
Now the documentation doesn't state that
it should be the object's address, just

26
00:02:31,639 --> 00:02:36,260
that it must be "guaranteed to be unique
and constant for this object during its

27
00:02:36,260 --> 00:02:41,689
lifetime". The CPython implementation does
return the object's memory address, but

28
00:02:41,689 --> 00:02:46,639
not all Pythons will do that. The ID for
an object may be different each time you

29
00:02:46,640 --> 00:02:51,280
run the program, but while your program
is running, the object will have the same id.

30
00:02:51,280 --> 00:02:53,299
Of course, if python has to destroy the

31
00:02:53,299 --> 00:02:58,189
object and re-create it, then it's ID will
also change. So that gives us a good way

32
00:02:58,189 --> 00:03:04,080
to tell if an object is changed, or if
python has to create a new object.

33
00:03:04,080 --> 00:03:11,620
Let's go back to our code. Let's run it. We can
see here that both variables have the

34
00:03:11,629 --> 00:03:17,060
same ID. They're both bound to the same
value - True - so that makes sense that it

35
00:03:17,060 --> 00:03:21,580
should have the same ID. Remember that
we're printing the ID of True here.

36
00:03:21,580 --> 00:03:26,580
The variables - result and another_result - are just names that we've bound

37
00:03:26,580 --> 00:03:31,099
to that value. Alright, so if bool
values could be changed, then changing

38
00:03:31,099 --> 00:03:34,909
the values should mean that the ID
doesn't change. So let's see if that's

39
00:03:34,909 --> 00:03:43,099
the case, so I'm going to add some new
code down here, line 6, result equals

40
00:03:43,099 --> 00:03:47,569
False with a capital F. Then we're going
to print left parenthesis id left

41
00:03:47,569 --> 00:03:52,129
parenthesis result, then two right
parentheses to end the line. Let's run

42
00:03:52,129 --> 00:03:58,099
that code, and you can see here, we've got
a different ID for a result with the

43
00:03:58,099 --> 00:04:03,079
code that we've just added. Because bools
are immutable, we weren't able to change

44
00:04:03,080 --> 00:04:10,080
the value of True. What Python's done
instead, is rebound result to a new value - False.

45
00:04:10,080 --> 00:04:13,099
When we were looking at the slides,
when I said that bools were immutable,

46
00:04:13,099 --> 00:04:16,820
you may have been thinking, but hang on -
I can assign a new value to a boolean

47
00:04:16,820 --> 00:04:22,220
variable. Now that would be true in other
languages. In C or Java, for example,

48
00:04:22,220 --> 00:04:26,419
you can assign new values to boolean
variables. In Python, though, that's not

49
00:04:26,419 --> 00:04:29,840
the case.
Instead, we've attached the name result,

50
00:04:29,840 --> 00:04:39,300
to a new value. So let's comment out all
that code, and here's another example here.

51
00:04:39,300 --> 00:04:41,920
I'm going to type result equals double

52
00:04:41,920 --> 00:04:46,560
quotes Correct with a capital C, and
that's in double quotes, as I mentioned,

53
00:04:46,560 --> 00:04:53,820
and then on line 10, I'm going to type
another_result is equal to result,

54
00:04:53,820 --> 00:04:56,100
and I probably could have pasted
those lines before I commented it out but

55
00:04:56,100 --> 00:05:00,240
I'll just type it again: print and left
parentheses, id left parenthesis

56
00:05:00,240 --> 00:05:06,520
result, and the same thing, this time for
another_result. So I'm printing

57
00:05:06,520 --> 00:05:13,280
the ID for both variables, again, as we
did earlier. Alright, so let's run the program,

58
00:05:13,280 --> 00:05:17,880
and you can see here that the
variables, again, have the same id.

59
00:05:17,890 --> 00:05:22,210
Alright, so moving on, let's attempt to
mutate result. Now we can use augmented

60
00:05:22,210 --> 00:05:25,540
assignment to concatenate more
characters to the end. Let's have a go at

61
00:05:25,540 --> 00:05:37,660
doing that: result plus equals double
quotes ish. I'll print the id again.

62
00:05:37,660 --> 00:05:41,340
So if we could mutate the string, then the
value that result is bound to would

63
00:05:41,350 --> 00:05:45,910
change. The ID should remain the same if
that happened. Let's run the program and

64
00:05:45,910 --> 00:05:52,480
see what happens - and it's not. As you can
see there, result now has a different id.

65
00:05:52,480 --> 00:05:55,480
Now just in case you're not convinced,
I'm going to print the id for

66
00:05:55,480 --> 00:06:00,760
another_result as well, so let's do
that. I'll just take a copy of that from

67
00:06:00,760 --> 00:06:09,130
line 12 and paste it in. Run that. So
notice that the id of result has

68
00:06:09,130 --> 00:06:12,790
changed, but another_result
still has the same id that it had to

69
00:06:12,790 --> 00:06:17,080
start with. Because strings are immutable,
Python couldn't change the string

70
00:06:17,080 --> 00:06:21,700
correct. What it did instead was create a
new string, and bind the name result to

71
00:06:21,700 --> 00:06:24,910
this new string. Now don't worry if
you're still a bit confused about this -

72
00:06:24,910 --> 00:06:28,660
it will make sense once you've seen the
same thing with a mutable object,

73
00:06:28,660 --> 00:06:32,580
and we'll look at doing that in the next video.

