1
1

00:00:00,770  -->  00:00:02,510
<v ->Hi guys, and welcome back.</v>
2

2

00:00:02,510  -->  00:00:05,220
In this video we're going to learn about if statements.
3

3

00:00:05,220  -->  00:00:07,979
An if statement allows us to use a Boolean
4

4

00:00:07,979  -->  00:00:10,340
to make a decision in our programme
5

5

00:00:10,340  -->  00:00:12,160
and potentially do different things,
6

6

00:00:12,160  -->  00:00:14,278
depending on the value of that Boolean.
7

7

00:00:14,278  -->  00:00:16,930
Of course, if statements are most useful
8

8

00:00:16,930  -->  00:00:19,020
when you have user input,
9

9

00:00:19,020  -->  00:00:22,220
such that your programme can potentially get different data,
10

10

00:00:22,220  -->  00:00:25,580
depending on who runs it and who's using your programme.
11

11

00:00:25,580  -->  00:00:27,830
So here we've got an input that asks the user
12

12

00:00:27,830  -->  00:00:29,430
for the day of the week,
13

13

00:00:29,430  -->  00:00:31,960
so what day of the week is it today?
14

14

00:00:31,960  -->  00:00:33,730
And then, we're going to check
15

15

00:00:33,730  -->  00:00:36,320
whether the current day of the week is Monday.
16

16

00:00:36,320  -->  00:00:37,440
So, how would you check
17

17

00:00:37,440  -->  00:00:39,260
whether the current day of the week is Monday?
18

18

00:00:39,260  -->  00:00:44,260
You would say day_of_week == the string "Monday".
19

19

00:00:45,050  -->  00:00:49,630
Let's print this out to see whether our Boolean works.
20

20

00:00:49,630  -->  00:00:52,870
So, all I've done is printed the Boolean out.
21

21

00:00:52,870  -->  00:00:54,813
And again, remember that this here
22

22

00:00:54,813  -->  00:00:58,053
will evaluate first, so Python will say,
23

23

00:00:58,053  -->  00:01:00,500
"Day of week, is it equal to Monday?"
24

24

00:01:00,500  -->  00:01:02,650
And if it is, then it will replace
25

25

00:01:02,650  -->  00:01:04,920
this entire thing by "true."
26

26

00:01:04,920  -->  00:01:07,597
Otherwise, it will be replaced by "false."
27

27

00:01:08,600  -->  00:01:09,990
And, if we run this,
28

28

00:01:09,990  -->  00:01:11,830
you see that we get our prompt,
29

29

00:01:11,830  -->  00:01:13,220
and you can type in "Monday,"
30

30

00:01:13,220  -->  00:01:14,780
and then you get True.
31

31

00:01:14,780  -->  00:01:16,092
But, if you type "Tuesday,"
32

32

00:01:16,092  -->  00:01:18,670
then you get False.
33

33

00:01:18,670  -->  00:01:20,750
So this is what we expected.
34

34

00:01:20,750  -->  00:01:22,930
Now, instead of printing this out,
35

35

00:01:22,930  -->  00:01:26,410
we can actually put it in an if statement.
36

36

00:01:26,410  -->  00:01:29,040
So the if statement is like this,
37

37

00:01:29,040  -->  00:01:33,450
you type in your if keyword, which is special in Python.
38

38

00:01:33,450  -->  00:01:36,040
Then you put a space, and then,
39

39

00:01:36,040  -->  00:01:37,772
without putting brackets or anything,
40

40

00:01:37,772  -->  00:01:41,980
you type your Boolean comparison.
41

41

00:01:41,980  -->  00:01:44,373
And then at the end you put your colon.
42

42

00:01:45,320  -->  00:01:49,140
This will evaluate as soon as the if statement runs,
43

43

00:01:49,140  -->  00:01:51,450
so as soon as Python reaches line 3,
44

44

00:01:51,450  -->  00:01:54,250
it's gonna calculate the result of this Boolean,
45

45

00:01:54,250  -->  00:01:56,630
which is gonna be true if the user entered "Monday,"
46

46

00:01:56,630  -->  00:01:58,680
and false if they didn't.
47

47

00:01:58,680  -->  00:02:00,930
And if it is true,
48

48

00:02:00,930  -->  00:02:05,590
then we will run the code under the if statement.
49

49

00:02:05,590  -->  00:02:07,720
And here's one of the most important things
50

