1 00:00:00,009 --> 00:00:05,940 Welcome back. Now, I want to walk you through an exercise that has two tasks. 2 00:00:06,179 --> 00:00:11,020 And on my screen, you can see we have a dictionary of different logs. 3 00:00:11,250 --> 00:00:13,020 You can see the key value pairs there, 4 00:00:13,029 --> 00:00:17,219 the username and then the IP S and then the statuses, right? 5 00:00:17,229 --> 00:00:17,540 So 6 00:00:17,829 --> 00:00:19,209 the two tasks are as follows. 7 00:00:19,219 --> 00:00:19,850 First of all, 8 00:00:19,860 --> 00:00:26,329 we want to identify and print out all the unique IP addresses in the logs. 9 00:00:26,569 --> 00:00:31,729 And then second is to look for a special IP address that might exist. 10 00:00:32,098 --> 00:00:34,689 If it exists, then we're gonna print out something. 11 00:00:34,700 --> 00:00:37,169 If it doesn't exist, we can print out something else. 12 00:00:37,409 --> 00:00:39,509 So you're more than welcome to pause the video and 13 00:00:39,520 --> 00:00:42,750 actually attempt to solve these two tasks on your own. 14 00:00:42,919 --> 00:00:46,270 One hint I will give is that we're definitely going 15 00:00:46,279 --> 00:00:49,270 to have to work with some sort of A for loop 16 00:00:49,830 --> 00:00:53,430 that's gonna go through each log and then especially 17 00:00:53,569 --> 00:00:56,459 look for the value of the IP addresses. 18 00:00:57,220 --> 00:01:01,830 And we're gonna have to pass the results into a set 19 00:01:01,990 --> 00:01:03,360 because remember 20 00:01:03,540 --> 00:01:04,638 that with sets, 21 00:01:05,120 --> 00:01:07,569 we're not going to have any duplicates. 22 00:01:07,580 --> 00:01:12,629 And since we're looking for unique IP addresses, sets would be the ideal way to go. 23 00:01:12,639 --> 00:01:14,160 So you can pause the video 24 00:01:14,349 --> 00:01:15,720 and try it on your own. 25 00:01:16,010 --> 00:01:16,769 If not, 26 00:01:17,000 --> 00:01:20,510 I'm gonna walk you through on how we can solve both tasks. So first of all, 27 00:01:21,029 --> 00:01:26,019 task one, we need to identify all the unique IP addresses. 28 00:01:26,029 --> 00:01:31,330 So one thing we could do right now is we could simply create a set 29 00:01:31,989 --> 00:01:36,819 that's going to store these unique IP addresses. And I'm going to call that set 30 00:01:37,319 --> 00:01:38,849 unique 31 00:01:39,150 --> 00:01:39,900 IP S 32 00:01:40,540 --> 00:01:41,209 OK? 33 00:01:41,959 --> 00:01:48,099 And now what I'm gonna do is because it's a set, I'm gonna open up my curly places, 34 00:01:48,580 --> 00:01:52,959 but now think about it. OK? We want to go through each log 35 00:01:53,410 --> 00:01:58,199 and then look for the value of the IP 36 00:01:58,400 --> 00:01:59,220 key. 37 00:01:59,580 --> 00:02:01,419 So check this out. OK? 38 00:02:01,900 --> 00:02:03,580 I'm gonna say log 39 00:02:04,660 --> 00:02:08,669 and now I'm gonna open up my brackets because we wanna target what 40 00:02:08,839 --> 00:02:11,000 the key, which is 41 00:02:11,179 --> 00:02:12,139 IP. 42 00:02:12,539 --> 00:02:14,750 So essentially we're saying, OK, 43 00:02:15,580 --> 00:02:17,210 for each log, 44 00:02:17,389 --> 00:02:19,690 let's access the IP. 45 00:02:20,000 --> 00:02:24,330 And now I'm gonna introduce my for loop by saying four 46 00:02:24,740 --> 00:02:26,009 log 47 00:02:26,639 --> 00:02:27,250 in 48 00:02:27,520 --> 00:02:28,759 logs. 49 00:02:29,449 --> 00:02:33,850 All right. So what we've done right now is we've created the for loop 50 00:02:33,990 --> 00:02:36,350 that will go through each log 51 00:02:36,710 --> 00:02:38,600 in our logs dictionary. 52 00:02:38,759 --> 00:02:43,830 And we will specifically look for the value of the IP key. 53 00:02:44,190 --> 00:02:44,869 So 54 00:02:45,380 --> 00:02:48,279 since we've passed everything into a set, 55 00:02:48,539 --> 00:02:52,110 all we need to do right now is just to simply print 56 00:02:52,449 --> 00:02:55,110 and then in brackets, we can say something like 57 00:02:55,750 --> 00:02:58,050 uh unique IP 58 00:02:58,339 --> 00:03:00,130 addresses 59 00:03:01,020 --> 00:03:01,929 colon 60 00:03:02,199 --> 00:03:06,279 and then simply add a comma and now unique underscore 61 00:03:07,110 --> 00:03:07,160 ips 62 00:03:07,880 --> 00:03:08,889 and there it is, 63 00:03:09,199 --> 00:03:15,009 I'm pretty sure you might have expected that this will take a few more lines of code. 64 00:03:15,350 --> 00:03:18,589 So, congratulations if you are able to solve this on your own, 65 00:03:18,600 --> 00:03:20,539 regardless of whether or not it was two lines 66 00:03:20,550 --> 00:03:21,869 or three lines or four lines or five lines, 67 00:03:21,880 --> 00:03:22,300 because 68 00:03:22,490 --> 00:03:24,500 there are other ways how we could 69 00:03:24,779 --> 00:03:28,410 solve the task, but this is easily the shortest and the most effective way. 70 00:03:28,860 --> 00:03:32,660 So let me go ahead and run the program right now just to make sure it works. 71 00:03:32,669 --> 00:03:34,440 And there you go. Awesome. So 72 00:03:34,600 --> 00:03:39,820 we have everything in there. 1921681.11 0.51 0.2 0.4 73 00:03:39,940 --> 00:03:43,259 and then 1.3. Awesome. Cool. All right, 74 00:03:43,710 --> 00:03:46,729 next is gonna be task 75 00:03:46,880 --> 00:03:47,639 two. 76 00:03:47,850 --> 00:03:48,839 Ok. So 77 00:03:49,490 --> 00:03:51,589 what are we looking for in here? 78 00:03:51,850 --> 00:03:56,869 We're looking for a special IP that's equal to one and 21681.4. So 79 00:03:57,350 --> 00:04:04,389 we can say special IP equals one and 21681.4. 80 00:04:04,809 --> 00:04:05,190 So 81 00:04:05,990 --> 00:04:08,210 what are we going to do right now? 82 00:04:08,610 --> 00:04:10,179 We'll have to use the if 83 00:04:10,559 --> 00:04:14,029 else statement because we're saying, ok, if 84 00:04:14,320 --> 00:04:18,450 this special IP address was found in this, do this 85 00:04:18,890 --> 00:04:21,329 else, do something else. So 86 00:04:21,839 --> 00:04:22,769 I'm gonna say 87 00:04:23,459 --> 00:04:25,559 if special 88 00:04:25,709 --> 00:04:27,359 underscore IP 89 00:04:27,959 --> 00:04:28,579 in what 90 00:04:29,070 --> 00:04:29,399 in 91 00:04:30,529 --> 00:04:33,690 we in a unique set, 92 00:04:34,809 --> 00:04:36,760 unique underscore I'm sorry, unique 93 00:04:36,940 --> 00:04:37,160 IPs, 94 00:04:37,279 --> 00:04:37,359 rather 95 00:04:38,690 --> 00:04:39,630 unique underscore 96 00:04:39,739 --> 00:04:39,929 ips. 97 00:04:40,910 --> 00:04:43,750 What do we want to do? We wanted to say print 98 00:04:44,540 --> 00:04:46,380 and then we can say, 99 00:04:46,709 --> 00:04:48,279 you know, special 100 00:04:48,649 --> 00:04:49,679 IP 101 00:04:51,079 --> 00:04:53,059 address was found. 102 00:04:53,179 --> 00:04:53,799 Ok. 103 00:04:55,589 --> 00:04:57,279 Oops, sorry about that. 104 00:04:58,570 --> 00:04:59,850 And then we can say 105 00:05:00,809 --> 00:05:01,700 uh 106 00:05:03,579 --> 00:05:04,450 print 107 00:05:05,859 --> 00:05:06,750 and then 108 00:05:06,890 --> 00:05:08,320 special 109 00:05:08,679 --> 00:05:11,320 IP address was not found. 110 00:05:12,940 --> 00:05:15,119 And there you go. 111 00:05:16,000 --> 00:05:16,940 Ok, 112 00:05:17,709 --> 00:05:21,529 let's go ahead, run the program and there it is, 113 00:05:21,540 --> 00:05:23,950 the special IP address was indeed found. 114 00:05:24,309 --> 00:05:27,209 Now, if I change four here to, let's say 10, 115 00:05:28,140 --> 00:05:31,000 just to make sure that it's actually working and I run the program again. 116 00:05:31,279 --> 00:05:34,679 There you go. Special IP address was not found. 117 00:05:35,130 --> 00:05:39,019 So once again, congratulations. If you were able to solve 118 00:05:39,720 --> 00:05:41,869 either tasks or both tasks on your own. 119 00:05:41,940 --> 00:05:45,010 If not, don't worry, it's all part of the learning process, 120 00:05:45,140 --> 00:05:47,510 but I'm going to give you an exercise right 121 00:05:47,519 --> 00:05:49,880 now that you'll have to attempt on your own. 122 00:05:49,890 --> 00:05:50,130 So 123 00:05:50,450 --> 00:05:52,559 I will see you in the next class.