1
00:00:05,270 --> 00:00:11,480
Welcome back everyone in this lecture we're going to discuss if elif and L statements let's begin to

2
00:00:11,480 --> 00:00:13,350
learn more about control flow.

3
00:00:13,400 --> 00:00:17,570
We often only want certain code to execute on a particular condition has been met.

4
00:00:17,570 --> 00:00:23,630
For example if my dog is hungry that's some condition then I will feed the dog perform some action or

5
00:00:23,630 --> 00:00:24,690
some line of code.

6
00:00:25,790 --> 00:00:32,670
So to control the flow of logic we're going to be able to some keywords if elif and else control flow

7
00:00:32,670 --> 00:00:38,820
syntax makes use of colons and in the notation otherwise known as whitespace and this indentation system

8
00:00:38,820 --> 00:00:44,030
is crucial to Python and it's what really sets it apart from other programming languages.

9
00:00:44,050 --> 00:00:49,120
So let's go through the examples of what the actual syntax of an IF statement an elephant statement

10
00:00:49,120 --> 00:00:50,340
would look like.

11
00:00:50,380 --> 00:00:54,580
So let's imagine we want to check some condition before executing a block of code.

12
00:00:54,580 --> 00:00:58,930
What we do is we say if and then we have some sort of condition and eventually that's going to be a

13
00:00:58,930 --> 00:00:59,650
boolean value.

14
00:00:59,650 --> 00:01:06,310
So if true if false for example could say if one is less than two or if a user names equal equal to

15
00:01:06,310 --> 00:01:09,240
admin and then we perform or execute some code.

16
00:01:09,250 --> 00:01:13,400
So notice here we have a colon and then some tab indented spaces.

17
00:01:13,480 --> 00:01:19,380
So that's the block code will be executed when we have that condition to be true.

18
00:01:19,390 --> 00:01:21,870
Now we can also have an if else statement.

19
00:01:21,940 --> 00:01:28,260
Basically if some condition is true we execute some code else we execute and do something else.

20
00:01:28,300 --> 00:01:34,050
So let us hear if statements they're going to have some conditional check colon and then some indication

21
00:01:34,090 --> 00:01:38,710
and a block of code else isn't going to have a condition related to it because the else is going to

22
00:01:38,710 --> 00:01:43,650
execute only when everything above it hasn't been true or the condition hasn't been met.

23
00:01:44,890 --> 00:01:49,570
Now if you want to check for multiple conditions then you can say if some condition colon executes some

24
00:01:49,570 --> 00:01:55,320
code Elif basically checking for some other condition do something different and you can stack as many

25
00:01:55,340 --> 00:01:56,470
Ls as you want.

26
00:01:56,560 --> 00:02:00,850
And at the end if none of those conditions are true you can have an else to do something else.

27
00:02:00,850 --> 00:02:07,530
At the very end OK let's explore these concepts in more detail by helping over to the Adam text editor.

28
00:02:07,540 --> 00:02:09,730
OK here I am at the Adam text editor.

29
00:02:09,790 --> 00:02:11,890
I'm going to start with just a simple IF statement.

30
00:02:12,070 --> 00:02:19,610
I will say if one is less than two colon and when to hit enter as long as you've already said this as

31
00:02:19,640 --> 00:02:22,660
the PI file you should see an automatic in the notation.

32
00:02:22,690 --> 00:02:27,480
If not you can always just tab over or have four spaces and then that's the indentation.

33
00:02:27,490 --> 00:02:33,410
So this is how Python is going to differentiate blocks of code from each other it's all based on indentation.

34
00:02:33,460 --> 00:02:39,440
So I say if one is Leston to go ahead and print's Yep.

35
00:02:39,810 --> 00:02:40,810
So that's all I'm going to do.

36
00:02:40,810 --> 00:02:43,030
Just a simple IF statement let's run this code.

37
00:02:44,660 --> 00:02:45,350
And I see.

38
00:02:45,410 --> 00:02:46,110
Yep.

39
00:02:46,150 --> 00:02:50,000
Now let's see what happens when I switch this to some statement that I know isn't true.

