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