WEBVTT

1
00:00.000 --> 00:04.860
Now, one of the things that makes programming so, so powerful is the ability to

2
00:04.860 --> 00:05.680
actually invoke

3
00:05.680 --> 00:10.320
conditional logic. One earth is this all about. Well, remember, we talked about

4
00:10.320 --> 00:10.880
particular

5
00:10.880 --> 00:13.820
data types that we could have. We could have, you know, things like strings, we

6
00:13.820 --> 00:14.400
could have things

7
00:14.400 --> 00:18.670
like integers or lists, that type of thing. In this case here, what I want to

8
00:18.670 --> 00:19.520
do is to briefly

9
00:19.520 --> 00:24.000
talk to you about the data type known as a billion. Now, basically, a billion,

10
00:24.000 --> 00:24.800
in fact, let me just

11
00:24.800 --> 00:30.320
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

11
00:24.800 --> 00:30.320
write that a little bit clearer. And there we go. A billion is simply a true or

12
00:30.320 --> 00:31.680
a false statements.

13
00:31.680 --> 00:37.040
Now, quite simply, the utility of this particular data type is crucial to the

14
00:37.040 --> 00:38.160
implementation of

15
00:38.160 --> 00:43.750
conditional logic. Essentially, we want to evaluate if a condition is true. And

16
00:43.750 --> 00:45.040
if it is true,

17
00:45.040 --> 00:49.290
then we want the program to do something. However, if that is not true, we can

18
00:49.290 --> 00:50.320
have the program

19
00:50.320 --> 00:54.800
then do something else. So imagine this. Let's imagine you'll walk up to a

20
00:54.800 --> 00:56.240
concert venue and

21
00:56.240 --> 00:59.920
the person at the door stops you and they ask you, hey, do you have a ticket?

22
00:59.920 --> 01:00.880
So what you do is you
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

22
00:59.920 --> 01:00.880
So what you do is you

23
01:00.880 --> 01:05.340
reach into your pocket and you hand them over a piece of paper. Now, at this

24
01:05.340 --> 01:06.640
stage here, the person

25
01:06.640 --> 01:10.440
on the door is going to run a conditional check. Mentally, what they're going

26
01:10.440 --> 01:12.080
to assess is that if the

27
01:12.080 --> 01:17.290
paper that you just gave them is actually a ticket to the actual concert, then

28
01:17.290 --> 01:18.480
a particular action

29
01:18.480 --> 01:21.880
is going to happen, then it's going to allow you to pass through. However, if

30
01:21.880 --> 01:23.520
the paper is not a ticket

31
01:23.520 --> 01:28.070
to the venue, then something else is going to happen. Then essentially, which

32
01:28.070 --> 01:28.960
in this case here,

33
01:28.960 --> 01:32.390
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

33
01:28.960 --> 01:32.390
he is essentially going to tell you to go away. So this is a kind of crude

34
01:32.390 --> 01:33.360
example, but it really

35
01:33.360 --> 01:36.480
is highlighting what we're talking about here. Now, the key words which I'm

36
01:36.480 --> 01:37.520
using here are actually

37
01:37.520 --> 01:42.030
very, very important. When we want to invoke conditional logic and Python, we

38
01:42.030 --> 01:42.800
actually create what

39
01:42.800 --> 01:47.840
is known as an if block, we say the word if and then define some particular

40
01:47.840 --> 01:49.360
condition. And then the

41
01:49.360 --> 01:53.840
Python program evaluates is this condition true. Now, if the condition is true,

42
01:53.840 --> 01:54.560
whatever we happen

43
01:54.560 --> 02:00.440
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

43
01:54.560 --> 02:00.440
to specify within the if statement is going to actually happen. However, if the

44
02:00.440 --> 02:01.600
program deems that

45
02:01.600 --> 02:05.400
the condition is actually false, then we work it down the chain. We can

46
02:05.400 --> 02:06.480
actually add a another

47
02:06.480 --> 02:10.430
condition known as LIF. Then we do the same process. We're going to evaluate if

48
02:10.430 --> 02:11.440
this new condition

49
02:11.440 --> 02:15.690
is true. And if it's true, then this particular action is going to happen. And

50
02:15.690 --> 02:16.640
we can just keep adding

51
02:16.640 --> 02:20.770
in more and more of these conditions, these LIF conditions until we get a true

52
02:20.770 --> 02:21.760
statement. Now,

53
02:21.760 --> 02:25.580
if we do not get a true statement, we can essentially allow for some default

54
02:25.580 --> 02:27.040
behavior by specifying

55
02:27.040 --> 02:30.800
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

55
02:27.040 --> 02:30.800
an else clause. Now, no doubt, if this is the first time you're hearing this,

56
02:30.800 --> 02:32.000
your head is probably

57
02:32.000 --> 02:35.460
swimming. This is obviously very, very confusing. The good thing is is that

58
02:35.460 --> 02:36.240
when you see this