50

00:02:07,720  -->  00:02:09,330
in Python programming,
51

51

00:02:09,330  -->  00:02:11,350
which is that there are a few spaces
52

52

00:02:11,350  -->  00:02:13,050
in front of this line here.
53

53

00:02:13,050  -->  00:02:15,630
This is called indentation in Python,
54

54

00:02:15,630  -->  00:02:19,910
and after a colon that defines a block of code,
55

55

00:02:19,910  -->  00:02:22,740
such as an if statement or a function or a class,
56

56

00:02:22,740  -->  00:02:24,378
we're gonna learn more about those later on,
57

57

00:02:24,378  -->  00:02:27,580
you need to put indentation.
58

58

00:02:27,580  -->  00:02:30,315
That is how Python tells which code
59

59

00:02:30,315  -->  00:02:32,520
is inside the if statement,
60

60

00:02:32,520  -->  00:02:36,160
i.e. which code you want to run when this is true.
61

61

00:02:36,160  -->  00:02:38,607
So, for example, you can print
62

62

00:02:38,607  -->  00:02:41,207
"Have a great start to your week!"
63

63

00:02:42,470  -->  00:02:44,890
Now, this is the line of code
64

64

00:02:44,890  -->  00:02:47,140
that is inside the if statement.
65

65

00:02:47,140  -->  00:02:49,930
If this is true, this line of code will run,
66

66

00:02:49,930  -->  00:02:53,040
because it's got these four spaces in front.
67

67

00:02:53,040  -->  00:02:53,873
I'll say at this point that
68

68

00:02:53,873  -->  00:02:55,610
you don't have to use four space in Python,
69

69

00:02:55,610  -->  00:02:57,850
you can use two, you can use six, or eight,
70

70

00:02:57,850  -->  00:03:00,540
whatever you want, but be consistent.
71

71

00:03:00,540  -->  00:03:02,400
So, always use the same number of spaces
72

72

00:03:02,400  -->  00:03:03,938
that's gonna make your code work,
73

73

00:03:03,938  -->  00:03:05,690
otherwise it may not work,
74

74

00:03:05,690  -->  00:03:07,880
and also it's gonna make your code look much better.
75

75

00:03:07,880  -->  00:03:09,360
If you've got different indentations
76

76

00:03:09,360  -->  00:03:10,720
in different parts of your programme,
77

77

00:03:10,720  -->  00:03:11,920
people are gonna be really confused
78

78

00:03:11,920  -->  00:03:13,400
when they look at your code.
79

79

00:03:13,400  -->  00:03:16,423
I recommend using four spaces for Python code.
80

80

00:03:17,320  -->  00:03:21,200
Now, if you print something and you remove the indentation,
81

81

00:03:21,200  -->  00:03:22,393
the spaces in front,
82

82

00:03:26,370  -->  00:03:28,320
for example, like this, what you're gonna get
83

83

00:03:28,320  -->  00:03:31,200
is that this code is always going to run,
84

84

00:03:31,200  -->  00:03:33,720
if this is true, or if it is false.
85

85

00:03:33,720  -->  00:03:35,530
Because it's not in the if statement,
86

86

00:03:35,530  -->  00:03:38,200
it's completely unrelated to the if statement.
87

87

00:03:38,200  -->  00:03:40,640
So, Python will run this code if it's true,
88

88

00:03:40,640  -->  00:03:41,800
it will run this code.
89

89

00:03:41,800  -->  00:03:43,520
If it's false, it will not run this code,
90

90

00:03:43,520  -->  00:03:45,300
and then it will jump to the next line
91

91

00:03:45,300  -->  00:03:47,290
and it will continue running from here.
92

92

00:03:47,290  -->  00:03:49,180
So, I'm gonna run this down here again,
93

93

00:03:49,180  -->  00:03:50,445
and I'm gonna type "Monday,"
94

94

00:03:50,445  -->  00:03:51,887
and then you see that we get,
95

95

00:03:51,887  -->  00:03:53,390
"Have a great start to your week!"
96

96

00:03:53,390  -->  00:03:55,250
and, "This always runs."
97

97

00:03:55,250  -->  00:03:56,771
But, if we type in "Tuesday,"
98

98

00:03:56,771  -->  00:03:59,130
then you just see "This always runs."
99

99

00:03:59,130  -->  00:04:01,847
and you don't see the "great start to your week".
100

100

