1 00:00:00,420 --> 00:00:03,300 Instructor: Let's talk about conditional logic, 2 00:00:03,300 --> 00:00:06,210 an important concept all over programming. 3 00:00:06,210 --> 00:00:09,360 What do I mean by conditional logic? 4 00:00:09,360 --> 00:00:11,970 We've learned about booleans, right? 5 00:00:11,970 --> 00:00:15,690 We have true and we have false. 6 00:00:15,690 --> 00:00:19,890 And some of you might be wondering why they're so useful. 7 00:00:19,890 --> 00:00:22,170 And when it comes to conditional logic 8 00:00:22,170 --> 00:00:25,260 booleans are really, really important. 9 00:00:25,260 --> 00:00:28,727 For example, let's say we create a variable, is_old 10 00:00:31,440 --> 00:00:32,280 and you know what? 11 00:00:32,280 --> 00:00:37,050 We're going to create a car that automatically detects 12 00:00:37,050 --> 00:00:38,850 if you can start the engine. 13 00:00:38,850 --> 00:00:41,460 Maybe if you're not old enough 14 00:00:41,460 --> 00:00:44,280 the engine won't start and it won't let you drive 15 00:00:44,280 --> 00:00:47,130 so that they make sure that this car is super safe 16 00:00:47,130 --> 00:00:51,750 and only people that are perhaps over 18 can drive it. 17 00:00:51,750 --> 00:00:54,060 So how can we code something like that? 18 00:00:54,060 --> 00:00:56,463 Let's assume Tesla has a new feature like this. 19 00:00:57,390 --> 00:00:59,490 Well, we'll have something like is_old 20 00:00:59,490 --> 00:01:03,393 and we'll set this to true, right? 21 00:01:04,530 --> 00:01:07,410 Maybe we have something else. 22 00:01:07,410 --> 00:01:10,290 Maybe it checks the license to make sure that we have 23 00:01:10,290 --> 00:01:12,590 a license so we can say is_licensed 24 00:01:15,150 --> 00:01:16,700 and then we can set it to true. 25 00:01:17,610 --> 00:01:21,630 Now, these could change true and false depending on the user 26 00:01:21,630 --> 00:01:25,200 and the driver, but when I talk about conditionals, 27 00:01:25,200 --> 00:01:26,670 this is what I mean. 28 00:01:26,670 --> 00:01:31,670 In Python, we can use the if keyword to say 29 00:01:31,830 --> 00:01:36,830 if some condition exists, and this is just me writing it, 30 00:01:37,230 --> 00:01:38,130 it doesn't exist. 31 00:01:38,130 --> 00:01:40,530 You can see that I get a red underline 32 00:01:40,530 --> 00:01:42,390 with invalid syntax. 33 00:01:42,390 --> 00:01:45,990 But here we wanna say a condition that evaluates 34 00:01:45,990 --> 00:01:47,760 to true or false. 35 00:01:47,760 --> 00:01:50,777 In our case, we can say, hey, if is_old, 36 00:01:53,342 --> 00:01:54,510 which what does it evaluate to? 37 00:01:54,510 --> 00:01:57,180 Well, it evaluates to true. 38 00:01:57,180 --> 00:02:02,180 If that's true, then let's say print, 39 00:02:02,340 --> 00:02:05,373 you are old enough to drive. 40 00:02:06,600 --> 00:02:08,490 Now there's a few new things here. 41 00:02:08,490 --> 00:02:13,380 One, I've added a semicolon or a column, and then 42 00:02:13,380 --> 00:02:17,070 you'll see here that the indentation is different. 43 00:02:17,070 --> 00:02:22,070 As a matter of fact, when I press enter after the column, 44 00:02:23,640 --> 00:02:27,210 you see that it automatically gives me a space. 45 00:02:27,210 --> 00:02:29,700 This tells the Python interpreter, 46 00:02:29,700 --> 00:02:34,560 I'm going to do an if statement, a conditional operation. 47 00:02:34,560 --> 00:02:39,360 And if this happens to be true, run everything that's inside 48 00:02:39,360 --> 00:02:42,483 of here that has this indentation. 49 00:02:43,970 --> 00:02:46,830 And if I do something without the indentation, 50 00:02:46,830 --> 00:02:50,583 let's say print check check. 51 00:02:52,350 --> 00:02:54,240 This to the Python interpreter 52 00:02:54,240 --> 00:02:56,790 is like a completely new line. 53 00:02:56,790 --> 00:03:00,480 It is not part of this if block. 54 00:03:00,480 --> 00:03:02,430 And if you're using the repl, you see over here 55 00:03:02,430 --> 00:03:04,050 I get a little minus sign. 56 00:03:04,050 --> 00:03:07,533 If I click this, you see how it hides. 57 00:03:09,150 --> 00:03:11,220 Well, what it does is it's saying, hey 58 00:03:11,220 --> 00:03:13,260 this is a code block over here. 59 00:03:13,260 --> 00:03:17,130 It's an entire thing in itself, but anything 60 00:03:17,130 --> 00:03:22,130 outside that is not indented doesn't belong to it. 61 00:03:22,410 --> 00:03:25,080 So to a Python interpreter, it's going to say 62 00:03:25,080 --> 00:03:29,100 I have this line and then I have this line. 63 00:03:29,100 --> 00:03:30,630 Let me show you what I mean. 64 00:03:30,630 --> 00:03:35,630 If I run this program, I get you are old enough to drive 65 00:03:36,210 --> 00:03:38,283 and then I also get check check. 66 00:03:39,390 --> 00:03:43,440 But what if I say is_old is now false? 67 00:03:43,440 --> 00:03:45,270 What do you think will happen? 68 00:03:45,270 --> 00:03:49,533 If I run this, I get check check. 69 00:03:51,090 --> 00:03:54,240 Our Python interpreter is going to say, hey, 70 00:03:54,240 --> 00:03:56,880 set variable is_old to false. 71 00:03:56,880 --> 00:04:01,080 Hey, set is licensed equals to true 72 00:04:01,080 --> 00:04:02,700 and then it's going to go to line three. 73 00:04:02,700 --> 00:04:04,440 Nope, nothing there, I'm gonna keep going. 74 00:04:04,440 --> 00:04:05,273 Line four. 75 00:04:05,273 --> 00:04:07,947 Hey, if is alt, is this true? 76 00:04:09,390 --> 00:04:11,310 No, if old is false 77 00:04:11,310 --> 00:04:14,430 and the interpreter is going to say, okay 78 00:04:14,430 --> 00:04:18,540 I'm only supposed to run this piece of code if this is true. 79 00:04:18,540 --> 00:04:21,690 Because this is now false, 80 00:04:21,690 --> 00:04:25,170 I'm just going to completely ignore what's underneath here 81 00:04:25,170 --> 00:04:29,220 and just go to the next line that has, well, no indentation. 82 00:04:29,220 --> 00:04:31,980 So it's going to print, check check. 83 00:04:31,980 --> 00:04:35,340 And this is the power of conditionals. 84 00:04:35,340 --> 00:04:38,160 We're able to essentially skip lines. 85 00:04:38,160 --> 00:04:40,950 The interpreter doesn't even care what's in here 86 00:04:40,950 --> 00:04:43,740 because I just told my program to skip 87 00:04:43,740 --> 00:04:46,380 from line four to line six. 88 00:04:46,380 --> 00:04:47,673 How cool is that? 89 00:04:48,600 --> 00:04:52,680 So we learned that there's this if keyword 90 00:04:52,680 --> 00:04:57,000 but there's also another thing we can use called else. 91 00:04:57,000 --> 00:04:57,870 And you'll notice here 92 00:04:57,870 --> 00:04:59,883 that I didn't add it to the indentation. 93 00:05:01,080 --> 00:05:05,587 And else as the name suggests, simply says, hey 94 00:05:06,870 --> 00:05:08,850 if this something is true, 95 00:05:08,850 --> 00:05:13,850 then do this otherwise, also else do this. 96 00:05:14,370 --> 00:05:17,610 And again, you see that I've added the indentation. 97 00:05:17,610 --> 00:05:21,480 So try to guess what's about to happen in this program. 98 00:05:21,480 --> 00:05:22,983 If I click run, 99 00:05:24,120 --> 00:05:27,407 I get check check, because it's saying, hey is_old, 100 00:05:28,440 --> 00:05:29,610 is that true? 101 00:05:29,610 --> 00:05:30,780 Nope, it's not true. 102 00:05:30,780 --> 00:05:33,300 Okay, I'm going to completely ignore that. 103 00:05:33,300 --> 00:05:34,740 The interpreter sees else. 104 00:05:34,740 --> 00:05:36,150 Okay, well this wasn't true. 105 00:05:36,150 --> 00:05:40,680 So I'm going to run, whenever this evaluates false 106 00:05:40,680 --> 00:05:43,803 I'm going to always run print check check. 107 00:05:44,910 --> 00:05:48,933 What if I change is_old to now equal to true? 108 00:05:50,340 --> 00:05:51,720 What will happen next? 109 00:05:51,720 --> 00:05:55,563 Well, I get you are old enough to drive. 110 00:05:56,940 --> 00:06:01,940 Else only runs if the if block of code evaluates to false. 111 00:06:05,370 --> 00:06:06,783 Very, very cool. 112 00:06:08,010 --> 00:06:09,450 All right, let me ask you another question. 113 00:06:09,450 --> 00:06:11,830 What if I did print here 114 00:06:13,530 --> 00:06:18,153 and let's change this to you are not of age. 115 00:06:19,860 --> 00:06:24,750 If I run this, is that what you expected? 116 00:06:24,750 --> 00:06:27,090 Well, I hope by now you agree 117 00:06:27,090 --> 00:06:32,090 that this is the expected behavior because is_old is true. 118 00:06:32,310 --> 00:06:34,110 I'm going to print this 119 00:06:34,110 --> 00:06:36,780 and then Python interpreter is going to say, well, 120 00:06:36,780 --> 00:06:37,613 this was true 121 00:06:37,613 --> 00:06:40,200 so I'm going to completely ignore the else block 122 00:06:40,200 --> 00:06:41,760 and then just go to the next line. 123 00:06:41,760 --> 00:06:43,320 What's on line eight? Nothing. 124 00:06:43,320 --> 00:06:46,290 So I'm gonna go to line nine and I'm going to print, 125 00:06:46,290 --> 00:06:48,233 okay, okay, okay. 126 00:06:49,650 --> 00:06:54,060 As you can see, we're now controlling the flow 127 00:06:54,060 --> 00:06:56,220 of our programs where instead 128 00:06:56,220 --> 00:06:59,040 of going from one to nine, just line by line 129 00:06:59,040 --> 00:07:02,400 we're now saying at line four, do some sort of check 130 00:07:02,400 --> 00:07:05,730 and based on that, skip a few lines. 131 00:07:05,730 --> 00:07:07,560 Very, very cool. 132 00:07:07,560 --> 00:07:10,200 All right, there's one other thing I wanna show you. 133 00:07:10,200 --> 00:07:15,200 And it's the elif, and I know this kind of looks weird. 134 00:07:16,980 --> 00:07:20,583 You would think that it'll be else if, but no, no, no. 135 00:07:21,960 --> 00:07:26,670 It's elif and what do you think this does? 136 00:07:26,670 --> 00:07:29,520 Well, you use it in combination with if. 137 00:07:29,520 --> 00:07:31,383 You say if something, 138 00:07:32,340 --> 00:07:36,810 if otherwise else, if another condition 139 00:07:36,810 --> 00:07:41,810 so let's say is licensed, then do another condition. 140 00:07:42,810 --> 00:07:47,463 So let's say print, you can drive now. 141 00:07:49,260 --> 00:07:52,770 Hmm, so let's go through this again, I'm going to say 142 00:07:52,770 --> 00:07:56,010 is this person old enough? 143 00:07:56,010 --> 00:08:00,870 If that person is_old enough and this evaluates to true, 144 00:08:00,870 --> 00:08:04,560 well then I want you to run this. 145 00:08:04,560 --> 00:08:08,970 Otherwise, I want you to run this condition. 146 00:08:08,970 --> 00:08:11,700 Hey, is this true? 147 00:08:11,700 --> 00:08:12,533 No? 148 00:08:12,533 --> 00:08:15,180 If it's not true, then jump to else. 149 00:08:15,180 --> 00:08:18,003 So what will happen here if I click run? 150 00:08:18,870 --> 00:08:23,700 We get true for the first conditional block right over here. 151 00:08:23,700 --> 00:08:27,000 So automatically Python interpreter is going to say, well, 152 00:08:27,000 --> 00:08:29,400 we just got true here, so I'm going to ignore this 153 00:08:29,400 --> 00:08:34,400 and ignore this, and then print okay, okay. 154 00:08:34,440 --> 00:08:38,313 But let's say that is_old is now false. 155 00:08:39,600 --> 00:08:42,363 What will happen next if I click run? 156 00:08:44,640 --> 00:08:45,990 This is false. 157 00:08:45,990 --> 00:08:49,440 So Python interpreter is going to say, nope, not gonna care 158 00:08:49,440 --> 00:08:51,090 about this block. 159 00:08:51,090 --> 00:08:55,260 And I'm going to go, hey else if, a condition, 160 00:08:55,260 --> 00:08:57,660 hey, is this person licensed? 161 00:08:57,660 --> 00:08:59,130 Yep, they are. 162 00:08:59,130 --> 00:09:00,060 So I'm going to print 163 00:09:00,060 --> 00:09:02,763 you can drive now and ignore else. 164 00:09:03,750 --> 00:09:05,013 So it works like this. 165 00:09:06,090 --> 00:09:08,613 What if both of these were false? 166 00:09:09,960 --> 00:09:11,940 Well, as you can imagine, 167 00:09:11,940 --> 00:09:14,700 the Python interpreter is going to ignore. 168 00:09:14,700 --> 00:09:15,750 It's going to run this. 169 00:09:15,750 --> 00:09:19,500 Say, nope, this is false, this, nope, this is false. 170 00:09:19,500 --> 00:09:22,500 And then finally it's going to run this. 171 00:09:22,500 --> 00:09:24,840 You see, that else is a catchall. 172 00:09:24,840 --> 00:09:28,170 That is, if none of these conditions are true, 173 00:09:28,170 --> 00:09:30,270 then we're just going to run this. 174 00:09:30,270 --> 00:09:31,920 So it's a backup in a sense. 175 00:09:31,920 --> 00:09:35,313 Hey, if all things fail, Jen, just do this. 176 00:09:36,300 --> 00:09:38,670 Very, very cool. 177 00:09:38,670 --> 00:09:41,340 Now, if you look at this program, you're thinking, hmm, 178 00:09:41,340 --> 00:09:43,980 this program doesn't really work that well, does it? 179 00:09:43,980 --> 00:09:45,330 I mean, we're checking 180 00:09:45,330 --> 00:09:48,570 if the person is_old and if person is licensed. 181 00:09:48,570 --> 00:09:50,340 But shouldn't we check both? 182 00:09:50,340 --> 00:09:52,290 We want somebody who has a license 183 00:09:52,290 --> 00:09:54,630 and who's old enough to dry. 184 00:09:54,630 --> 00:09:57,630 Hmm, this is, this is a buggy program. 185 00:09:57,630 --> 00:10:00,480 If we implement this in a Tesla, well, 186 00:10:00,480 --> 00:10:02,310 we're gonna get a lot of lawsuits, right? 187 00:10:02,310 --> 00:10:05,820 Because we can get somebody who's maybe licensed 188 00:10:05,820 --> 00:10:08,370 but isn't old enough, which I guess doesn't make sense. 189 00:10:08,370 --> 00:10:12,510 But we can get somebody who is old enough 190 00:10:12,510 --> 00:10:14,940 but never got their driver's license 191 00:10:14,940 --> 00:10:17,130 and somehow we get access to the car 192 00:10:17,130 --> 00:10:20,460 and they can start the engine and drive and oh, 193 00:10:20,460 --> 00:10:21,900 that's a lawsuit waiting to happen. 194 00:10:21,900 --> 00:10:24,420 So how can we fix this, hmm? 195 00:10:24,420 --> 00:10:25,590 You know what? 196 00:10:25,590 --> 00:10:30,590 The power is that this is an expression, right? 197 00:10:31,290 --> 00:10:33,570 An expression, if you remember 198 00:10:33,570 --> 00:10:37,680 is something that produces a value and that means 199 00:10:37,680 --> 00:10:41,280 that this doesn't have to, you know, just have true here. 200 00:10:41,280 --> 00:10:42,630 It could be an expression. 201 00:10:42,630 --> 00:10:46,530 So I could say, if is_old and is_licensed, 202 00:10:52,500 --> 00:10:54,180 then I can do that. 203 00:10:54,180 --> 00:10:57,000 And this is something new that we haven't seen before. 204 00:10:57,000 --> 00:11:00,870 The end, this another key word in Python. 205 00:11:00,870 --> 00:11:03,240 And in an upcoming video, we're gonna talk about more 206 00:11:03,240 --> 00:11:07,290 of these keywords, but this should read like English, right? 207 00:11:07,290 --> 00:11:12,290 If is_old and is licensed, then do this. 208 00:11:12,750 --> 00:11:15,693 So we can now remove the elif. 209 00:11:17,160 --> 00:11:21,180 And then here, both expressions 210 00:11:21,180 --> 00:11:22,680 need to evaluate the true. 211 00:11:22,680 --> 00:11:27,540 That is whatever happens here, this has to evaluate the true 212 00:11:27,540 --> 00:11:30,060 and this has to evaluate the true 213 00:11:30,060 --> 00:11:35,060 and only when both are true, again, as and states 214 00:11:35,070 --> 00:11:40,070 then you're old enough to drive and you have a license. 215 00:11:41,130 --> 00:11:46,130 So that if I run this, well, I get an error, right? 216 00:11:46,786 --> 00:11:48,720 Or not an error, but it goes 217 00:11:48,720 --> 00:11:51,240 into the LS block because both of these are false. 218 00:11:51,240 --> 00:11:54,243 If only one of them is true and I click run, 219 00:11:55,950 --> 00:11:57,540 you are not of age, 220 00:11:57,540 --> 00:12:01,500 because both of these have to be true. 221 00:12:01,500 --> 00:12:06,060 If I click run, hey, all right, the car is starting, 222 00:12:06,060 --> 00:12:07,953 you can drive away with your Tesla. 223 00:12:09,450 --> 00:12:12,900 Okay, before I finish this video, because this is a lot, 224 00:12:12,900 --> 00:12:15,120 I wanna just note one thing. 225 00:12:15,120 --> 00:12:18,033 That is the elif statement. 226 00:12:18,990 --> 00:12:21,630 The elif statement is really, really useful. 227 00:12:21,630 --> 00:12:24,040 Because when you have code like this 228 00:12:24,930 --> 00:12:27,483 where you have an if statement and an L statement, 229 00:12:28,650 --> 00:12:31,230 usually you only see one of each. 230 00:12:31,230 --> 00:12:33,210 I mean, sure, in another part of the program 231 00:12:33,210 --> 00:12:36,240 I could have another if statement and an if statement 232 00:12:36,240 --> 00:12:39,540 with another else statement inside of here. 233 00:12:39,540 --> 00:12:43,470 But usually when you have this grouping together, 234 00:12:43,470 --> 00:12:46,020 you only see one if and one else, 235 00:12:46,020 --> 00:12:48,840 but you can have multiple elifs. 236 00:12:48,840 --> 00:12:52,440 So I can have elif another condition here, 237 00:12:52,440 --> 00:12:57,090 and then I can have another elif another condition here, 238 00:12:57,090 --> 00:12:59,910 and you can have as many as you want. 239 00:12:59,910 --> 00:13:03,570 Eventually the program's going to either evaluate 240 00:13:03,570 --> 00:13:05,130 one of these conditions to true, 241 00:13:05,130 --> 00:13:07,260 or it's going to go into the else block. 242 00:13:07,260 --> 00:13:09,693 But usually you follow this order. 243 00:13:10,950 --> 00:13:13,050 All right, that was a lot. 244 00:13:13,050 --> 00:13:15,960 Let's take a break and explore this topic a little bit more. 245 00:13:15,960 --> 00:13:17,860 I'll see you in the next one, bye-bye.