59
02:36.240 --> 02:40.090
in action, this is one of those things that suddenly everything becomes much,

60
02:40.090 --> 02:41.600
much clearer. So let's do

61
02:41.600 --> 02:45.870
less of the theory and more of the practical stuff. Okay, so let's dive in and

62
02:45.870 --> 02:46.720
see an action.

63
02:46.720 --> 02:51.840
So let's go and create another example. Let's call this example 3.py and open

64
02:51.840 --> 02:52.800
you up. Now, before we

65
02:52.800 --> 02:56.220
actually begin here, very, very important to note is that within Python, in

66
02:56.220 --> 02:57.120
order to be able to

67
02:57.120 --> 03:01.330
evaluate the true statement of a particular condition, we want to be using
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:57.120 --> 03:01.330
evaluate the true statement of a particular condition, we want to be using

68
03:01.330 --> 03:03.120
particular operators.

69
03:03.120 --> 03:09.020
So if we want to evaluate that something is equal to another thing, we can say

70
03:09.020 --> 03:10.080
double equal signs,

71
03:10.080 --> 03:13.860
whereas if we want to invoke not equal logic, we can say exclamation mark and

72
03:13.860 --> 03:15.040
then an equal sign,

73
03:15.040 --> 03:19.440
whereas if we want to say if something is greater than a type of mathematical

74
03:19.440 --> 03:20.720
way, we can use a

75
03:20.720 --> 03:24.770
right arrow. And if we want to say something is less than we can use the left

76
03:24.770 --> 03:25.760
arrow, just bear that

77
03:25.760 --> 03:29.050
in mind as we begin writing our first condition. So let's just have a little

78
03:29.050 --> 03:29.920
variable here. Let's

79
03:29.920 --> 03:34.720
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

79
03:29.920 --> 03:34.720
just call this device_type. Okay, and what I will do here is I will just define

80
03:34.720 --> 03:35.760
this the value of

81
03:35.760 --> 03:40.920
Cisco. Now, I'm going to run some conditional logic. I will say if the device

82
03:40.920 --> 03:42.000
type that I've just

83
03:42.000 --> 03:46.860
got, if that variable is equal to, and again, how do we run the comparison? We

84
03:46.860 --> 03:48.480
use double equal signs.

85
03:48.480 --> 03:51.920
If that is equal to the value Cisco, which we can clearly see it is right here,

86
03:51.920 --> 03:52.480
this is a fairly

87
03:52.480 --> 03:56.490
simple case, but if that is the case, I'm going to have a colon and then if I

88
03:56.490 --> 03:57.200
hit return,

89
03:57.200 --> 04:01.210
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

89
03:57.200 --> 04:01.210
indented within, this is where we specify what we want to happen. And what I

90
04:01.210 --> 04:02.080
will do is I will just

91
04:02.080 --> 04:06.480
print out the words enable and configure terminal. Now, just to be clear here,

92
04:06.480 --> 04:07.200
this is not how we

93
04:07.200 --> 04:10.590
actually interact with the Cisco device. We don't just simply print out these

94
04:10.590 --> 04:11.440
words, but you can

95
04:11.440 --> 04:15.780
get an idea, this happens to be Cisco specific syntax. Okay, this is all I'm

96
04:15.780 --> 04:17.040
representing here in a

97
04:17.040 --> 04:21.450
simplified manner. Now, let me add another condition and I will say LIF. The

98
04:21.450 --> 04:22.880
device type is equal to

99
04:23.440 --> 04:27.300
Juniper. What I will do is print out the Juniper command, which would just

100
04:27.300 --> 04:28.640
simply be to type the word

101
04:28.640 --> 04:33.410
edit as opposed to enable and configure terminal. However, for anything else,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

101
04:28.640 --> 04:33.410
edit as opposed to enable and configure terminal. However, for anything else,

102
04:33.410 --> 04:35.280
what I'll do is just print,

103
04:35.280 --> 04:39.540
I'm not sure what command to type. So let me show you what's going to happen

104
04:39.540 --> 04:40.800
here. We have

105
04:40.800 --> 04:45.110
defined the variable device type, we're given at the value of Cisco, and then

106
04:45.110 --> 04:45.920
we run the conditional

107
04:45.920 --> 04:49.850
check. If the device type is equal to Cisco, then straight away we think is

108
04:49.850 --> 04:51.040
this condition true or

109
04:51.040 --> 04:55.420
false. Well, because it is true, what is going to happen is that the indented

110
04:55.420 --> 04:56.640
block, i.e. this

111
04:56.640 --> 05:01.230
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

111
04:56.640 --> 05:01.230
particular portion, is going to now execute, and then we simply do not continue

112
05:01.230 --> 05:02.560
any further, we just

113
05:02.560 --> 05:05.980
exit the entire block. So let's actually see this then. Let's run the example

114
05:05.980 --> 05:07.200
three and to enter.

