WEBVTT 00:00:00.000 --> 00:00:03.000 So far I've told you that mutation 00:00:03.000 --> 00:00:05.000 modifies the existing object. 00:00:05.000 --> 00:00:10.000 But you can't really see the difference from what we've done with strings. 00:00:10.000 --> 00:00:14.000 Where we'll really see the difference is when we introduce a new variable. 00:00:14.000 --> 00:00:17.000 Let's go back to the example we had before, 00:00:17.000 --> 00:00:19.000 and now we're going to add an extra assignment statement, 00:00:19.000 --> 00:00:23.000 and we'll introduce a new variable. 00:00:23.000 --> 00:00:25.000 Suppose we introduce the variable "q." 00:00:25.000 --> 00:00:29.000 And we assign p to q, and that means the value of p, 00:00:29.000 --> 00:00:31.000 which is the object that's this list, 00:00:31.000 --> 00:00:34.000 is now what q will refer to. 00:00:34.000 --> 00:00:37.000 The important thing here is after the assignment, 00:00:37.000 --> 00:00:40.000 p and q refer to the same list. 00:00:40.000 --> 00:00:43.000 Suppose we did an assignment statement 00:00:43.000 --> 00:00:50.000 to modify the value of one of the elements of q. 00:00:50.000 --> 00:00:53.000 Well, that will change element 4 of q, 00:00:53.000 --> 00:00:55.000 so this is element 4 of q. 00:00:55.000 --> 00:00:58.000 It will change that value 00:00:58.000 --> 00:01:01.000 to the new exclamation point. 00:01:01.000 --> 00:01:03.000 It also changed the value of p. 00:01:03.000 --> 00:01:07.000 Even though the assignment statement didn't include p, 00:01:07.000 --> 00:01:11.000 the fact that p and q refer to the same object 00:01:11.000 --> 00:01:13.000 means that it changed the value of p. 00:01:13.000 --> 00:01:15.000 To show you that things are different with strings, 00:01:15.000 --> 00:01:17.000 let's try that with a string. 00:01:17.000 --> 00:01:20.000 See if you can guess what happens when we try to use assignment 00:01:20.000 --> 00:01:23.000 to replace the first letter in the string. 00:01:23.000 --> 00:01:25.000 Let's run it to see what happens. 00:01:25.000 --> 00:01:27.000 And what we get is an error, 00:01:27.000 --> 00:01:30.000 and we get an error because the string is not mutable. 00:01:30.000 --> 00:01:32.000 There's no way to change the value of the string, 00:01:32.000 --> 00:01:36.000 and the error says there's no way to do assignment in a string, 00:01:36.000 --> 00:01:41.000 that that type of object, because it's immutable, does not support assignment. 00:01:41.000 --> 00:01:44.000 A key difference between mutable objects and immutable objects 00:01:44.000 --> 00:01:47.000 is once an object is mutable, 00:01:47.000 --> 00:01:51.000 then we have to worry about other variables that might refer to the same object. 00:01:51.000 --> 00:01:53.000 We can change the value of that object, 00:01:53.000 --> 00:01:57.000 and it affects not just the variable that we think we changed, 00:01:57.000 --> 00:02:00.000 it affects the value of other variables as well. 00:02:00.000 --> 00:02:02.000 Let's see an example of that. 00:02:02.000 --> 00:02:07.000 I've initialized p to the list containing the strings "Hello." 00:02:07.000 --> 00:02:11.000 Now I have an assignment that introduces the new variable q 00:02:11.000 --> 00:02:13.000 and assigns p to that variable. 00:02:13.000 --> 00:02:15.000 And we'll print out the values of p and q, 00:02:15.000 --> 00:02:19.000 and we'll see that both p and q contain the string Hello. 00:02:19.000 --> 00:02:23.000 But now let's change the value at position 0. 00:02:23.000 --> 00:02:26.000 Now we have an assignment that stores 00:02:26.000 --> 00:02:30.000 in the value at position 0 of p the letter y. 00:02:30.000 --> 00:02:35.000 This changes the value of p. 00:02:35.000 --> 00:02:43.000 What may be more surprising is this also changes the value of q. 00:02:43.000 --> 00:02:46.000 Even though we didn't use q in the assignment, 00:02:46.000 --> 00:02:49.000 it changed the value of q because q 00:02:49.000 --> 00:02:52.000 refers to the same object as p.