40
00:02:50,000 --> 00:02:53,330
So I'll say if one equals equals to print Yes.

41
00:02:54,590 --> 00:02:59,090
And now I don't see anything printed out which makes sense because one is not equal to two.

42
00:02:59,390 --> 00:03:04,380
So a more realistic example of something we would use this for is maybe that user name check again.

43
00:03:04,380 --> 00:03:12,730
So say user name provided was admen and then we'll have some check equal to admen.

44
00:03:12,990 --> 00:03:24,800
And then I can say if the user name is equal to the check then I can Prince access granted.

45
00:03:24,970 --> 00:03:29,470
So this kind of the examples we're going to be doing throughout the course this sort of boolean checks

46
00:03:29,590 --> 00:03:32,230
a member we can always add in logical operators as well.

47
00:03:32,230 --> 00:03:39,350
So I can say if user name is equal to check and let's say one is equal to 1.

48
00:03:39,620 --> 00:03:41,630
So then we should see Access Granted there.

49
00:03:41,780 --> 00:03:43,460
OK that's a simple IF statement.

50
00:03:43,610 --> 00:03:48,170
Let's imagine that we want to have some sort of backup lock up code in case this wasn't true.

51
00:03:48,170 --> 00:03:55,780
Well what it could then add onto is say else colon and then something else I could say print all above

52
00:03:55,790 --> 00:03:58,750
conditions were not true.

53
00:04:02,590 --> 00:04:05,590
So when I run this right now what would I expect to see.

54
00:04:05,680 --> 00:04:07,470
Currently these conditions are true.

55
00:04:07,510 --> 00:04:09,640
So I should still see access granted.

56
00:04:10,000 --> 00:04:15,040
So I hit enter and I see Access Granted but let's simplify this if statement to something that's just

57
00:04:15,040 --> 00:04:16,390
not going to be true.

58
00:04:16,420 --> 00:04:20,050
So say if one is equal to two.

59
00:04:20,500 --> 00:04:21,550
Well that's definitely not true.

60
00:04:21,550 --> 00:04:24,290
So basically this block of code won't be executed then.

61
00:04:24,430 --> 00:04:25,750
So I'll check this condition.

62
00:04:25,750 --> 00:04:27,220
That's not true it's a false.

63
00:04:27,250 --> 00:04:28,360
So it doesn't execute this.

64
00:04:28,390 --> 00:04:30,050
Instead it drops down to else.

65
00:04:30,100 --> 00:04:33,710
And notice how the indentation if and else are lined up exactly.

66
00:04:33,940 --> 00:04:38,000
That's how Python knows that these two are go together for each other.

67
00:04:38,020 --> 00:04:41,050
So when I run this now I should see all above.

68
00:04:41,050 --> 00:04:42,820
Conditions were not true.

69
00:04:43,210 --> 00:04:46,720
So I'm going to say Python my example about pi enter.

70
00:04:46,750 --> 00:04:49,240
Now I see all the above conditions were not true.

71
00:04:49,240 --> 00:04:52,950
So checking for one condition and then saying else execute another.

72
00:04:53,140 --> 00:04:59,030
However we can always add more conditions to check so we can do this just by tacking on an Ellis statement.

73
00:04:59,140 --> 00:05:00,630
And these are going to have other conditions.

74
00:05:00,640 --> 00:05:01,930
So let's do another one like.

75
00:05:01,960 --> 00:05:04,120
One is equal to 1.

76
00:05:04,230 --> 00:05:14,080
So I can print middle condition true save that and when I run this now I get to see middle condition

77
00:05:14,080 --> 00:05:15,300
is true.

78
00:05:15,310 --> 00:05:17,470
Now if this happens to be false as well.

79
00:05:17,470 --> 00:05:18,750
So now I have this one is false.

80
00:05:18,760 --> 00:05:20,290
This one's false than the else.

81
00:05:20,320 --> 00:05:24,490
At the very end is going to be executed and you can stack as many lives as you want.

