1 00:00:00,000 --> 00:00:03,000 So far I've told you that mutation 2 00:00:03,000 --> 00:00:05,000 modifies the existing object. 3 00:00:05,000 --> 00:00:10,000 But you can't really see the difference from what we've done with strings. 4 00:00:10,000 --> 00:00:14,000 Where we'll really see the difference is when we introduce a new variable. 5 00:00:14,000 --> 00:00:17,000 Let's go back to the example we had before, 6 00:00:17,000 --> 00:00:19,000 and now we're going to add an extra assignment statement, 7 00:00:19,000 --> 00:00:23,000 and we'll introduce a new variable. 8 00:00:23,000 --> 00:00:25,000 Suppose we introduce the variable "q." 9 00:00:25,000 --> 00:00:29,000 And we assign p to q, and that means the value of p, 10 00:00:29,000 --> 00:00:31,000 which is the object that's this list, 11 00:00:31,000 --> 00:00:34,000 is now what q will refer to. 12 00:00:34,000 --> 00:00:37,000 The important thing here is after the assignment, 13 00:00:37,000 --> 00:00:40,000 p and q refer to the same list. 14 00:00:40,000 --> 00:00:43,000 Suppose we did an assignment statement 15 00:00:43,000 --> 00:00:50,000 to modify the value of one of the elements of q. 16 00:00:50,000 --> 00:00:53,000 Well, that will change element 4 of q, 17 00:00:53,000 --> 00:00:55,000 so this is element 4 of q. 18 00:00:55,000 --> 00:00:58,000 It will change that value 19 00:00:58,000 --> 00:01:01,000 to the new exclamation point. 20 00:01:01,000 --> 00:01:03,000 It also changed the value of p. 21 00:01:03,000 --> 00:01:07,000 Even though the assignment statement didn't include p, 22 00:01:07,000 --> 00:01:11,000 the fact that p and q refer to the same object 23 00:01:11,000 --> 00:01:13,000 means that it changed the value of p. 24 00:01:13,000 --> 00:01:15,000 To show you that things are different with strings, 25 00:01:15,000 --> 00:01:17,000 let's try that with a string. 26 00:01:17,000 --> 00:01:20,000 See if you can guess what happens when we try to use assignment 27 00:01:20,000 --> 00:01:23,000 to replace the first letter in the string. 28 00:01:23,000 --> 00:01:25,000 Let's run it to see what happens. 29 00:01:25,000 --> 00:01:27,000 And what we get is an error, 30 00:01:27,000 --> 00:01:30,000 and we get an error because the string is not mutable. 31 00:01:30,000 --> 00:01:32,000 There's no way to change the value of the string, 32 00:01:32,000 --> 00:01:36,000 and the error says there's no way to do assignment in a string, 33 00:01:36,000 --> 00:01:41,000 that that type of object, because it's immutable, does not support assignment. 34 00:01:41,000 --> 00:01:44,000 A key difference between mutable objects and immutable objects 35 00:01:44,000 --> 00:01:47,000 is once an object is mutable, 36 00:01:47,000 --> 00:01:51,000 then we have to worry about other variables that might refer to the same object. 37 00:01:51,000 --> 00:01:53,000 We can change the value of that object, 38 00:01:53,000 --> 00:01:57,000 and it affects not just the variable that we think we changed, 39 00:01:57,000 --> 00:02:00,000 it affects the value of other variables as well. 40 00:02:00,000 --> 00:02:02,000 Let's see an example of that. 41 00:02:02,000 --> 00:02:07,000 I've initialized p to the list containing the strings "Hello." 42 00:02:07,000 --> 00:02:11,000 Now I have an assignment that introduces the new variable q 43 00:02:11,000 --> 00:02:13,000 and assigns p to that variable. 44 00:02:13,000 --> 00:02:15,000 And we'll print out the values of p and q, 45 00:02:15,000 --> 00:02:19,000 and we'll see that both p and q contain the string Hello. 46 00:02:19,000 --> 00:02:23,000 But now let's change the value at position 0. 47 00:02:23,000 --> 00:02:26,000 Now we have an assignment that stores 48 00:02:26,000 --> 00:02:30,000 in the value at position 0 of p the letter y. 49 00:02:30,000 --> 00:02:35,000 This changes the value of p. 50 00:02:35,000 --> 00:02:43,000 What may be more surprising is this also changes the value of q. 51 00:02:43,000 --> 00:02:46,000 Even though we didn't use q in the assignment, 52 00:02:46,000 --> 00:02:49,000 it changed the value of q because q 53 00:02:49,000 --> 00:02:52,000 refers to the same object as p.