1
00:00:05,300 --> 00:00:10,430
Welcome back, everyone, to our second discussion on control flow, which we'll be discussing if Elif

2
00:00:10,430 --> 00:00:11,090
and else.

3
00:00:12,490 --> 00:00:18,430
So as we begin to learn about control flow, we often only want certain pieces of code to execute when

4
00:00:18,430 --> 00:00:19,990
a particular condition has been met.

5
00:00:20,290 --> 00:00:23,590
For example, if my dog is hungry, which is a condition.

6
00:00:23,950 --> 00:00:30,700
True or false, then I will feed the dog, which is some action so you can think of if elephant statements

7
00:00:30,850 --> 00:00:32,409
as checking for a condition.

8
00:00:32,650 --> 00:00:35,500
And if that condition is true, perform an action.

9
00:00:37,200 --> 00:00:40,560
So to control this flow of logic, we used some keywords.

10
00:00:40,710 --> 00:00:42,570
If Elif and else.

11
00:00:43,980 --> 00:00:49,170
Control flow syntax makes use of colons and indentation, which we haven't actually explored too much

12
00:00:49,170 --> 00:00:50,130
here in Python.

13
00:00:50,460 --> 00:00:56,730
You'll notice that when you enter in your code editor, if it knows it's a Python file, it will sometimes

14
00:00:56,730 --> 00:00:59,490
actually automatically begin to indent your code.

15
00:00:59,850 --> 00:01:05,489
This indentation system of white space is crucial to Python, and it's what sets it apart from other

16
00:01:05,489 --> 00:01:08,460
programming languages and what makes it really readable.

17
00:01:08,640 --> 00:01:11,400
So we'll be discovering this as we talk about control flow.

18
00:01:11,850 --> 00:01:13,560
In fact, let's talk about the syntax now.

19
00:01:14,980 --> 00:01:21,790
So here's a syntax of a simple if statement you are going to say, if some condition, we could imagine

20
00:01:21,790 --> 00:01:23,690
some condition is just a Boolean for right now.

21
00:01:23,710 --> 00:01:27,820
True or false, then colon and then we execute some code.

22
00:01:29,180 --> 00:01:34,730
And if else statement looks like this, we have if and else at the same indentation.

23
00:01:34,850 --> 00:01:42,490
So we say if some condition happens to be true, colon execute some code, then we say else, meaning

24
00:01:42,500 --> 00:01:44,090
if that condition was not true.

25
00:01:44,330 --> 00:01:45,400
Go ahead and do something else.

26
00:01:45,420 --> 00:01:50,570
We can say if the dog is hungry, feed the dog else, don't do anything.

27
00:01:51,890 --> 00:01:56,990
Now we can check for as many conditions as we like before we actually perform else, we can say if we

28
00:01:56,990 --> 00:02:02,450
have some condition, execute some code L, if we can say else, if there's some other condition, do

29
00:02:02,450 --> 00:02:07,340
something different and so on and so on, you can stack as many lives as you want.

30
00:02:07,790 --> 00:02:08,660
So we have.

31
00:02:09,530 --> 00:02:15,170
If statements very, very simple, if else allows you to do something of that condition isn't true.

32
00:02:15,410 --> 00:02:20,690
And if you want to check for multiple conditions before your statements you have, if and else, let's

33
00:02:20,690 --> 00:02:22,610
see some examples of this in our code editor.

34
00:02:23,240 --> 00:02:25,430
OK, here I am inside example that pie.

35
00:02:25,640 --> 00:02:30,290
What I'm going to do is start off with just a simple if statement it's going to be the truth or the

36
00:02:30,290 --> 00:02:31,520
simplest statement possible.

37
00:02:31,550 --> 00:02:37,190
And I say if and notice that there's actually code here to help you out if you click, if it's telling

38
00:02:37,190 --> 00:02:42,050
you, OK, you're starting an if statement, so say if true colon.

