Welcome back. Now, I want to walk you through an exercise that has two tasks.
And on my screen, you can see we have a dictionary of different logs.
You can see the key value pairs there,
the username and then the IP S and then the statuses, right?
So
the two tasks are as follows.
First of all,
we want to identify and print out all the unique IP addresses in the logs.
And then second is to look for a special IP address that might exist.
If it exists, then we're gonna print out something.
If it doesn't exist, we can print out something else.
So you're more than welcome to pause the video and
actually attempt to solve these two tasks on your own.
One hint I will give is that we're definitely going
to have to work with some sort of A for loop
that's gonna go through each log and then especially
look for the value of the IP addresses.
And we're gonna have to pass the results into a set
because remember
that with sets,
we're not going to have any duplicates.
And since we're looking for unique IP addresses, sets would be the ideal way to go.
So you can pause the video
and try it on your own.
If not,
I'm gonna walk you through on how we can solve both tasks. So first of all,
task one, we need to identify all the unique IP addresses.
So one thing we could do right now is we could simply create a set
that's going to store these unique IP addresses. And I'm going to call that set
unique
IP S
OK?
And now what I'm gonna do is because it's a set, I'm gonna open up my curly places,
but now think about it. OK? We want to go through each log
and then look for the value of the IP
key.
So check this out. OK?
I'm gonna say log
and now I'm gonna open up my brackets because we wanna target what
the key, which is
IP.
So essentially we're saying, OK,
for each log,
let's access the IP.
And now I'm gonna introduce my for loop by saying four
log
in
logs.
All right. So what we've done right now is we've created the for loop
that will go through each log
in our logs dictionary.
And we will specifically look for the value of the IP key.
So
since we've passed everything into a set,
all we need to do right now is just to simply print
and then in brackets, we can say something like
uh unique IP
addresses
colon
and then simply add a comma and now unique underscore
ips
and there it is,
I'm pretty sure you might have expected that this will take a few more lines of code.
So, congratulations if you are able to solve this on your own,
regardless of whether or not it was two lines
or three lines or four lines or five lines,
because
there are other ways how we could
solve the task, but this is easily the shortest and the most effective way.
So let me go ahead and run the program right now just to make sure it works.
And there you go. Awesome. So
we have everything in there. 1921681.11 0.51 0.2 0.4
and then 1.3. Awesome. Cool. All right,
next is gonna be task
two.
Ok. So
what are we looking for in here?
We're looking for a special IP that's equal to one and 21681.4. So
we can say special IP equals one and 21681.4.
So
what are we going to do right now?
We'll have to use the if
else statement because we're saying, ok, if
this special IP address was found in this, do this
else, do something else. So
I'm gonna say
if special
underscore IP
in what
in
we in a unique set,
unique underscore I'm sorry, unique
IPs,
rather
unique underscore
ips.
What do we want to do? We wanted to say print
and then we can say,
you know, special
IP
address was found.
Ok.
Oops, sorry about that.
And then we can say
uh
print
and then
special
IP address was not found.
And there you go.
Ok,
let's go ahead, run the program and there it is,
the special IP address was indeed found.
Now, if I change four here to, let's say 10,
just to make sure that it's actually working and I run the program again.
There you go. Special IP address was not found.
So once again, congratulations. If you were able to solve
either tasks or both tasks on your own.
If not, don't worry, it's all part of the learning process,
but I'm going to give you an exercise right
now that you'll have to attempt on your own.
So
I will see you in the next class.