WEBVTT 1 00:00:01.200 --> 00:00:03.020 Hi there, and welcome back. 2 00:00:03.020 --> 00:00:03.910 In this video we're going 3 00:00:03.910 --> 00:00:06.610 to be learning about if statements. 4 00:00:06.610 --> 00:00:09.750 If statements allow our programmes to make decisions 5 00:00:09.750 --> 00:00:12.350 depending on a Boolean. 6 00:00:12.350 --> 00:00:14.820 Let's get started with a friend name. 7 00:00:14.820 --> 00:00:17.570 Let's say we've got a friend called Rolf 8 00:00:17.570 --> 00:00:21.000 and we want to check whether the user's name is Rolf. 9 00:00:21.000 --> 00:00:22.250 So the first thing we have to do 10 00:00:22.250 --> 00:00:24.920 is ask the user for their name, 11 00:00:24.920 --> 00:00:27.800 and then we're gonna start typing our first if statement. 12 00:00:27.800 --> 00:00:29.360 What we're gonna do is we're gonna start 13 00:00:29.360 --> 00:00:32.300 with the first keyword if. 14 00:00:32.300 --> 00:00:34.630 The if keyword is followed by a space 15 00:00:34.630 --> 00:00:37.630 and then we put the Boolean. 16 00:00:37.630 --> 00:00:42.630 If this Boolean is true, then we will execute some code. 17 00:00:42.860 --> 00:00:46.570 So if true, we will execute some code. 18 00:00:46.570 --> 00:00:50.200 So we're gonna say hello, friend. 19 00:00:50.200 --> 00:00:53.440 Now of course, true is always true, 20 00:00:53.440 --> 00:00:57.430 so this if statement is always going to execute. 21 00:00:57.430 --> 00:01:00.000 So what we want to put in here instead of true, 22 00:01:00.000 --> 00:01:04.180 is a Boolean condition that depends on our variables, 23 00:01:04.180 --> 00:01:07.910 one of which is something dynamic that the user types. 24 00:01:07.910 --> 00:01:10.120 That way every time the user runs the programme, 25 00:01:10.120 --> 00:01:11.640 they can type something different 26 00:01:11.640 --> 00:01:15.530 and this if statement will behave in one way or another. 27 00:01:15.530 --> 00:01:16.830 What we're gonna put here is, 28 00:01:16.830 --> 00:01:20.463 if user underscore name, equal equal, friend. 29 00:01:21.640 --> 00:01:23.950 Now, whenever we run this programme, 30 00:01:23.950 --> 00:01:26.730 the friend variable will equal to Rolf, 31 00:01:26.730 --> 00:01:28.360 and the username variable will equal 32 00:01:28.360 --> 00:01:30.370 to whatever the user types. 33 00:01:30.370 --> 00:01:32.320 Then we will compare them, 34 00:01:32.320 --> 00:01:34.360 and if this evaluates to true, 35 00:01:34.360 --> 00:01:38.080 the if statement will run this code here. 36 00:01:38.080 --> 00:01:40.420 If it evaluates to false, then the if statement 37 00:01:40.420 --> 00:01:42.260 will not run this code. 38 00:01:42.260 --> 00:01:45.420 Now, let's talk about the syntax of the if statement. 39 00:01:45.420 --> 00:01:47.380 First of all we have the if keyword, 40 00:01:47.380 --> 00:01:51.823 then we have our Boolean, then we have this colon, 41 00:01:52.750 --> 00:01:54.590 which is necessary in the if statement 42 00:01:54.590 --> 00:01:57.020 at the end of the first line. 43 00:01:57.020 --> 00:02:00.170 And then we have the body of the if statement. 44 00:02:00.170 --> 00:02:03.380 Now something very important about if statements 45 00:02:03.380 --> 00:02:08.290 is that they have to have spaces in front of each line. 46 00:02:08.290 --> 00:02:11.650 Usually we use four spaces in Python. 47 00:02:11.650 --> 00:02:15.230 That's important because if you want to add another thing 48 00:02:15.230 --> 00:02:19.650 inside the if statement, such as print this runs too, 49 00:02:19.650 --> 00:02:22.510 this also has four spaces in front of it, 50 00:02:22.510 --> 00:02:25.590 and therefore Python knows that both of these prints 51 00:02:25.590 --> 00:02:28.930 are inside the if statement, and they should run 52 00:02:28.930 --> 00:02:31.800 only if this evaluates to true. 53 00:02:31.800 --> 00:02:34.580 As soon as you eliminate those spaces 54 00:02:34.580 --> 00:02:35.980 and you type something else, 55 00:02:37.920 --> 00:02:40.780 this is no longer part of the if statement. 56 00:02:40.780 --> 00:02:42.230 It always runs. 57 00:02:42.230 --> 00:02:44.970 Because it's outside the if statement. 58 00:02:44.970 --> 00:02:48.810 So to recap, this if statement will run this code 59 00:02:48.810 --> 00:02:52.610 inside the body, only if this is true, 60 00:02:52.610 --> 00:02:55.180 and these lines need to have some spacing 61 00:02:55.180 --> 00:02:57.480 so that Python knows that they are both 62 00:02:57.480 --> 00:02:59.090 inside the if statement. 63 00:02:59.090 --> 00:03:00.670 Then when you take away the spaces, 64 00:03:00.670 --> 00:03:03.590 Python knows that the if statement is terminated. 65 00:03:03.590 --> 00:03:06.373 Notice that you cannot do this, 66 00:03:07.660 --> 00:03:09.490 because Python would give you an error. 67 00:03:09.490 --> 00:03:13.670 This follows a line that doesn't have spaces in front 68 00:03:13.670 --> 00:03:15.640 so it's not part of the if statement, 69 00:03:15.640 --> 00:03:17.540 it's just a line with spaces in front, 70 00:03:17.540 --> 00:03:19.120 and Python won't like that. 71 00:03:19.120 --> 00:03:20.780 So make sure that all the lines 72 00:03:20.780 --> 00:03:22.370 that are inside the if statement 73 00:03:22.370 --> 00:03:23.840 have these spaces. 74 00:03:23.840 --> 00:03:25.440 As soon as you terminate the spaces, 75 00:03:25.440 --> 00:03:27.570 you terminate the if statement. 76 00:03:27.570 --> 00:03:30.090 Let's run this just to show you how it works. 77 00:03:30.090 --> 00:03:33.600 I'll type in Rolf, and you see the three things printed out. 78 00:03:33.600 --> 00:03:36.840 Hello friend, this runs too, and this runs anyway, 79 00:03:36.840 --> 00:03:39.110 and now if I run it again and I type Bob, 80 00:03:39.110 --> 00:03:41.470 you'll see that this runs anyway is printed out. 81 00:03:41.470 --> 00:03:43.010 That's this line here. 82 00:03:43.010 --> 00:03:44.810 And these two lines don't get printed out 83 00:03:44.810 --> 00:03:46.630 because the username was not equal 84 00:03:46.630 --> 00:03:48.840 to the friend in this case. 85 00:03:48.840 --> 00:03:50.470 Now imagine you wanted to do something 86 00:03:50.470 --> 00:03:52.440 if the username is equal to the friend, 87 00:03:52.440 --> 00:03:54.210 such as print hello friend, 88 00:03:54.210 --> 00:03:57.380 and also something if they are not equal to one another, 89 00:03:57.380 --> 00:03:59.700 such as hello stranger. 90 00:03:59.700 --> 00:04:01.373 You could do something like this. 91 00:04:04.760 --> 00:04:07.380 You can do this, but it is a little bit verbose. 92 00:04:07.380 --> 00:04:08.850 There's a lot of typing involved, 93 00:04:08.850 --> 00:04:10.440 and there's a lot of similarities 94 00:04:10.440 --> 00:04:12.300 between these two statements. 95 00:04:12.300 --> 00:04:15.290 In fact, Python comes in with another keyword, 96 00:04:15.290 --> 00:04:19.923 the else keyword, that is precisely for this thing. 97 00:04:20.937 --> 00:04:24.070 This if statement is now called a compound if statement 98 00:04:24.070 --> 00:04:26.210 or an if statement with two branches, 99 00:04:26.210 --> 00:04:29.350 and what happens is if this one is not true, 100 00:04:29.350 --> 00:04:32.190 it will jump to the else one. 101 00:04:32.190 --> 00:04:34.570 So if the username is not equal to the friend, 102 00:04:34.570 --> 00:04:37.660 this will be false, and we will jump here 103 00:04:37.660 --> 00:04:39.910 and we will print hello, stranger. 104 00:04:39.910 --> 00:04:41.470 Let's run this. 105 00:04:41.470 --> 00:04:43.250 And I will type in Bob. 106 00:04:43.250 --> 00:04:45.830 And you'll see that hello, stranger gets printed out. 107 00:04:45.830 --> 00:04:47.610 So what's happened here is that Python knows 108 00:04:47.610 --> 00:04:49.660 that these two statements are related, 109 00:04:49.660 --> 00:04:53.010 because they come one after another 110 00:04:53.010 --> 00:04:57.070 without any other commands in between. 111 00:04:57.070 --> 00:04:59.500 What I mean by that is you've got this if statement 112 00:04:59.500 --> 00:05:00.930 then you've got the body, 113 00:05:00.930 --> 00:05:05.060 and the next line without these spaces in front 114 00:05:05.060 --> 00:05:06.350 is the else. 115 00:05:06.350 --> 00:05:08.620 So Python can build a relationship between these two 116 00:05:08.620 --> 00:05:10.160 and say they're related. 117 00:05:10.160 --> 00:05:13.130 And then what happens is, if this is not true, 118 00:05:13.130 --> 00:05:14.163 we jump here. 119 00:05:15.210 --> 00:05:18.720 Notice that if you put anything else here, 120 00:05:18.720 --> 00:05:21.380 you're going to get problems. 121 00:05:21.380 --> 00:05:22.940 Now, Python does not know 122 00:05:22.940 --> 00:05:26.210 that this else is related to this if. 123 00:05:26.210 --> 00:05:29.260 And therefore it tells you that this is invalid Python code 124 00:05:29.260 --> 00:05:32.010 because it doesn't make sense to just have an else statement 125 00:05:32.010 --> 00:05:33.620 on its own. 126 00:05:33.620 --> 00:05:35.570 Earlier in the course we mentioned that we could turn 127 00:05:35.570 --> 00:05:38.580 any value into a Boolean by passing it 128 00:05:38.580 --> 00:05:40.160 through the bool function. 129 00:05:40.160 --> 00:05:43.680 For example, print bool of five. 130 00:05:43.680 --> 00:05:45.930 Here we're given five to the bool function 131 00:05:45.930 --> 00:05:47.770 by putting it inside the brackets 132 00:05:47.770 --> 00:05:50.530 and then we're giving that to the print function 133 00:05:50.530 --> 00:05:51.600 to show it to the user. 134 00:05:51.600 --> 00:05:54.750 So if we run this, you'll see that true gets printed out 135 00:05:54.750 --> 00:05:57.633 because bool of five is true. 136 00:05:58.790 --> 00:06:01.273 Bool of zero for example, is false. 137 00:06:02.360 --> 00:06:03.870 So why is this important? 138 00:06:03.870 --> 00:06:08.870 Well you can do if five, then print something like this. 139 00:06:09.210 --> 00:06:12.280 What this means is that, if you do this by the way 140 00:06:12.280 --> 00:06:14.670 this will always print out runs, 141 00:06:14.670 --> 00:06:19.460 because five is true when passed through bool. 142 00:06:19.460 --> 00:06:20.640 And behind the scenes, 143 00:06:20.640 --> 00:06:22.760 Python is always passing everything here 144 00:06:22.760 --> 00:06:24.790 through the bool function to check 145 00:06:24.790 --> 00:06:28.110 whether it evaluates to true or false. 146 00:06:28.110 --> 00:06:30.030 I just wanted to show you that the thing 147 00:06:30.030 --> 00:06:32.010 that you put in the if statement does not have 148 00:06:32.010 --> 00:06:34.170 to be a Boolean comparison. 149 00:06:34.170 --> 00:06:36.060 It can just be a value. 150 00:06:36.060 --> 00:06:39.423 For example, if we ask the user for their name, 151 00:06:40.800 --> 00:06:43.850 we can just say if name. 152 00:06:43.850 --> 00:06:48.850 Here, what we're doing is if the name evaluates to true 153 00:06:48.900 --> 00:06:51.470 then we will print we know your name. 154 00:06:51.470 --> 00:06:52.710 Now this is interesting, 155 00:06:52.710 --> 00:06:55.740 because if we print the bool of name, 156 00:06:55.740 --> 00:06:59.080 you will see that no matter what we type, 157 00:06:59.080 --> 00:07:02.070 such as Rolf, it evaluates to true. 158 00:07:02.070 --> 00:07:05.010 If we type something else, such as Bob, 159 00:07:05.010 --> 00:07:06.520 it evaluates to true. 160 00:07:06.520 --> 00:07:10.053 If we type something else, such as nothing, 161 00:07:11.660 --> 00:07:13.670 that's when it evaluates to false. 162 00:07:13.670 --> 00:07:16.710 So the bool of an empty string, 163 00:07:16.710 --> 00:07:20.480 i.e. if we don't type anything, is false. 164 00:07:20.480 --> 00:07:22.080 So this is one of these scenarios 165 00:07:22.080 --> 00:07:24.960 where putting the variable directly in the if statement 166 00:07:24.960 --> 00:07:26.740 can be useful. 167 00:07:26.740 --> 00:07:29.230 Simply if we don't have a value for name 168 00:07:29.230 --> 00:07:32.093 or if the names value is an empty string, 169 00:07:33.160 --> 00:07:34.463 we don't run this. 170 00:07:35.850 --> 00:07:37.400 Imagine you've got this code, 171 00:07:37.400 --> 00:07:39.300 and you have a list of your friends, 172 00:07:39.300 --> 00:07:42.430 Rolf, Bob and Anne, and a list of your family members, 173 00:07:42.430 --> 00:07:44.390 Jen and Charlie. 174 00:07:44.390 --> 00:07:47.240 Now we want to ask the user for their name, 175 00:07:47.240 --> 00:07:48.830 and then print hello friend, 176 00:07:48.830 --> 00:07:50.500 if they are one of your friends, 177 00:07:50.500 --> 00:07:53.110 hello family if they are one of your family members, 178 00:07:53.110 --> 00:07:55.793 or I don't know you if they are neither of them. 179 00:07:56.950 --> 00:08:00.038 We'll start by doing if user underscore name, 180 00:08:00.038 --> 00:08:01.963 equal equal friends. 181 00:08:02.800 --> 00:08:05.020 Now, here we've got a problem. 182 00:08:05.020 --> 00:08:07.560 Equal equal compares two things directly. 183 00:08:07.560 --> 00:08:10.270 And username is always going to be a string. 184 00:08:10.270 --> 00:08:13.150 That's what the input function always give us. 185 00:08:13.150 --> 00:08:15.710 But friends is a list. 186 00:08:15.710 --> 00:08:17.070 So these two things are never going 187 00:08:17.070 --> 00:08:18.530 to be equal to one another, 188 00:08:18.530 --> 00:08:21.180 just because they are not even the same type of data. 189 00:08:22.350 --> 00:08:23.700 So what we want to do is we want 190 00:08:23.700 --> 00:08:26.390 to use Python's keyword in. 191 00:08:26.390 --> 00:08:30.080 The in keyword checks for whether the username 192 00:08:30.080 --> 00:08:33.323 is a value contained within the friends collection. 193 00:08:34.710 --> 00:08:39.710 So if this is true, then we will print hello, friend. 194 00:08:42.350 --> 00:08:45.030 Now, we wanna check whether the username is 195 00:08:45.030 --> 00:08:46.610 in the family members. 196 00:08:46.610 --> 00:08:48.240 So you may be tempted to do something like 197 00:08:48.240 --> 00:08:50.943 if username in family, 198 00:08:52.220 --> 00:08:54.920 and then of course your else statement. 199 00:08:54.920 --> 00:08:57.713 What we print, I don't know you. 200 00:08:59.500 --> 00:09:01.240 You may be tempted to do this, 201 00:09:01.240 --> 00:09:03.250 but alas that is a problem. 202 00:09:03.250 --> 00:09:06.580 Let's run this programme and I'll type Rolf. 203 00:09:06.580 --> 00:09:09.240 And now you could see that we get two things printed out, 204 00:09:09.240 --> 00:09:10.400 which is not ideal. 205 00:09:10.400 --> 00:09:13.100 We get hello friend, and then I don't know you. 206 00:09:13.100 --> 00:09:15.340 Clearly not the case if we print hello friend, 207 00:09:15.340 --> 00:09:17.250 we must know this person. 208 00:09:17.250 --> 00:09:19.223 So here's what happened in the code. 209 00:09:20.240 --> 00:09:23.130 We have Rolf inside our friends list, 210 00:09:23.130 --> 00:09:26.430 but we don't have Rolf inside our family list. 211 00:09:26.430 --> 00:09:31.430 Then the username was Rolf, so Rolf in friends is true. 212 00:09:31.720 --> 00:09:33.700 So we printed hello, friend. 213 00:09:33.700 --> 00:09:36.610 Now in an unrelated if statement, we checked 214 00:09:36.610 --> 00:09:39.000 whether the username it was in family. 215 00:09:39.000 --> 00:09:42.100 So Rolf in family is false. 216 00:09:42.100 --> 00:09:44.440 So because these two if statements are related 217 00:09:44.440 --> 00:09:46.750 to one another, we jump directly to the else, 218 00:09:46.750 --> 00:09:49.130 and we printed I don't know you. 219 00:09:49.130 --> 00:09:50.920 So really what we want to do, 220 00:09:50.920 --> 00:09:53.700 is we wanna chain all of these if statements, 221 00:09:53.700 --> 00:09:57.000 so that only one of them ever runs. 222 00:09:57.000 --> 00:09:58.960 Of course Python comes with that too. 223 00:09:58.960 --> 00:10:00.960 Just type elif. 224 00:10:00.960 --> 00:10:05.960 What this means is if this, else if this, else that. 225 00:10:06.750 --> 00:10:11.750 So we will do if Rolf in friends, print hello friend, 226 00:10:12.080 --> 00:10:16.410 otherwise, if the username is in family, print hello family, 227 00:10:16.410 --> 00:10:19.260 otherwise if neither of those were true, 228 00:10:19.260 --> 00:10:21.430 print I don't know you. 229 00:10:21.430 --> 00:10:24.930 Now if we run this, and we type Rolf, 230 00:10:24.930 --> 00:10:26.970 you see that hello friend gets printed out, 231 00:10:26.970 --> 00:10:28.073 and nothing else. 232 00:10:29.350 --> 00:10:33.740 Similarly if we type here something like Smith, 233 00:10:33.740 --> 00:10:35.990 you'll see I don't know you. 234 00:10:35.990 --> 00:10:39.490 You can put as many elif statements as you want 235 00:10:39.490 --> 00:10:40.730 inside one of these chains, 236 00:10:40.730 --> 00:10:44.730 so you can have multiple of those if you wanted 237 00:10:44.730 --> 00:10:46.480 and as many of you want of course 238 00:10:46.480 --> 00:10:50.380 if they have the same comparison then that's kinda pointless 239 00:10:50.380 --> 00:10:52.690 but you can put as many of them as you want 240 00:10:52.690 --> 00:10:55.060 and they get evaluated in order, 241 00:10:55.060 --> 00:10:57.130 so this one gets evaluated first, 242 00:10:57.130 --> 00:10:59.200 then this one and so on. 243 00:10:59.200 --> 00:11:03.080 The first one that runs means that the others don't run. 244 00:11:03.080 --> 00:11:07.260 So if the username is in friends, these all get skipped. 245 00:11:07.260 --> 00:11:08.930 If the username is not in friends, 246 00:11:08.930 --> 00:11:10.560 then we will try this one. 247 00:11:10.560 --> 00:11:13.410 And if this is true, then this gets skipped. 248 00:11:13.410 --> 00:11:16.940 Finally, if none of them are true, then we run this. 249 00:11:16.940 --> 00:11:19.350 By the way I wanted to say that these four spaces 250 00:11:19.350 --> 00:11:23.190 in front of the lines have a special name 251 00:11:23.190 --> 00:11:25.430 and that is called indentation. 252 00:11:25.430 --> 00:11:27.970 I say that because you'll hear me say this more often 253 00:11:27.970 --> 00:11:30.770 and also you may hear it when you're reading about code. 254 00:11:30.770 --> 00:11:34.313 The indentation is the spaces in front of a line 255 00:11:34.313 --> 00:11:37.780 that denote that this is inside a block. 256 00:11:37.780 --> 00:11:42.350 And in this case the block in question is the if body. 257 00:11:42.350 --> 00:11:45.800 Usually we use four spaces for indentation in Python code, 258 00:11:45.800 --> 00:11:47.210 but you can use two if you want 259 00:11:47.210 --> 00:11:49.070 or indeed any other number of spaces. 260 00:11:49.070 --> 00:11:51.310 However, four is the normal amount to use 261 00:11:51.310 --> 00:11:53.660 and I would recommend that you use that. 262 00:11:53.660 --> 00:11:56.030 Many problems for beginner programmers 263 00:11:56.030 --> 00:11:59.020 in Python are caused by indentation. 264 00:11:59.020 --> 00:12:02.470 Just take special care when indenting your code 265 00:12:02.470 --> 00:12:05.380 and putting spaces in front of it or removing spaces 266 00:12:05.380 --> 00:12:08.020 from the front of it, as that can sometimes cause 267 00:12:08.020 --> 00:12:10.850 a bit of confusion, and can cause some problems. 268 00:12:10.850 --> 00:12:12.200 That's everything for this video though. 269 00:12:12.200 --> 00:12:14.560 We've learned about if statements. 270 00:12:14.560 --> 00:12:17.510 Thank you for joining me, and I'll see you in the next one.