39
00:02:42,470 --> 00:02:47,810
And note that once you type out a colon, if you were to hit enter, right now we are automatically

40
00:02:47,810 --> 00:02:53,720
indented, meaning that the code editor understands, Hey, you have a block of code here that's going

41
00:02:53,720 --> 00:02:57,320
to execute when this if statement happens to be true, so going say print.

42
00:02:59,230 --> 00:02:59,710
True.

43
00:03:00,190 --> 00:03:06,250
And I can add as much code here as I want inside this block, so we can say another line again, notice

44
00:03:06,250 --> 00:03:12,070
that if something is true, we're going to print out or execute all the indented code here.

45
00:03:12,820 --> 00:03:13,720
So I'm going to save that.

46
00:03:14,080 --> 00:03:15,160
Let's go ahead and run this.

47
00:03:16,270 --> 00:03:18,250
We can see true in other line.

48
00:03:18,790 --> 00:03:20,710
Let's go ahead and change this to be false.

49
00:03:21,490 --> 00:03:22,960
So if false?

50
00:03:24,700 --> 00:03:26,530
Then if I run this again.

51
00:03:27,930 --> 00:03:32,820
Notice, I don't get back anything, so this is only going to execute if it's actually true or false.

52
00:03:33,150 --> 00:03:35,820
Now typically, you're not going to see the bullion themselves.

53
00:03:35,940 --> 00:03:39,720
Instead, you're going to see some condition to check.

54
00:03:40,380 --> 00:03:42,630
So what kind of conditions can we actually check?

55
00:03:43,500 --> 00:03:44,940
Well, let's do the password.

56
00:03:44,940 --> 00:03:45,480
Check again.

57
00:03:45,510 --> 00:03:47,850
I'm going to say password.

58
00:03:48,950 --> 00:03:50,570
It's just my password.

59
00:03:51,350 --> 00:03:55,460
And let's check our, let's say, our database password or stored a password.

60
00:03:56,960 --> 00:04:04,220
Is equal to my password, so I'm going to say, if we have some condition, I can say the condition

61
00:04:05,000 --> 00:04:08,390
is going to be checking, Hey, is password.

62
00:04:09,390 --> 00:04:10,020
Variable.

63
00:04:11,210 --> 00:04:12,290
Equals to.

64
00:04:13,380 --> 00:04:15,540
Stored password lecturer, we spell that right.

65
00:04:16,410 --> 00:04:16,920
There we go.

66
00:04:17,550 --> 00:04:23,160
No, the actual state of equal science here, I'm saying, equals equal.

67
00:04:23,160 --> 00:04:29,610
So if my stored password is equal, isn't it quality to the password, then this will return a Boolean

68
00:04:29,610 --> 00:04:32,460
that I will then do a variable assignment for some condition.

69
00:04:32,910 --> 00:04:36,510
And then I can print here something like passwords match.

70
00:04:40,060 --> 00:04:40,420
OK.

71
00:04:40,480 --> 00:04:42,220
So again, have these two variables.

72
00:04:42,760 --> 00:04:44,770
I'm checking to see if they're equal to each other.

73
00:04:44,980 --> 00:04:51,730
I'm assigning that bullion to some condition and then I'm printing out if they're true print passwords

74
00:04:51,730 --> 00:04:51,890
match.

75
00:04:51,900 --> 00:04:55,330
So remember if statements only execute if the bullion is true?

76
00:04:56,230 --> 00:04:58,190
So it looks like the passwords do match.

77
00:04:58,210 --> 00:05:03,010
And in fact, I don't technically need to do this extra step of this variable assignment.

78
00:05:03,310 --> 00:05:07,060
Instead, I can just grab this actual comparison operation.

79
00:05:08,080 --> 00:05:10,300
And put it in the if statement itself.

80
00:05:11,330 --> 00:05:13,610
And this is typically how you're going to see these sort of things.

