1 00:00:00,940 --> 00:00:02,370 - [Instructor] In this self check exercise, 2 00:00:02,370 --> 00:00:04,240 we have a couple of different problems 3 00:00:04,240 --> 00:00:05,810 that we'd like you to attack. 4 00:00:05,810 --> 00:00:08,570 First, we want you to create a tuple called high-low 5 00:00:08,570 --> 00:00:11,800 that represents a day of the week, along with the high 6 00:00:11,800 --> 00:00:14,990 and the low temperature as integers for that day. 7 00:00:14,990 --> 00:00:18,960 And we'd like you to display its string representation 8 00:00:18,960 --> 00:00:23,240 and then perform these two tasks using that high-low tuple. 9 00:00:23,240 --> 00:00:26,620 First, we want you to access each of the individual values 10 00:00:26,620 --> 00:00:29,520 in that tuple via index numbers 11 00:00:29,520 --> 00:00:32,000 and display the element values. 12 00:00:32,000 --> 00:00:35,090 And then, we want you to unpack that tuple 13 00:00:35,090 --> 00:00:38,480 into just the variables Day and High, 14 00:00:38,480 --> 00:00:43,140 and show what happens and explain why that happens, as well. 15 00:00:43,140 --> 00:00:47,170 In the second exercise, we want you to create a list 16 00:00:47,170 --> 00:00:51,280 of names containing three name strings, and then enumerate 17 00:00:51,280 --> 00:00:54,980 those values using the enumerate function so that you 18 00:00:54,980 --> 00:00:59,870 can display each element's index and corresponding value. 19 00:00:59,870 --> 00:01:02,440 So go ahead and pause this video, 20 00:01:02,440 --> 00:01:05,580 take a shot at the two exercises you see here on the screen, 21 00:01:05,580 --> 00:01:07,793 and then come back to see the answers. 22 00:01:12,620 --> 00:01:15,960 Okay, let's go ahead and reveal the answer here 23 00:01:15,960 --> 00:01:17,940 for this first exercise. 24 00:01:17,940 --> 00:01:20,660 So again, initially we're going to create the tuple 25 00:01:20,660 --> 00:01:25,210 called high-low, and we'll stick into that a name of a day 26 00:01:25,210 --> 00:01:27,750 and a couple of integer values representing the high 27 00:01:27,750 --> 00:01:29,610 and low temperatures, respectively. 28 00:01:29,610 --> 00:01:31,560 So let me go ahead and execute that. 29 00:01:31,560 --> 00:01:34,640 And to display its string representation, we can simply 30 00:01:34,640 --> 00:01:36,310 evaluate the variable. 31 00:01:36,310 --> 00:01:39,450 So here is that string representation. 32 00:01:39,450 --> 00:01:42,610 Now in Part A up above, we said we wanted you to use 33 00:01:42,610 --> 00:01:46,000 the square brackets operator and index numbers 34 00:01:46,000 --> 00:01:49,490 to access each of the three pieces of information. 35 00:01:49,490 --> 00:01:52,320 So here, we're going to produce a formatted string, 36 00:01:52,320 --> 00:01:55,150 in which the first placeholder is going to access 37 00:01:55,150 --> 00:01:57,880 element zero of the tuple, which is the day. 38 00:01:57,880 --> 00:02:00,980 The second placeholder is going to access element one 39 00:02:00,980 --> 00:02:03,180 of the tuple, which is the high temperature. 40 00:02:03,180 --> 00:02:06,670 And the last is going to access the low temperature 41 00:02:06,670 --> 00:02:07,860 within that tuple. 42 00:02:07,860 --> 00:02:11,250 So let's just display that and we can see that, indeed, 43 00:02:11,250 --> 00:02:14,870 we were able to pick off each of those elements. 44 00:02:14,870 --> 00:02:17,840 Now, you may recall from the preceding couple of videos 45 00:02:17,840 --> 00:02:20,960 that, when you're unpacking a tuple or any other sequence, 46 00:02:20,960 --> 00:02:24,340 the left side of the assignment must have the appropriate 47 00:02:24,340 --> 00:02:27,920 number of variables to match up with the total number 48 00:02:27,920 --> 00:02:30,410 of elements within that sequence. 49 00:02:30,410 --> 00:02:33,580 So in this piece of the exercise, we asked you to unpack 50 00:02:33,580 --> 00:02:38,580 a three-element tuple into a set of two variables, 51 00:02:38,920 --> 00:02:40,230 Day and High. 52 00:02:40,230 --> 00:02:43,590 And if I execute that, you can see here that we wind 53 00:02:43,590 --> 00:02:47,540 up with a value error: too many values to unpack. 54 00:02:47,540 --> 00:02:51,540 So Python is going to check the length of the sequence 55 00:02:51,540 --> 00:02:52,680 on the right side, 56 00:02:52,680 --> 00:02:55,640 make sure it matches up with the number of variables 57 00:02:55,640 --> 00:02:58,350 on the left side, and in this case it does not. 58 00:02:58,350 --> 00:03:00,860 There are too few variables on the left side, 59 00:03:00,860 --> 00:03:05,700 and thus, too many values to stuff into those variables. 60 00:03:05,700 --> 00:03:09,580 And therefore, we get an error in this particular case. 61 00:03:09,580 --> 00:03:13,030 In this last, minimize things just the answer 62 00:03:13,030 --> 00:03:15,720 saying that we have to have enough variables 63 00:03:15,720 --> 00:03:18,800 to unpack the corresponding elements. 64 00:03:18,800 --> 00:03:21,780 Now in this second exercise, we wanted you to create a list 65 00:03:21,780 --> 00:03:25,000 of names with three strings and then use a for loop 66 00:03:25,000 --> 00:03:28,690 and the enumerate function to iterate over those values 67 00:03:28,690 --> 00:03:31,690 and display their indices and values. 68 00:03:31,690 --> 00:03:34,040 So here we have a string-- 69 00:03:34,040 --> 00:03:38,010 a list, excuse me, containing three string names. 70 00:03:38,010 --> 00:03:40,200 And let's go ahead and iterate over that 71 00:03:40,200 --> 00:03:41,560 with the enumerate function. 72 00:03:41,560 --> 00:03:45,340 So Names is the list to enumerate over. 73 00:03:45,340 --> 00:03:49,150 We're going to wind up with a sequence of three tuples: 74 00:03:49,150 --> 00:03:53,390 zero in Amanda, one in Sam, two in David. 75 00:03:53,390 --> 00:03:57,200 We're going to unpack those tuples into I for the index 76 00:03:57,200 --> 00:03:59,420 number and Name for the name. 77 00:03:59,420 --> 00:04:04,050 And we'll simply then display those values line by line 78 00:04:04,050 --> 00:04:05,723 after the cell here.