1 00:00:00,420 --> 00:00:01,980 - [Instructor] In this Self Check exercise, 2 00:00:01,980 --> 00:00:04,140 you'll use the techniques that you learned 3 00:00:04,140 --> 00:00:07,670 in the preceding two videos to work with slices. 4 00:00:07,670 --> 00:00:10,420 So, first we want you to start out by creating a list 5 00:00:10,420 --> 00:00:11,880 called numbers with the values 6 00:00:11,880 --> 00:00:14,410 from one through 15 in the list 7 00:00:14,410 --> 00:00:16,450 then we want you to use slices 8 00:00:16,450 --> 00:00:19,450 to perform the following four operations. 9 00:00:19,450 --> 00:00:21,760 So first we want you to create a slice 10 00:00:21,760 --> 00:00:24,410 that contains only the even integers. 11 00:00:24,410 --> 00:00:26,640 Then we want you to replace the elements 12 00:00:26,640 --> 00:00:29,340 at indices five through nine with zeros 13 00:00:29,340 --> 00:00:31,530 and show the resulting list. 14 00:00:31,530 --> 00:00:35,290 Then we want you to keep only the first five elements, 15 00:00:35,290 --> 00:00:38,770 so delete the end of the list and show the result. 16 00:00:38,770 --> 00:00:42,180 And finally we want you to delete all the remaining elements 17 00:00:42,180 --> 00:00:46,310 by assigning to a slice and also display the result. 18 00:00:46,310 --> 00:00:49,250 So go ahead and pause the video and give those a shot, 19 00:00:49,250 --> 00:00:51,173 then come back to see the answers. 20 00:00:56,390 --> 00:00:59,540 Okay, let's go ahead and start revealing the answers here. 21 00:00:59,540 --> 00:01:02,690 We'll start out by creating the list called numbers 22 00:01:02,690 --> 00:01:06,490 and you may recall that the list function is capable 23 00:01:06,490 --> 00:01:09,740 of taking a sequence that you give it as an argument 24 00:01:09,740 --> 00:01:13,360 and turning it into a list object. 25 00:01:13,360 --> 00:01:16,950 So as a result of this operation, we create the list 26 00:01:16,950 --> 00:01:20,710 starting from one up to, but not including, 16 27 00:01:20,710 --> 00:01:22,250 and we can of course display 28 00:01:22,250 --> 00:01:25,770 that to see the values from one through 15. 29 00:01:25,770 --> 00:01:29,120 Now in part A here, we wanted you to select only 30 00:01:29,120 --> 00:01:33,200 the even integers from the original list. 31 00:01:33,200 --> 00:01:35,790 So if we look at that original list, we see that 32 00:01:35,790 --> 00:01:38,730 the first even integer starts at index one, 33 00:01:38,730 --> 00:01:41,970 which is why we have a one at the beginning of our slice. 34 00:01:41,970 --> 00:01:45,510 We want to go all the way through the end of the list, 35 00:01:45,510 --> 00:01:47,873 selecting those even elements. 36 00:01:47,873 --> 00:01:52,873 So here we explicitly specify the length of the list numbers 37 00:01:53,270 --> 00:01:55,510 but we could also just leave that empty 38 00:01:55,510 --> 00:01:59,048 between the two colons and as you saw previously, 39 00:01:59,048 --> 00:02:01,720 it will automatically figure out that we want 40 00:02:01,720 --> 00:02:04,360 the entire list to be processed. 41 00:02:04,360 --> 00:02:08,120 And then finally we have a step of two, saying we want 42 00:02:08,120 --> 00:02:11,440 to go every other element throughout the list, 43 00:02:11,440 --> 00:02:13,070 as we work our way along. 44 00:02:13,070 --> 00:02:16,320 So, if we execute that, we can see that we get only 45 00:02:16,320 --> 00:02:19,656 the even integers from that operation. 46 00:02:19,656 --> 00:02:23,470 Next up we wanted in part B to replace the elements 47 00:02:23,470 --> 00:02:25,710 at indices five through nine with zeros. 48 00:02:25,710 --> 00:02:29,210 So here's the expression that will enable you to do that. 49 00:02:29,210 --> 00:02:33,680 Remember that when you specify a subset slice, 50 00:02:33,680 --> 00:02:36,020 you specify for the second index, 51 00:02:36,020 --> 00:02:38,150 the one past the N position 52 00:02:38,150 --> 00:02:40,490 so if you want to process five through nine, 53 00:02:40,490 --> 00:02:43,116 you say 5:10, start from index five 54 00:02:43,116 --> 00:02:46,430 and go up to, but not including, 10. 55 00:02:46,430 --> 00:02:48,150 Now, you may recall, 56 00:02:48,150 --> 00:02:52,710 that when you multiply a sequence by an integer, 57 00:02:52,710 --> 00:02:54,920 you get that sequence repeated 58 00:02:54,920 --> 00:02:56,950 the specified number of times. 59 00:02:56,950 --> 00:03:01,220 I demonstrated that previously with a string 60 00:03:01,220 --> 00:03:05,810 which gave us a new string containing the specified number 61 00:03:05,810 --> 00:03:08,800 of iterations of whatever we were multiplying, 62 00:03:08,800 --> 00:03:10,710 in our case it was an asterisk. 63 00:03:10,710 --> 00:03:15,028 Here we're multiplying a list containing one zero element 64 00:03:15,028 --> 00:03:20,028 by the length of this slice, numbers sub 5:10. 65 00:03:20,220 --> 00:03:23,945 So number sub 5:10, gives me a sub list 66 00:03:23,945 --> 00:03:27,750 for the index positions five, six, seven, eight and nine, 67 00:03:27,750 --> 00:03:29,860 that's a five element sub list. 68 00:03:29,860 --> 00:03:32,774 So what we're saying here is create a five element list 69 00:03:32,774 --> 00:03:37,270 of zeros and assign it to those five elements 70 00:03:37,270 --> 00:03:39,050 of the numbers list. 71 00:03:39,050 --> 00:03:41,250 So if I go ahead and execute that, 72 00:03:41,250 --> 00:03:43,510 then we can evaluate numbers 73 00:03:43,510 --> 00:03:48,378 and see that those five elements are now all zeros. 74 00:03:48,378 --> 00:03:51,360 Next up, we want in part C to keep only 75 00:03:51,360 --> 00:03:53,840 the first five elements of the list. 76 00:03:53,840 --> 00:03:56,330 So this is a way that you can express that, 77 00:03:56,330 --> 00:03:58,100 so we're saying select the sub list 78 00:03:58,100 --> 00:04:02,522 from index five through the end of the list 79 00:04:02,522 --> 00:04:04,840 which is saying select everything 80 00:04:04,840 --> 00:04:07,070 after the first five elements, 81 00:04:07,070 --> 00:04:11,470 and replace all of those elements with nothing in this case. 82 00:04:11,470 --> 00:04:13,770 So let's execute that 83 00:04:13,770 --> 00:04:16,140 and then display the contents of numbers 84 00:04:16,140 --> 00:04:17,940 and you can see we were able to keep 85 00:04:17,940 --> 00:04:20,094 just the first five elements of that list. 86 00:04:20,094 --> 00:04:23,470 And then finally, for part D we wanted 87 00:04:23,470 --> 00:04:25,670 to delete all these remaining elements 88 00:04:25,670 --> 00:04:27,059 by assigning to a slice 89 00:04:27,059 --> 00:04:30,450 and in this case, whatever's left in the list, 90 00:04:30,450 --> 00:04:35,100 this is going to select the entire copy of those elements 91 00:04:35,100 --> 00:04:37,420 and replace those elements with nothing. 92 00:04:37,420 --> 00:04:41,340 So let's execute that and finally if we evaluate numbers, 93 00:04:41,340 --> 00:04:43,573 we can see that it is now empty.