1 00:00:00,510 --> 00:00:02,370 Instructor: All right, it's exercise time. 2 00:00:02,370 --> 00:00:04,140 Let's combine everything that we've learned 3 00:00:04,140 --> 00:00:06,210 into this challenge. 4 00:00:06,210 --> 00:00:08,940 What I want you to do is to create a function 5 00:00:08,940 --> 00:00:11,850 called highest even. 6 00:00:11,850 --> 00:00:16,740 And this function is going to take a list, data type 7 00:00:16,740 --> 00:00:20,280 and this function for now will just pass. 8 00:00:20,280 --> 00:00:22,290 But I want you to fill in this part 9 00:00:22,290 --> 00:00:24,120 with whatever you think is going to 10 00:00:24,120 --> 00:00:25,740 allow us to achieve this. 11 00:00:25,740 --> 00:00:29,040 If I do print, highest even 12 00:00:29,040 --> 00:00:34,040 and I give it a list of let's say, 1, 2, 3, 4, 8. 13 00:00:36,300 --> 00:00:39,990 I want the highest even to be printed in this list. 14 00:00:39,990 --> 00:00:42,150 And actually, let's do 10 here. 15 00:00:42,150 --> 00:00:46,353 So the highest even number in this list is 10. 16 00:00:47,640 --> 00:00:49,413 Just for fun, let's add 11 as well. 17 00:00:50,400 --> 00:00:54,603 So the answer should be printed here as 10. 18 00:00:55,470 --> 00:00:58,170 So based on everything you've learned so far, 19 00:00:58,170 --> 00:01:00,330 try to fill this part so that you 20 00:01:00,330 --> 00:01:03,120 create your own function, highest even. 21 00:01:03,120 --> 00:01:05,610 Pause the video here, give it a try. 22 00:01:05,610 --> 00:01:07,610 Otherwise, I'm gonna go with the answer. 23 00:01:08,640 --> 00:01:11,850 So what I wanna do here, let's think about this, 24 00:01:11,850 --> 00:01:14,970 is I wanna make sure that, first of all, 25 00:01:14,970 --> 00:01:18,933 we only consider the numbers that are even in here. 26 00:01:20,160 --> 00:01:25,160 So maybe what I'll do is create a new list called Evens. 27 00:01:25,710 --> 00:01:29,370 And Evens is going to be an empty list for now 28 00:01:29,370 --> 00:01:34,370 but I'm going to populate this with all of the even numbers. 29 00:01:34,440 --> 00:01:37,833 So I'm going to remove three and 11 from this. 30 00:01:38,880 --> 00:01:40,020 So let's do that. 31 00:01:40,020 --> 00:01:43,203 I'm going to say for item in L I. 32 00:01:44,640 --> 00:01:45,940 If item 33 00:01:47,400 --> 00:01:48,550 modulo two 34 00:01:49,560 --> 00:01:52,180 equals zero, which means it's going to be even 35 00:01:53,250 --> 00:01:55,090 then I'm going to even 36 00:01:57,000 --> 00:01:59,040 append the item. 37 00:01:59,040 --> 00:02:01,623 So I'm gonna add it to this new list. 38 00:02:03,270 --> 00:02:08,270 And then finally in here, once we have the evens list 39 00:02:09,270 --> 00:02:12,840 I wanna find the highest number. 40 00:02:12,840 --> 00:02:17,040 Now you can do this manually and go through it one by one 41 00:02:17,040 --> 00:02:21,330 or Python comes with a nice function called Max. 42 00:02:21,330 --> 00:02:24,540 And as you can see, we give it an iterable 43 00:02:24,540 --> 00:02:27,630 and it'll tell us what the max number is. 44 00:02:27,630 --> 00:02:30,243 In our case, we can just give it evens. 45 00:02:31,380 --> 00:02:34,740 And remember, we wanna return something from this function. 46 00:02:34,740 --> 00:02:37,590 That is what is the highest number? 47 00:02:37,590 --> 00:02:41,970 Well, we wanna return whatever the max of the evens is. 48 00:02:41,970 --> 00:02:43,053 If I run this, 49 00:02:45,150 --> 00:02:46,383 I get 10. 50 00:02:47,310 --> 00:02:49,113 Awesome, looks like it's working. 51 00:02:50,250 --> 00:02:53,790 Now some of you may have gotten tricked and tried to 52 00:02:53,790 --> 00:02:58,690 return inside of the for loop, but if we do this, remember 53 00:02:59,820 --> 00:03:03,930 that this still works, but we actually return 54 00:03:03,930 --> 00:03:07,200 from the function when we hit our first item. 55 00:03:07,200 --> 00:03:08,640 If we had another item, 56 00:03:08,640 --> 00:03:12,783 let's say two in here, and I click run, 57 00:03:13,980 --> 00:03:15,510 we get two. 58 00:03:15,510 --> 00:03:16,950 And why is that? 59 00:03:16,950 --> 00:03:18,960 Because I'm returning. 60 00:03:18,960 --> 00:03:21,450 When we loop the first time through our item 61 00:03:21,450 --> 00:03:22,770 which will be two. 62 00:03:22,770 --> 00:03:25,170 Is two an even number? 63 00:03:25,170 --> 00:03:26,003 Yes it is. 64 00:03:26,003 --> 00:03:28,440 So we're going to append and then we're finally 65 00:03:28,440 --> 00:03:33,440 returning max evens, which only contains the item two. 66 00:03:34,020 --> 00:03:35,400 So you wanna make sure here 67 00:03:35,400 --> 00:03:37,650 that the indentation really matters. 68 00:03:37,650 --> 00:03:41,460 And if you're returning something from inside a loop 69 00:03:41,460 --> 00:03:45,480 you wanna make sure that that's your intended behavior. 70 00:03:45,480 --> 00:03:49,650 You usually wanna loop through something and then return. 71 00:03:49,650 --> 00:03:52,953 So make sure you don't prematurely exit the loop. 72 00:03:53,880 --> 00:03:55,380 Nice and simple. 73 00:03:55,380 --> 00:03:59,250 Remember, your answer may be different than mine. 74 00:03:59,250 --> 00:04:02,760 There are many ways to go about this, but hopefully 75 00:04:02,760 --> 00:04:06,750 by practicing this, you have a better idea of functions. 76 00:04:06,750 --> 00:04:09,240 We now have a great utility function 77 00:04:09,240 --> 00:04:13,263 highest even that we can use all over our program. 78 00:04:14,340 --> 00:04:17,459 Good job getting this far, and I'll see you in the next one. 79 00:04:17,459 --> 00:04:18,293 Bye-bye.