81
00:05:13,670 --> 00:05:20,690
So if password is equal to stored password print passwords, match and now I can run this and I see

82
00:05:20,690 --> 00:05:21,570
passwords match.

83
00:05:21,590 --> 00:05:26,330
If I were to edit one of these so that it matched, so just add some extra code there and then run this

84
00:05:26,330 --> 00:05:30,140
again, you're not going to see any output because this condition was not true.

85
00:05:31,040 --> 00:05:33,750
OK, so what happens if that condition is not true?

86
00:05:33,770 --> 00:05:37,340
I don't actually see anything or report it, if I did want to see something, report it.

87
00:05:37,880 --> 00:05:39,680
I can check with an else statement.

88
00:05:40,130 --> 00:05:43,040
So if else means if this is not true.

89
00:05:43,340 --> 00:05:45,410
Else, go ahead and do something.

90
00:05:45,440 --> 00:05:48,440
Notice how the else doesn't have any sort of condition to be met.

91
00:05:48,680 --> 00:05:54,440
Else only executes if all the if statements are elif statements above it happened to be false.

92
00:05:54,980 --> 00:05:56,210
So then I can prints here.

93
00:05:57,690 --> 00:06:00,450
No password match.

94
00:06:02,170 --> 00:06:02,500
All right.

95
00:06:02,800 --> 00:06:07,990
So again, I'm checking, hey, if the password matches my stored password, I'll print out the passwords

96
00:06:07,990 --> 00:06:13,840
match else, I'll print out no password match and take a close look at the invitation because this is

97
00:06:13,840 --> 00:06:15,970
absolutely key with Python.

98
00:06:16,360 --> 00:06:19,300
Notice how, if and else are linked together.

99
00:06:19,720 --> 00:06:23,440
And this white line here in the Visual Studio code actually kind of tells you that they're linked.

100
00:06:23,800 --> 00:06:27,940
And it also tells you, Oh, by the way, you have these indented blocks of code here.

101
00:06:28,510 --> 00:06:32,320
So now what I'm going to do is I'm going to go ahead and save this.

102
00:06:32,980 --> 00:06:33,880
And let's run this.

103
00:06:34,630 --> 00:06:39,580
The passwords currently do not match, so I should expect the result to say no password match.

104
00:06:40,330 --> 00:06:45,940
OK, now let's say I'm some sort of admin user, so I can say admin.

105
00:06:46,990 --> 00:06:49,090
Is equal to true.

106
00:06:49,750 --> 00:06:55,150
And imagine if I'm an admin, I should be able to maybe override the password condition.

107
00:06:55,750 --> 00:06:58,480
So let's add in yet another check.

108
00:06:58,900 --> 00:07:01,360
So I'm going to say if the passwords match.

109
00:07:01,390 --> 00:07:06,790
Go ahead and print passwords match, then add in another condition to check with Elif.

110
00:07:07,420 --> 00:07:08,590
So this is LCF.

111
00:07:08,680 --> 00:07:11,590
And then I do have to actually pass any condition here.

112
00:07:11,600 --> 00:07:15,520
So then I'll say Elif, and then I can say something like admin.

113
00:07:15,550 --> 00:07:16,630
No admin has actually.

114
00:07:16,630 --> 00:07:17,830
It's going to be bullying itself.

115
00:07:18,310 --> 00:07:19,240
Then I can print.

116
00:07:21,850 --> 00:07:25,060
Passwords did not match, but.

117
00:07:26,490 --> 00:07:29,580
Admin granted or whatever you want to say here.

118
00:07:31,190 --> 00:07:35,660
And now take a look at the logic, I'm going to zoom out just a little bit so we can see more of this

119
00:07:35,660 --> 00:07:36,590
logic code here.

120
00:07:37,990 --> 00:07:38,320
All right.

121
00:07:38,410 --> 00:07:43,840
So I have the password condition check, and then I also have this variable, which is a Boolean admin,

