1 00:00:00,920 --> 00:00:02,410 - [Instructor] In this Self Check exercise 2 00:00:02,410 --> 00:00:04,360 we'd like you to use some of the techniques 3 00:00:04,360 --> 00:00:07,080 that you learned in the preceding video. 4 00:00:07,080 --> 00:00:11,210 First we'd like you create a function called cube_list, 5 00:00:11,210 --> 00:00:13,800 which is going to walk through the list 6 00:00:13,800 --> 00:00:15,670 that it receives as a parameter, 7 00:00:15,670 --> 00:00:17,300 and for each item in the list 8 00:00:17,300 --> 00:00:21,550 is going to raise that item to the third power 9 00:00:21,550 --> 00:00:25,860 and store the result back into that element of the list. 10 00:00:25,860 --> 00:00:28,250 And then we want you to show what the contents 11 00:00:28,250 --> 00:00:30,410 of the numbers array that you pass 12 00:00:30,410 --> 00:00:33,800 into the function is after the function call. 13 00:00:33,800 --> 00:00:37,690 Separately we'd also like you to use an empty list, 14 00:00:37,690 --> 00:00:39,670 we said to call it characters, 15 00:00:39,670 --> 00:00:42,410 and the += operator to show 16 00:00:42,410 --> 00:00:45,580 what gets placed into that list 17 00:00:45,580 --> 00:00:48,150 when you append the string Birthday 18 00:00:49,130 --> 00:00:51,370 to the end of the list characters. 19 00:00:51,370 --> 00:00:52,950 So go ahead and pause the video 20 00:00:52,950 --> 00:00:55,170 and give those two exercises a shot 21 00:00:55,170 --> 00:00:57,620 and when you come back I'll show you the answers. 22 00:01:02,120 --> 00:01:04,960 Okay, let's go and reveal the definition 23 00:01:04,960 --> 00:01:07,440 of the cube_list function. 24 00:01:07,440 --> 00:01:10,450 So here we have a cube_list function that receives 25 00:01:10,450 --> 00:01:12,670 a parameter named values. 26 00:01:12,670 --> 00:01:15,610 By the way, for those of you who are new 27 00:01:15,610 --> 00:01:17,820 to the Python programming language, 28 00:01:17,820 --> 00:01:22,030 coming from languages with required type information 29 00:01:22,030 --> 00:01:25,923 like Java and C and C++ and C#, again 30 00:01:25,923 --> 00:01:28,810 there's nothing here that actually says 31 00:01:28,810 --> 00:01:30,780 this has to be a list. 32 00:01:30,780 --> 00:01:34,370 Now if you don't give it a list or a sequence of values 33 00:01:34,370 --> 00:01:37,270 that this for loop can iterate through, 34 00:01:37,270 --> 00:01:39,720 that's going to result in error messages 35 00:01:39,720 --> 00:01:42,500 at run time and similarly, if you don't give it 36 00:01:42,500 --> 00:01:47,250 something that's compatible with the exponentiation operator 37 00:01:47,250 --> 00:01:51,330 that too could result in problems at run time. 38 00:01:51,330 --> 00:01:55,130 But these problems that are possible are also 39 00:01:55,130 --> 00:01:57,090 as a result of the flexibility 40 00:01:57,090 --> 00:01:58,990 of the Python language itself. 41 00:01:58,990 --> 00:02:02,110 So I can pass anything I want into this function 42 00:02:02,110 --> 00:02:04,420 and as long as it's something that's compatible 43 00:02:04,420 --> 00:02:06,890 with what I'm doing in this for loop, 44 00:02:06,890 --> 00:02:10,970 then that item will work with my cube_list function. 45 00:02:10,970 --> 00:02:12,820 So let's go ahead and define the function. 46 00:02:12,820 --> 00:02:14,520 We're simply going to iterate through 47 00:02:14,520 --> 00:02:19,140 each of the elements in the list called values 48 00:02:19,140 --> 00:02:21,530 that we receive, or the sequence called values, 49 00:02:21,530 --> 00:02:24,050 and for every item in that list, 50 00:02:24,050 --> 00:02:26,220 we are going to modify the item 51 00:02:26,220 --> 00:02:28,890 by raising it to the third power. 52 00:02:28,890 --> 00:02:31,970 So let's then create our numbers list 53 00:02:31,970 --> 00:02:34,000 that contains the values from one through 10, 54 00:02:34,000 --> 00:02:36,200 as specified in the problem statement, 55 00:02:36,200 --> 00:02:39,870 and let's call cube_list with those values. 56 00:02:39,870 --> 00:02:42,940 Now if this worked correctly, numbers should contain 57 00:02:42,940 --> 00:02:45,070 the cubes of these values, so we'll have 58 00:02:45,070 --> 00:02:48,310 one up to 1,000 in the results, 59 00:02:48,310 --> 00:02:51,900 and indeed we now have the cubed values. 60 00:02:51,900 --> 00:02:53,810 Let's take a look at the second exercise 61 00:02:53,810 --> 00:02:56,680 where we simply wanted to take the string Birthday 62 00:02:56,680 --> 00:02:59,640 and concatenate its elements to a list. 63 00:02:59,640 --> 00:03:02,680 So we said to start by creating an empty list 64 00:03:02,680 --> 00:03:06,240 called characters, then we want to do 65 00:03:06,240 --> 00:03:08,790 the append operation using the +=, 66 00:03:08,790 --> 00:03:11,440 and again if the left side is a list 67 00:03:11,440 --> 00:03:13,810 the right side must be some sort 68 00:03:13,810 --> 00:03:16,380 of iterable sequence of elements. 69 00:03:16,380 --> 00:03:19,160 So far the types of iterable sequences 70 00:03:19,160 --> 00:03:23,070 we've looked at are strings, tuples and lists, 71 00:03:23,070 --> 00:03:26,020 and in this case we have string, so whoops! 72 00:03:26,020 --> 00:03:28,210 So let's execute that, and finally 73 00:03:28,210 --> 00:03:30,490 let's evaluate characters so that we can see 74 00:03:30,490 --> 00:03:34,110 the individual characters were separated out 75 00:03:34,110 --> 00:03:36,990 into separate strings, one at a time, 76 00:03:36,990 --> 00:03:40,193 each of those was appended to the empty list.