00:04:02,890  -->  00:04:04,295
If you want to print something
101

101

00:04:04,295  -->  00:04:07,429
if the day of the week is not Monday,
102

102

00:04:07,429  -->  00:04:09,870
then you can do something like this:
103

103

00:04:09,870  -->  00:04:14,870
If day_of_week is not Monday, then print
104

104

00:04:15,597  -->  00:04:17,230
"Full speed ahead!"
105

105

00:04:17,230  -->  00:04:19,980
Let's run that, and then I'll type "Monday,"
106

106

00:04:19,980  -->  00:04:22,200
and you get "Have a great start to your week!"
107

107

00:04:22,200  -->  00:04:24,490
But, if we type "Tuesday,"
108

108

00:04:24,490  -->  00:04:27,240
then you see "Full speed ahead!"
109

109

00:04:27,240  -->  00:04:29,150
Now this is actually not great Python code,
110

110

00:04:29,150  -->  00:04:31,368
because you have two checks
111

111

00:04:31,368  -->  00:04:34,740
that will always be evaluated by Python.
112

112

00:04:34,740  -->  00:04:37,400
Here you've got day_of_week == "Monday"
113

113

00:04:37,400  -->  00:04:39,500
and this will always be checked,
114

114

00:04:39,500  -->  00:04:41,760
whether it is true or false, by Python.
115

115

00:04:41,760  -->  00:04:45,080
And then you have day_of_week is not equal to "Monday"
116

116

00:04:45,080  -->  00:04:47,180
and this will always be checked by Python,
117

117

00:04:48,240  -->  00:04:51,060
regardless of whether this one was true.
118

118

00:04:51,060  -->  00:04:53,170
Evidently, if this one is true,
119

119

00:04:53,170  -->  00:04:56,380
this one cannot be, and vice versa.
120

120

00:04:56,380  -->  00:04:59,130
So, Python provides you a very nice piece of syntax
121

121

00:04:59,130  -->  00:05:01,017
called the else syntax.
122

122

00:05:01,017  -->  00:05:02,590
And what this does is,
123

123

00:05:02,590  -->  00:05:05,403
if this is true, then we run this code.
124

124

00:05:06,340  -->  00:05:08,540
Otherwise, run this code.
125

125

00:05:08,540  -->  00:05:11,713
So this is much nicer and also is much easier to read.
126

126

00:05:12,570  -->  00:05:14,100
Let me say at this stage,
127

127

00:05:14,100  -->  00:05:17,350
that only one of these two will run.
128

128

00:05:17,350  -->  00:05:19,640
You cannot possibly run both,
129

129

00:05:19,640  -->  00:05:21,670
because you can't have this be true
130

130

00:05:21,670  -->  00:05:23,523
and also false at the same time.
131

131

00:05:24,500  -->  00:05:26,908
In addition, if you wanted to print something else,
132

132

00:05:26,908  -->  00:05:29,370
if the day of the week is Tuesday,
133

133

00:05:29,370  -->  00:05:34,253
you can do elif day_of_week == "Tuesday"
134

134

00:05:35,810  -->  00:05:39,580
And what this does is it creates a chained if statement,
135

135

00:05:39,580  -->  00:05:42,637
an if statement with three different branches.
136

136

00:05:42,637  -->  00:05:44,290
First, Python will check
137

137

00:05:44,290  -->  00:05:46,372
whether the day of the week is Monday,
138

138

00:05:46,372  -->  00:05:48,760
and it will print this if it is true.
139

139

00:05:48,760  -->  00:05:49,920
But if it's not true,
140

140

00:05:49,920  -->  00:05:50,970
then Python will check
141

141

00:05:50,970  -->  00:05:52,710
whether the day of the week is Tuesday,
142

142

00:05:52,710  -->  00:05:55,450
and it will print this if it is true.
143

143

00:05:55,450  -->  00:05:57,650
But if neither of those are true,
144

144

00:05:57,650  -->  00:05:59,543
then it will go to the else.
145

145

00:06:00,730  -->  00:06:04,130
An important part of this syntax here is that
146

146

00:06:04,130  -->  00:06:06,193
if the day of the week is Monday,
147

147

00:06:07,040  -->  00:06:09,380
then Python will print this stuff,
148

148

00:06:09,380  -->  00:06:11,070
and it will ignore the rest.
149

149

00:06:11,070  -->  00:06:14,420
So it will never calculate this comparison.
150

