Now, one of the things that makes programming so, so powerful is the ability to
actually invoke
conditional logic. One earth is this all about. Well, remember, we talked about
particular
data types that we could have. We could have, you know, things like strings, we
could have things
like integers or lists, that type of thing. In this case here, what I want to
do is to briefly
talk to you about the data type known as a billion. Now, basically, a billion,
in fact, let me just
write that a little bit clearer. And there we go. A billion is simply a true or WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
write that a little bit clearer. And there we go. A billion is simply a true or
a false statements.
Now, quite simply, the utility of this particular data type is crucial to the
implementation of
conditional logic. Essentially, we want to evaluate if a condition is true. And
if it is true,
then we want the program to do something. However, if that is not true, we can
have the program
then do something else. So imagine this. Let's imagine you'll walk up to a
concert venue and
the person at the door stops you and they ask you, hey, do you have a ticket?
So what you do is you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So what you do is you
reach into your pocket and you hand them over a piece of paper. Now, at this
stage here, the person
on the door is going to run a conditional check. Mentally, what they're going
to assess is that if the
paper that you just gave them is actually a ticket to the actual concert, then
a particular action
is going to happen, then it's going to allow you to pass through. However, if
the paper is not a ticket
to the venue, then something else is going to happen. Then essentially, which
in this case here,
he is essentially going to tell you to go away. So this is a kind of crude WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
he is essentially going to tell you to go away. So this is a kind of crude
example, but it really
is highlighting what we're talking about here. Now, the key words which I'm
using here are actually
very, very important. When we want to invoke conditional logic and Python, we
actually create what
is known as an if block, we say the word if and then define some particular
condition. And then the
Python program evaluates is this condition true. Now, if the condition is true,
whatever we happen
to specify within the if statement is going to actually happen. However, if the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to specify within the if statement is going to actually happen. However, if the
program deems that
the condition is actually false, then we work it down the chain. We can
actually add a another
condition known as LIF. Then we do the same process. We're going to evaluate if
this new condition
is true. And if it's true, then this particular action is going to happen. And
we can just keep adding
in more and more of these conditions, these LIF conditions until we get a true
statement. Now,
if we do not get a true statement, we can essentially allow for some default
behavior by specifying
an else clause. Now, no doubt, if this is the first time you're hearing this, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
an else clause. Now, no doubt, if this is the first time you're hearing this,
your head is probably
swimming. This is obviously very, very confusing. The good thing is is that
when you see this
in action, this is one of those things that suddenly everything becomes much,
much clearer. So let's do
less of the theory and more of the practical stuff. Okay, so let's dive in and
see an action.
So let's go and create another example. Let's call this example 3.py and open
you up. Now, before we
actually begin here, very, very important to note is that within Python, in
order to be able to
evaluate the true statement of a particular condition, we want to be using WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
evaluate the true statement of a particular condition, we want to be using
particular operators.
So if we want to evaluate that something is equal to another thing, we can say
double equal signs,
whereas if we want to invoke not equal logic, we can say exclamation mark and
then an equal sign,
whereas if we want to say if something is greater than a type of mathematical
way, we can use a
right arrow. And if we want to say something is less than we can use the left
arrow, just bear that
in mind as we begin writing our first condition. So let's just have a little
variable here. Let's
just call this device_type. Okay, and what I will do here is I will just define WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just call this device_type. Okay, and what I will do here is I will just define
this the value of
Cisco. Now, I'm going to run some conditional logic. I will say if the device
type that I've just
got, if that variable is equal to, and again, how do we run the comparison? We
use double equal signs.
If that is equal to the value Cisco, which we can clearly see it is right here,
this is a fairly
simple case, but if that is the case, I'm going to have a colon and then if I
hit return,
indented within, this is where we specify what we want to happen. And what I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
indented within, this is where we specify what we want to happen. And what I
will do is I will just
print out the words enable and configure terminal. Now, just to be clear here,
this is not how we
actually interact with the Cisco device. We don't just simply print out these
words, but you can
get an idea, this happens to be Cisco specific syntax. Okay, this is all I'm
representing here in a
simplified manner. Now, let me add another condition and I will say LIF. The
device type is equal to
Juniper. What I will do is print out the Juniper command, which would just
simply be to type the word
edit as opposed to enable and configure terminal. However, for anything else, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
edit as opposed to enable and configure terminal. However, for anything else,
what I'll do is just print,
I'm not sure what command to type. So let me show you what's going to happen
here. We have
defined the variable device type, we're given at the value of Cisco, and then
we run the conditional
check. If the device type is equal to Cisco, then straight away we think is
this condition true or
false. Well, because it is true, what is going to happen is that the indented
block, i.e. this
particular portion, is going to now execute, and then we simply do not continue WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular portion, is going to now execute, and then we simply do not continue
any further, we just
exit the entire block. So let's actually see this then. Let's run the example
three and to enter.
We can see here we're just simply printing out enable and configure terminal
because the device
type is indeed Cisco. However, if I modify the script and I change it to be Jun
iper, save this,
run the exact same script and to enter, now we just type the command edit. Now
the logic here was
pretty much the same. We have our variable called device type, we give it a
value of Juniper,
and then we have our first conditional test. Is the device type equal to the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then we have our first conditional test. Is the device type equal to the
value Cisco,
is this true or false? Well, as we know if it was true, it would execute this
particular portion
of the code, the indented block, but because it happens to be false, all that
has happened is the
indented portion is ignored completely, and we just skip on to the next
condition and we then run
the same test. Is the device type equal to Juniper? Then we run the test, true
or false. In this case
here, this condition is true. So what happens? We execute the indented block,
which is this portion WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
which is this portion
here, and then we don't continue any further. So as a result of this condition
being true,
this indented block happened to executes and nothing else happens. Now again,
if we happen to
modify this one more time, and I just maybe change it to something like saying
knock here, what's
going to happen right here? Well, you can probably guess what's going to happen
if I rerun it, check
the command. It says I'm not sure which command to type. So again, all that
happened here was we
evaluated, was the device type equal to Cisco? That happened to be false. So
this part here was just
ignored. We then skip on down the line to the next condition. Was the device WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
ignored. We then skip on down the line to the next condition. Was the device
type equal to Juniper?
Again, that was also false. So we simply ignore this indented block. And if we
wanted,
we could continually add more LIF statements to add more and more conditions.
However, what we've
got here is an else block, which is a default block. Basically, if we ever
reach this particular
portion, IE all the conditions before happen to be false, then what is going to
happen is that
anything indented within the else block is always going to execute. So WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
anything indented within the else block is always going to execute. So
basically, if we ever make it
this far, IE all the other conditions, the prior conditions happen to be false,
then this one will
always, always run. See that? Now, honestly, this type of conditional logic
using these if blocks,
this is so, so important in the world of network automation. It can do things
like allow us to
determine which command should be sent to which device is going to allow us to
be able to send
alerts to particular system depending on the state of our network. You know, if
our OSPF adjacencies
happen to be in the full state, then you know, send a success message, whereas WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
happen to be in the full state, then you know, send a success message, whereas
if we happen to be in
the downstate, then send an alert, that type of thing, all of this is
controlled, utilizing this
Conditional Logic.