1 00:00:07,940 --> 00:00:11,280 Lists are mutable, you can change them, 2 00:00:11,280 --> 00:00:16,710 you can reassign the contents at any position to be a different value. 3 00:00:16,710 --> 00:00:18,360 So, in this example, 4 00:00:18,360 --> 00:00:22,950 we're setting fruit as a variable to have the value banana, apple, 5 00:00:22,950 --> 00:00:25,140 cherry and then we're going to change it so that 6 00:00:25,140 --> 00:00:28,590 the first item isn't banana, it's pear instead. 7 00:00:28,590 --> 00:00:34,490 Let's go through this, a line at a time and draw out the reference diagram. 8 00:00:34,490 --> 00:00:41,190 On line one, we 9 00:00:41,190 --> 00:00:47,210 create a variable fruit and assign it to a list with three items. 10 00:00:47,210 --> 00:00:53,810 We have banana, we 11 00:00:53,810 --> 00:01:01,860 have apple and we've got cherry. 12 00:01:15,640 --> 00:01:19,835 If we print it out, we'll we get banana, apple and cherry. 13 00:01:19,835 --> 00:01:23,225 Then we'll get to line four where we're going to 14 00:01:23,225 --> 00:01:27,490 change the value in one of these positions. 15 00:01:27,490 --> 00:01:30,485 If previously seen indexing into lists, 16 00:01:30,485 --> 00:01:35,010 lists have positions like zero, one and two. 17 00:01:35,010 --> 00:01:43,050 So, fruit square bracket zero is referring to this position that currently has banana. 18 00:01:43,660 --> 00:01:48,920 On line four, we're not just referring to fruit square bracket zero, 19 00:01:48,920 --> 00:01:53,660 we are changing the value of fruit square bracket zero. 20 00:01:53,660 --> 00:01:58,275 We're making fruit square bracket zero equal to a new value. 21 00:01:58,275 --> 00:02:04,160 So, that has the effect of replacing the old value with a new one, 22 00:02:04,160 --> 00:02:11,230 we cross out banana and we add a new value pear. 23 00:02:14,300 --> 00:02:20,180 Then we get to line five where we're going to make another replacement. 24 00:02:20,180 --> 00:02:24,410 In this case, we're replacing at position minus one, 25 00:02:24,410 --> 00:02:27,650 just as we did for looking up values in 26 00:02:27,650 --> 00:02:32,720 a list by position zero one two or from the end position minus one, 27 00:02:32,720 --> 00:02:35,585 position minus, two position minus three, 28 00:02:35,585 --> 00:02:38,770 we can assign to those positions. 29 00:02:38,770 --> 00:02:42,290 So, fruit square bracket minus one equals 30 00:02:42,290 --> 00:02:46,100 something that's going to cause us to cross out, 31 00:02:46,100 --> 00:02:51,485 replace the current value that's there with a new value, 32 00:02:51,485 --> 00:02:54,810 in this case, orange. 33 00:02:59,260 --> 00:03:03,510 So, let's try running this, 34 00:03:11,330 --> 00:03:15,120 the first time something is printed, 35 00:03:15,120 --> 00:03:21,190 we have the original values of banana, apple and cherry. 36 00:03:21,190 --> 00:03:26,130 Then we made our two substitutions and then we printed something else. 37 00:03:26,130 --> 00:03:32,845 We printed the new value of fruit which had pear, apple and orange. 38 00:03:32,845 --> 00:03:36,470 Instead of just replacing a single item, 39 00:03:36,470 --> 00:03:39,080 we can replace the items at 40 00:03:39,080 --> 00:03:44,330 a particular slice using the same slice notation we used for looking up items, 41 00:03:44,330 --> 00:03:48,220 we can use that to find a place where we want to do a replacement. 42 00:03:48,220 --> 00:03:51,999 So, let's look at this one in codelens, 43 00:03:51,999 --> 00:03:58,260 the first line when we execute it creates a variable called alist, 44 00:03:59,480 --> 00:04:02,280 alist has, as its value, 45 00:04:02,280 --> 00:04:06,330 this list of five or six items a, b, c, d, 46 00:04:06,330 --> 00:04:13,885 e and f. Line two is going to mutate to destructively modify this list. 47 00:04:13,885 --> 00:04:17,690 Now, remember that when we use the slice notation, 48 00:04:17,690 --> 00:04:22,360 we're referring to a subsequence of the list 49 00:04:22,360 --> 00:04:27,930 and we begin at position one and we go up to, 50 00:04:27,930 --> 00:04:33,200 but not including position three. 51 00:04:33,200 --> 00:04:38,060 So, this is a slice that's two items long and we're going to 52 00:04:38,060 --> 00:04:42,540 replace those and get rid of that whole slice. 53 00:04:42,540 --> 00:04:53,560 We're going to add in two new items there, x and y. 54 00:04:54,850 --> 00:04:57,575 So, now when we print it out, 55 00:04:57,575 --> 00:05:00,620 let's just see the net effect of that, 56 00:05:00,620 --> 00:05:11,430 pure notations, we've now replaced the items at positions one and two with x and y. 57 00:05:11,690 --> 00:05:16,560 If I print it, I now get a, x, y, d, e, 58 00:05:16,560 --> 00:05:18,750 f instead of a, b, c, 59 00:05:18,750 --> 00:05:21,360 d, e, f. Now, 60 00:05:21,360 --> 00:05:25,110 we're not required to have the things that 61 00:05:25,110 --> 00:05:31,145 we add in be of the same length as the things that are being replaced. 62 00:05:31,145 --> 00:05:38,670 Here's an example showing how we can actually use this reassignment to delete items. 63 00:05:39,650 --> 00:05:45,150 So, in this case, we first assign alist to have the values a, 64 00:05:45,150 --> 00:05:47,740 b, c, d, e and f, 65 00:05:47,740 --> 00:05:51,465 we're going to start at position one, 66 00:05:51,465 --> 00:05:54,045 go up to position three, 67 00:05:54,045 --> 00:05:57,050 but not including position three and we're going to delete 68 00:05:57,050 --> 00:06:02,310 those items and replace them with the empty list. 69 00:06:02,440 --> 00:06:06,080 We're going to replace them with all of the items that are 70 00:06:06,080 --> 00:06:09,595 in this empty list which is nothing. 71 00:06:09,595 --> 00:06:12,890 So, we're really going to just get a, d, 72 00:06:12,890 --> 00:06:20,980 e and f. Sure enough when I go forward, 73 00:06:21,530 --> 00:06:24,450 I get a, d, 74 00:06:24,450 --> 00:06:30,240 e and f. Lists are mutable, strings aren't. 75 00:06:30,240 --> 00:06:34,095 We could reassign to position zero in a list, 76 00:06:34,095 --> 00:06:38,265 we cannot reassign to position zero in a string. 77 00:06:38,265 --> 00:06:41,120 So, for example, if I run this code, 78 00:06:41,120 --> 00:06:46,965 I set the variable greeting to be a string and then 79 00:06:46,965 --> 00:06:55,120 I try to change the thing at position zero just analogous to what we were doing before, 80 00:06:55,120 --> 00:06:58,820 we had reading, 81 00:06:59,240 --> 00:07:09,590 bound to this string. 82 00:07:38,990 --> 00:07:42,210 So, we have greeting bound to that string. 83 00:07:42,210 --> 00:07:46,410 In position zero, we've got 84 00:07:46,410 --> 00:07:51,385 that h and you might think that we could just replace the h with a j, 85 00:07:51,385 --> 00:07:53,165 but we can't do that, 86 00:07:53,165 --> 00:07:56,950 that is not permitted and we get this error message. 87 00:07:56,950 --> 00:07:59,900 You get a message saying, 88 00:07:59,910 --> 00:08:05,675 "Type string does not support item assignment, 89 00:08:05,675 --> 00:08:11,820 we can't reassign a value within a string," Now, 90 00:08:11,820 --> 00:08:13,755 suppose we really did want to take, 91 00:08:13,755 --> 00:08:18,360 our Hello world string and turn it into Jello world. 92 00:08:18,360 --> 00:08:22,320 How could we do that? We can't reassign at position zero, 93 00:08:22,320 --> 00:08:26,045 that's what we found out just a moment ago. So, how do we do it? 94 00:08:26,045 --> 00:08:29,810 Well, we can take the string Hello world and we can 95 00:08:29,810 --> 00:08:34,170 make a new string that has some of Hello world, 96 00:08:34,170 --> 00:08:36,945 but a little bit different. 97 00:08:36,945 --> 00:08:39,465 In particular, we're going to take that 98 00:08:39,465 --> 00:08:49,520 Hello world and we're 99 00:08:49,520 --> 00:08:56,264 going to start at position, 100 00:08:56,264 --> 00:08:57,450 so this position zero, 101 00:08:57,450 --> 00:09:00,345 position one, two, three and so on. 102 00:09:00,345 --> 00:09:06,770 We're going to start at one and go all the way to the end. 103 00:09:07,250 --> 00:09:11,175 That's what it tells us when we say greeting, 104 00:09:11,175 --> 00:09:14,205 square bracket one colon. 105 00:09:14,205 --> 00:09:17,530 Greeting is the name of our variable 106 00:09:26,220 --> 00:09:31,210 and we're going to start at position one that's with the letter 107 00:09:31,210 --> 00:09:35,740 e and go all the way to the end because we don't have anything after the colon, 108 00:09:35,740 --> 00:09:39,505 that means, keep going all the way to the end of the sequence. 109 00:09:39,505 --> 00:09:45,670 So, that gives us Hello world and if we take 110 00:09:45,890 --> 00:09:52,880 Hello world concatenate that 111 00:09:52,880 --> 00:09:58,860 onto J we get Jello world. 112 00:10:08,540 --> 00:10:12,120 Then we have Jello world. 113 00:10:12,120 --> 00:10:14,505 Now notice, on line four, 114 00:10:14,505 --> 00:10:17,620 it says print greeting. 115 00:10:20,560 --> 00:10:25,730 Greeting is still Hello world. 116 00:10:25,730 --> 00:10:29,275 So, that's why we get this is our second line. 117 00:10:29,275 --> 00:10:37,680 New greeting was bound 118 00:10:37,680 --> 00:10:44,410 to our new string Jello world and that's why we get this printed out. 119 00:10:45,070 --> 00:10:49,550 So, you can't change the original string Hello world, 120 00:10:49,550 --> 00:10:53,820 but you can make a new string using a lot of the letters from 121 00:10:53,820 --> 00:10:59,690 Hello world and just concatenating them onto a new first letter. 122 00:11:00,170 --> 00:11:07,650 Here's an example of concatenating together a bigger string from parts. 123 00:11:07,820 --> 00:11:11,410 Let's show this one in codelens. 124 00:11:11,660 --> 00:11:15,120 We first start with a small phrase, 125 00:11:15,120 --> 00:11:18,585 many moons, we want to expand that. 126 00:11:18,585 --> 00:11:26,430 So, we define a new string that has many moons plus some more letters. 127 00:11:27,020 --> 00:11:32,875 So, we still have the old variable phrase, 128 00:11:32,875 --> 00:11:38,020 but we have an additional variable now called phrase expanded. 129 00:11:38,020 --> 00:11:41,570 We can keep doing that we can make it even longer. 130 00:11:41,570 --> 00:11:45,060 That's what's going to happen in step three. 131 00:11:49,160 --> 00:11:53,170 So, phrase larger is yet a longer string. 132 00:11:53,170 --> 00:11:57,910 Notice that the previous variables are still bound to their original values. 133 00:11:57,910 --> 00:12:03,380 Then finally, we're going to replace that small m with 134 00:12:03,380 --> 00:12:10,585 a capital M. So, 135 00:12:10,585 --> 00:12:16,070 you'll be assembling longer strings from shorter strings in this way and it 136 00:12:16,070 --> 00:12:18,710 will let you put variables in the middle of 137 00:12:18,710 --> 00:12:22,920 your strings to generate slightly different strings each time.