150

00:06:14,420  -->  00:06:16,750
So this is actually good for performance,
151

151

00:06:16,750  -->  00:06:19,800
and also it lets you have a comparison here
152

152

00:06:19,800  -->  00:06:22,460
that when it evaluates, or when it's true,
153

153

00:06:22,460  -->  00:06:24,130
it ignores the rest.
154

154

00:06:24,130  -->  00:06:25,900
So there's actually a lot of stuff you can do
155

155

00:06:25,900  -->  00:06:27,030
with this knowledge,
156

156

00:06:27,030  -->  00:06:28,430
and you'll encounter that plenty
157

157

00:06:28,430  -->  00:06:30,803
while you're doing Python programming.
158

158

00:06:32,190  -->  00:06:35,180
A common problem when you are dealing with user input
159

159

00:06:35,180  -->  00:06:38,510
is that they may not enter exactly what you expect.
160

160

00:06:38,510  -->  00:06:40,100
For example, it is quite common
161

161

00:06:40,100  -->  00:06:43,603
for a user to enter all lowercase letters
162

162

00:06:43,603  -->  00:06:46,365
when you ask them for something,
163

163

00:06:46,365  -->  00:06:48,270
just because they can't be bothered
164

164

00:06:48,270  -->  00:06:49,970
typing the uppercase letters.
165

165

00:06:49,970  -->  00:06:51,740
So here, the user has typed "monday"
166

166

00:06:51,740  -->  00:06:54,397
all in lowercase, and then we have printed out
167

167

00:06:54,397  -->  00:06:56,860
"Full speed ahead!" because the day of the week
168

168

00:06:56,860  -->  00:06:59,740
is not exactly equal to the string "Monday"
169

169

00:06:59,740  -->  00:07:01,400
with a capital M.
170

170

00:07:01,400  -->  00:07:04,360
So, you have to do something about it
171

171

00:07:04,360  -->  00:07:06,610
if you want your programme to be able to detect
172

172

00:07:06,610  -->  00:07:08,460
whether the day of the week is Monday
173

173

00:07:08,460  -->  00:07:11,470
in uppercase and in lowercase.
174

174

00:07:11,470  -->  00:07:14,360
What I'd recommend is that you get the user's input
175

175

00:07:15,270  -->  00:07:19,200
and just after you type .lower
176

176

00:07:19,200  -->  00:07:23,490
And then, this is going to turn this entire string
177

177

00:07:24,610  -->  00:07:27,163
into it's lowercase variant.
178

178

00:07:28,310  -->  00:07:31,931
Now, if they enter Monday with the capital M,
179

179

00:07:31,931  -->  00:07:34,830
then it is also gonna be turned into monday
180

180

00:07:34,830  -->  00:07:36,402
with a lowercase m,
181

181

00:07:36,402  -->  00:07:38,810
which means that monday =="Monday"
182

182

00:07:38,810  -->  00:07:41,460
with an uppercase M will not match.
183

183

00:07:41,460  -->  00:07:42,970
So, instead what you should do
184

184

00:07:42,970  -->  00:07:45,500
is turn everything to lowercase.
185

185

00:07:45,500  -->  00:07:48,160
By turning the user's input into lowercase,
186

186

00:07:48,160  -->  00:07:50,740
and your checks into lowercase,
187

187

00:07:50,740  -->  00:07:52,680
you're always going to have the same case,
188

188

00:07:52,680  -->  00:07:54,690
because they're both lowercase.
189

189

00:07:54,690  -->  00:07:56,690
So if we save that and run it,
190

190

00:07:56,690  -->  00:07:59,190
you'll see that now I can type "monday,"
191

191

00:07:59,190  -->  00:08:01,970
but I can also type "MONDAY" all in uppercase,
192

192

00:08:01,970  -->  00:08:04,850
and I can also type "Monday" as usual.
193

193

00:08:04,850  -->  00:08:06,970
So this is a good way of insuring that your programme
194

194

00:08:06,970  -->  00:08:10,973
will work, no matter what type of case the users type.
195

195

00:08:11,830  -->  00:08:12,890
Let's leave it at this,
196

196

00:08:12,890  -->  00:08:14,310
thank you for joining me in this video,
197

197

00:08:14,310  -->  00:08:15,440
I hope it's been useful,
198

198

00:08:15,440  -->  00:08:17,090
and I'll see you in the next one.