82
00:05:24,490 --> 00:05:28,790
So if you want another LFA there maybe can check for fours equal to four.

83
00:05:29,060 --> 00:05:37,230
Prince third condition and now we run this I get back third condition.

84
00:05:37,360 --> 00:05:41,120
Something to keep in mind is let's imagine we have two cases that are true.

85
00:05:41,410 --> 00:05:44,160
So notice right now my second case one is equal to one.

86
00:05:44,170 --> 00:05:46,770
So the condition is true and therefore is equal to four.

87
00:05:46,780 --> 00:05:47,870
That's also true.

88
00:05:47,980 --> 00:05:48,940
When I run this.

89
00:05:48,970 --> 00:05:51,230
So make sure you save that.

90
00:05:51,440 --> 00:05:53,540
I only see middle condition is true.

91
00:05:53,540 --> 00:05:55,310
I don't see this third condition.

92
00:05:55,340 --> 00:05:59,130
That's because it's only going to execute the first hit of a true condition.

93
00:05:59,150 --> 00:06:01,690
It's not going to go down and continue for the rest.

94
00:06:01,690 --> 00:06:07,010
So for if statements and if elif statements and if else statements it's only going to execute the first

95
00:06:07,010 --> 00:06:10,700
block of code that's within a true conditional statement.

96
00:06:10,700 --> 00:06:16,500
All right so let's finish this off by kind of showing one last more realistic example.

97
00:06:16,560 --> 00:06:25,680
So it will do the following we'll say user name is equal to admen will say permission is equal.

98
00:06:25,700 --> 00:06:28,560
True and that's actually all we'll do.

99
00:06:28,600 --> 00:06:44,620
And we'll say if the user name is equal to admen and we have permission then Prince full access health.

100
00:06:44,720 --> 00:06:51,840
So Aliff Well we've got to put a condition there will say a user name is equal to Addleman.

101
00:06:51,880 --> 00:06:53,100
However there's no permission.

102
00:06:53,110 --> 00:07:09,070
So say an's permission is false then old prince admin access only no full permission.

103
00:07:11,960 --> 00:07:22,260
And then else we'll say Prince no access OK so here what we're saying is we have a username admen permission

104
00:07:22,260 --> 00:07:23,510
is equal to true.

105
00:07:23,530 --> 00:07:24,720
So take a look at this.

106
00:07:24,730 --> 00:07:28,020
Try to freeze frame it and see which one do you think is going to execute.

107
00:07:28,030 --> 00:07:31,960
Full access admin access only or no access.

108
00:07:31,970 --> 00:07:35,050
OK so if we take a look at this user name is equal to admin.

109
00:07:35,200 --> 00:07:37,830
So this condition is true and permission is true.

110
00:07:37,840 --> 00:07:40,740
So it looks like we should see right away for access.

111
00:07:40,750 --> 00:07:42,280
So when I run this that's what I see.

112
00:07:42,280 --> 00:07:46,910
Full access no change is permission to be false.

113
00:07:46,930 --> 00:07:51,050
So now when I run this well I'm not going to have permission is equal to true anymore.

114
00:07:51,060 --> 00:07:53,030
Instead it's going to be equal to false.

115
00:07:53,110 --> 00:07:56,740
So I should see adamant access only no full permission.

116
00:07:56,950 --> 00:07:59,020
And now let's set permission.

117
00:07:59,050 --> 00:08:01,800
True but let's go ahead and provide a different username.

118
00:08:01,960 --> 00:08:08,620
We provide Jose as a user name and when I run this I get back no access because none of these conditions

119
00:08:08,620 --> 00:08:10,350
or else were met.

120
00:08:10,780 --> 00:08:12,290
OK that's the very basics.

121
00:08:12,340 --> 00:08:18,040
If elephant statements will be using these a lot for control flow if you have any questions feel free

122
00:08:18,040 --> 00:08:19,560
to post the Q&amp;A forums.

123
00:08:19,690 --> 00:08:20,880
We'll see you at the next lecture.

124
00:08:20,890 --> 00:08:22,210
Well we begin to discuss Lupe's.