122
00:07:44,080 --> 00:07:49,000
and you're going to see this a lot, this sort of if condition where it's just a single check on some

123
00:07:49,000 --> 00:07:49,810
variable, it's Boolean.

124
00:07:49,810 --> 00:07:54,430
So if admin, which is if true print passwords that match the admin granted.

125
00:07:54,730 --> 00:08:00,370
And I can know that the passwords did not match because if Elephant L statements go in order from top

126
00:08:00,370 --> 00:08:00,730
down.

127
00:08:01,090 --> 00:08:04,030
So it's first going to check to the passwords match if that's not true.

128
00:08:04,360 --> 00:08:05,620
Check if this condition is true.

129
00:08:05,830 --> 00:08:07,420
Is are they an admin true or false?

130
00:08:07,640 --> 00:08:11,320
If that's true, go ahead and print this and we can stop else.

131
00:08:11,590 --> 00:08:14,500
If the passwords didn't match and they weren't an administrator.

132
00:08:14,530 --> 00:08:16,540
Go ahead and print no password match.

133
00:08:16,810 --> 00:08:21,310
And in this case, I can also report back and not in admin.

134
00:08:21,310 --> 00:08:25,690
I know they're not in admin and I know the passwords in a match because if by the time we get to else

135
00:08:25,690 --> 00:08:29,110
these have an executed, then I know those conditions weren't true.

136
00:08:29,560 --> 00:08:32,080
So if I save this, what do I expect the result to be right now?

137
00:08:32,470 --> 00:08:33,809
Well, the passwords don't match.

138
00:08:33,850 --> 00:08:38,770
However, I do have admin set to true, so I should expect this to say passwords that Match had granted.

139
00:08:39,909 --> 00:08:41,950
And there it is passwords of the admin grants.

140
00:08:41,950 --> 00:08:43,480
It lets change admin to false.

141
00:08:45,120 --> 00:08:45,990
Save that change.

142
00:08:47,500 --> 00:08:51,400
And out says password, match or know, password match and not an admin.

143
00:08:51,820 --> 00:08:55,000
And then finally, let's just double check and make the passwords match.

144
00:08:56,140 --> 00:08:58,420
And we should expect the first if statement to execute.

145
00:08:59,400 --> 00:08:59,880
There it is.

146
00:09:00,360 --> 00:09:07,290
Passwords matched something to note here is if I were to actually change this to true, that means this

147
00:09:07,300 --> 00:09:08,760
Elif statement, this admin.

148
00:09:08,760 --> 00:09:09,710
That's also true.

149
00:09:09,720 --> 00:09:15,180
However, if statements that are linked together, they're only going to execute the very first true

150
00:09:15,180 --> 00:09:18,120
condition so they won't go ahead and keep printing out all of these.

151
00:09:18,390 --> 00:09:21,330
That if statement is only linked to the very first true one.

152
00:09:21,750 --> 00:09:27,690
So if I run this, I'm only going to see passwords matched, even though I am an equal to true.

153
00:09:28,020 --> 00:09:31,370
That's just something to keep in mind that it's going in order from top down.

154
00:09:31,380 --> 00:09:35,790
And if you have multiple stacked elif statements like this, so you can keep adding these as many as

155
00:09:35,790 --> 00:09:39,930
you want for checking for different conditions, they can look like this if this is true, that this

156
00:09:39,930 --> 00:09:44,550
else, if this is true, that continue and continue until you get to the very ELSE. with no condition

157
00:09:44,550 --> 00:09:46,080
to check and then do something.

158
00:09:46,800 --> 00:09:47,160
All right.

159
00:09:47,640 --> 00:09:53,280
So that's it for if Elif and else statements, they're going to be really powerful once we're able to

160
00:09:53,280 --> 00:09:56,040
connect them with loops, which is what we're going to discuss next.