115
05:07.200 --> 05:10.880
We can see here we're just simply printing out enable and configure terminal

116
05:10.880 --> 05:11.760
because the device

117
05:11.760 --> 05:16.760
type is indeed Cisco. However, if I modify the script and I change it to be Jun

118
05:16.760 --> 05:17.920
iper, save this,

119
05:18.480 --> 05:23.060
run the exact same script and to enter, now we just type the command edit. Now

120
05:23.060 --> 05:24.160
the logic here was

121
05:24.160 --> 05:28.010
pretty much the same. We have our variable called device type, we give it a

122
05:28.010 --> 05:28.960
value of Juniper,

123
05:28.960 --> 05:32.880
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

123
05:28.960 --> 05:32.880
and then we have our first conditional test. Is the device type equal to the

124
05:32.880 --> 05:33.760
value Cisco,

125
05:33.760 --> 05:37.840
is this true or false? Well, as we know if it was true, it would execute this

126
05:37.840 --> 05:38.560
particular portion

127
05:38.560 --> 05:42.990
of the code, the indented block, but because it happens to be false, all that

128
05:42.990 --> 05:44.080
has happened is the

129
05:44.080 --> 05:47.830
indented portion is ignored completely, and we just skip on to the next

130
05:47.830 --> 05:49.360
condition and we then run

131
05:49.360 --> 05:53.600
the same test. Is the device type equal to Juniper? Then we run the test, true

132
05:53.600 --> 05:54.880
or false. In this case

133
05:54.880 --> 05:59.660
here, this condition is true. So what happens? We execute the indented block,

134
05:59.660 --> 06:00.720
which is this portion
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

134
05:59.660 --> 06:00.720
which is this portion

135
06:00.720 --> 06:04.780
here, and then we don't continue any further. So as a result of this condition

136
06:04.780 --> 06:05.520
being true,

137
06:05.520 --> 06:09.840
this indented block happened to executes and nothing else happens. Now again,

138
06:09.840 --> 06:10.400
if we happen to

139
06:10.400 --> 06:14.470
modify this one more time, and I just maybe change it to something like saying

140
06:14.470 --> 06:15.280
knock here, what's

141
06:15.280 --> 06:17.800
going to happen right here? Well, you can probably guess what's going to happen

142
06:17.800 --> 06:18.880
if I rerun it, check

143
06:18.880 --> 06:22.920
the command. It says I'm not sure which command to type. So again, all that

144
06:22.920 --> 06:24.080
happened here was we

145
06:24.080 --> 06:28.030
evaluated, was the device type equal to Cisco? That happened to be false. So

146
06:28.030 --> 06:29.280
this part here was just

147
06:29.280 --> 06:33.530
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

147
06:29.280 --> 06:33.530
ignored. We then skip on down the line to the next condition. Was the device

148
06:33.530 --> 06:34.880
type equal to Juniper?

149
06:34.880 --> 06:39.230
Again, that was also false. So we simply ignore this indented block. And if we

150
06:39.230 --> 06:39.760
wanted,

151
06:39.760 --> 06:44.200
we could continually add more LIF statements to add more and more conditions.

152
06:44.200 --> 06:45.280
However, what we've

153
06:45.280 --> 06:49.880
got here is an else block, which is a default block. Basically, if we ever

154
06:49.880 --> 06:51.120
reach this particular

155
06:51.120 --> 06:55.440
portion, IE all the conditions before happen to be false, then what is going to

156
06:55.440 --> 06:56.240
happen is that

157
06:56.240 --> 07:00.300
anything indented within the else block is always going to execute. So
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

157
06:56.240 --> 07:00.300
anything indented within the else block is always going to execute. So

158
07:00.300 --> 07:01.440
basically, if we ever make it

159
07:01.440 --> 07:05.640
this far, IE all the other conditions, the prior conditions happen to be false,

160
07:05.640 --> 07:06.400
then this one will

161
07:06.400 --> 07:10.300
always, always run. See that? Now, honestly, this type of conditional logic

162
07:10.300 --> 07:11.760
using these if blocks,

163
07:11.760 --> 07:16.360
this is so, so important in the world of network automation. It can do things

164
07:16.360 --> 07:17.120
like allow us to

165
07:17.120 --> 07:20.420
determine which command should be sent to which device is going to allow us to

166
07:20.420 --> 07:21.760
be able to send

167
07:21.760 --> 07:26.010
alerts to particular system depending on the state of our network. You know, if

168
07:26.010 --> 07:27.120
our OSPF adjacencies

169
07:27.120 --> 07:31.600
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

169
07:27.120 --> 07:31.600
happen to be in the full state, then you know, send a success message, whereas

170
07:31.600 --> 07:32.880
if we happen to be in

171
07:32.880 --> 07:36.580
the downstate, then send an alert, that type of thing, all of this is

172
07:36.580 --> 07:38.240
controlled, utilizing this

173
07:38.240 --> 07:39.400
Conditional Logic.
