1 00:00:00,370 --> 00:00:02,060 - [Instructor] In this self check exercise, 2 00:00:02,060 --> 00:00:05,740 we have a couple of exercises for you to perform. 3 00:00:05,740 --> 00:00:08,540 In the first one, we would like you to use a 4 00:00:08,540 --> 00:00:11,360 list comprehension, using the techniques that you just saw 5 00:00:11,360 --> 00:00:14,783 in the preceding video, to create a list of tuples, 6 00:00:14,783 --> 00:00:17,320 in which you'll have the numbers from one 7 00:00:17,320 --> 00:00:19,970 through five and their corresponding cubes. 8 00:00:19,970 --> 00:00:22,410 So your list is going to look something like 9 00:00:22,410 --> 00:00:25,320 what we show here, so one cubed is one, 10 00:00:25,320 --> 00:00:28,125 two cubed is eight, three cubed is 27. 11 00:00:28,125 --> 00:00:31,550 Now, we haven't of course shown you creating tuples 12 00:00:31,550 --> 00:00:34,030 with a list comprehension yet, so it is important 13 00:00:34,030 --> 00:00:36,808 to point out that in order for you to create tuples, 14 00:00:36,808 --> 00:00:40,070 the expression to the left of the for clause 15 00:00:40,070 --> 00:00:44,220 has to have parentheses around it, and it would then 16 00:00:44,220 --> 00:00:46,900 contain a comma separated list representing 17 00:00:46,900 --> 00:00:50,375 the elements that would be placed into that tuple. 18 00:00:50,375 --> 00:00:53,010 Now for the other exercise, we want you to use 19 00:00:53,010 --> 00:00:55,360 a list comprehension and the range function, 20 00:00:55,360 --> 00:00:58,430 with a step this time, to create a list of 21 00:00:58,430 --> 00:01:01,322 the multiples of three that are less than 30. 22 00:01:01,322 --> 00:01:04,370 So go ahead and pause the video and give those a shot, 23 00:01:04,370 --> 00:01:06,383 and then come back to see the answers. 24 00:01:10,723 --> 00:01:13,180 Okay, let's go ahead and reveal the 25 00:01:13,180 --> 00:01:15,580 answer to this first exercise here. 26 00:01:15,580 --> 00:01:17,942 Now again, we're trying to create tuples 27 00:01:17,942 --> 00:01:21,480 based on the values in the range one through five. 28 00:01:21,480 --> 00:01:24,280 So first, let's deal with the range part here. 29 00:01:24,280 --> 00:01:27,772 For every item called X in the range one up to 30 00:01:27,772 --> 00:01:30,540 but not including six, so one through five, 31 00:01:30,540 --> 00:01:32,602 we're going to take that item, X, 32 00:01:32,602 --> 00:01:37,602 and place it into a tuple of items, and the second element 33 00:01:38,416 --> 00:01:42,635 in that tuple is going to be X's value cubed. 34 00:01:42,635 --> 00:01:46,880 So we give X and X cubed, we wrap that in parentheses 35 00:01:46,880 --> 00:01:49,440 because that is required when you're putting a tuple 36 00:01:49,440 --> 00:01:52,660 to the left of the for keyword in a list comprehension, 37 00:01:52,660 --> 00:01:57,090 and if we then go ahead and execute that and evaluate cubes, 38 00:01:57,090 --> 00:02:00,340 we can see that indeed, we get the tuples 39 00:02:00,340 --> 00:02:03,621 containing each value and its corresponding cube. 40 00:02:03,621 --> 00:02:07,160 Now for the second exercise, again we wanted to create 41 00:02:07,160 --> 00:02:10,260 a list of the multiples of three less than 30, 42 00:02:10,260 --> 00:02:13,870 and we wanted to use a range with a step to do that. 43 00:02:13,870 --> 00:02:18,484 So in this case, our for clause uses the range three 44 00:02:18,484 --> 00:02:23,484 up to but not including 30, counting by three each time. 45 00:02:23,610 --> 00:02:27,050 So for every value in that range, we're simply going to 46 00:02:27,050 --> 00:02:31,079 include that value in our list, and we simply name the 47 00:02:31,079 --> 00:02:35,190 target variable in order to include that value in the list. 48 00:02:35,190 --> 00:02:38,488 So if I execute that cell and then evaluate multiples, 49 00:02:38,488 --> 00:02:41,340 you can see we get all the multiples of three, 50 00:02:41,340 --> 00:02:43,160 up to but not including 30. 51 00:02:43,160 --> 00:02:46,913 So the last one is 27 in this case.