‫Let's talk about conditional logic, an important concept all over programming.
‫What do I mean by conditional logic?
‫We've learned about booleans, right?
‫We have true.
‫And we have false.
‫And some of you might be wondering why they're so useful.
‫And when it comes to conditional logic, booleans are really, really important.
‫For example, let's say we create a variable is alt.
‫And you know what?
‫We're going to create a car that automatically detects if you can start the engine.
‫Maybe if you're not old enough, the engine won't start and it won't let you drive so that they make
‫sure that this car is super safe and only people that are perhaps over 18 can drive it.
‫So how can we code something like that?
‫Let's assume Tesla has a new feature like this.
‫Well, we'll have something like is old and we'll set this to.
‫True.
‫Right.
‫Maybe we have something else.
‫Maybe it checks the license to make sure that we have a license so we can say is license.
‫And then we can set it to true.
‫Now these could change true and false, depending on the user and the driver.
‫But when I talk about conditionals, this is what I mean.
‫In Python we can use the if keyword to say if some condition exists and this is just me writing it.
‫It doesn't exist.
‫You can see that I get a red underline with invalid syntax.
‫But here we want to say a condition that evaluates to true or false.
‫In our case, we can say, hey, if is old, which what does it evaluate to well, evaluates to true.
‫If that's true, then let's say print.
‫You are old enough to drive.
‫Now there's a few new things here.
‫One, I've added a semicolon or a colon.
‫And then you'll see here that the indentation is different.
‫As a matter of fact, when I press enter.
‫After the Cullen, you see that it automatically gives me a space.
‫This tells the Python interpreter I'm going to do an if statement, a conditional operation.
‫And if this happens to be true.
‫Run everything that's inside of here that has this indentation.
‫And if I do something without the indentation, let's say print.
‫Check.
‫This to the Python interpreter is like a completely new line.
‫It is not part of this F block.
‫And if you're using the ripple you see over here, I get a little minus sign if I click this.
‫You see how it hides.
‫Well, what it does is it's saying, hey, this is a code block over here.
‫It's an entire thing in itself.
‫But anything outside that is not indented well doesn't belong to it.
‫So to a Python interpreter, it's going to say, I have this line and then I have this line.
‫Let me show you what I mean if I run this program.
‫I get you are old enough to drive.
‫And then I also get check.
‫Check.
‫But what if I say is old is now false?
‫What do you think will happen?
‫If I run this.
‫I get?
‫Check.
‫Our Python interpreter is going to say, hey, set variable result to false hey set is licensed equals
‫to true.
‫And then it's going to go to line three.
‫No, nothing there.
‫I'm going to keep going line for.
‫Hey, if is old, is this true?
‫No.
‫If all this false and the interpreter is going to say, och, I'm only supposed to run this piece of
‫code if this is true because this is now false, I'm just going to completely ignore what's underneath
‫here and just go to the next line that has, well, no indentation.
‫So it's going to print, check, check.
‫And this is the power of conditionals.
‫We're able to essentially skip lines.
‫The interpreter doesn't even care what's in here because I just told my program to skip from line four
‫to line six.
‫How cool is that?
‫So we learned that there is this if keyword, but there's also another thing we can use called else.
‫And you'll notice here that I didn't add it to the indentation.
‫And else, as the name suggests, simply says.
‫Hey, if this something is true, then do this.
‫Otherwise also else do this.
‫And again, you see that I've added the indentation.
‫So try to guess what's about to happen in this program.
‫If I click Run.
‫I get check.
‫Check because it's saying, hey, is old.
‫Is that true?
‫Nope, it's not true.
‫Okay.
‫I'm going to completely ignore that the interpreter sees else.
‫Well, this wasn't true.
‫So I'm going to run.
‫Whenever this evaluates to false, I'm going to always run.
‫Print, check.
‫Check.
‫What if I change?
‫Is old to now equal to true?
‫What will happen next?
‫Well.
‫I get?
‫You are old enough to drive.
‫Else.
‫Only runs if the f block of code evaluates to false.
‫Very, very cool.
‫All right.
‫Let me ask you another question.
‫What if I did print here?
‫And let's change this to you are not of age.
‫If I run this.
‫Is that what you expected?
‫Well, I hope I now you agree that this is the expected behavior because is old is true.
‫I'm going to print this and then Python interpreter is going to say, well, this was true, so I'm
‫going to completely ignore the block and then just go to the next line.
‫What's on line eight?
‫Nothing.
‫So I'm going to go to line nine and I'm going to print OC OC OC.
‫As you can see, we're now controlling the flow of our programs where instead of going from 1 to 9,
‫just line by line, we're now saying a line four, do some sort of check.
‫And based on that, skip a few lines.
‫Very, very cool.
‫All right.
‫There's one other thing I want to show you.
‫And it's the L.
‫If.
‫And I know this kind of looks weird.
‫You would think that it will be else if, but.
‫No, no, no.
‫It's l.
‫F.
‫And what do you think this does?
‫Well, you use it in combination with if you say if something.
‫If.
‫Otherwise.
‫Else.
‫If.
‫Another condition.
‫So, let's say.
‫Is licensed.
‫Then do another condition.
‫So let's say print.
‫You can drive now.
‫So let's go through this again.
‫I'm going to say, is this person old enough?
‫If that person is old enough and this evaluates to true.
‫Well, then I want you to.
‫Run this.
‫Otherwise.
‫I want you to run this condition.
‫Hey, is this true?
‫No.
‫If it's not true, then jump to else.
‫So what will happen here if I click Run?
‫We get true for the first conditional block right over here.
‫So automatically Python interpreter is going to say, Well, we just got true here, so I'm going to
‫ignore this and ignore this and then print.
‫Okay, okay.
‫But let's say that is old is now false.
‫What will happen next?
‫If I click Run.
‫This is false.
‫So Python interpreter is going to say, nope, not going to care about this block and I'm going to go,
‫hey, else if a condition hey, is this person licensed?
‫Yep, they are.
‫So I'm going to print.
‫You can drive now and ignore else.
‫So it works like this.
‫What if both of these were false?
‫Well, as you can imagine, the Python interpreter is going to ignore it's going to run this say, nope,
‫this is false, this nope, this is false.
‫And then finally it's going to run this.
‫You see, that else is a catch.
‫All that is, if none of these conditions are true, then we're just going to run this.
‫So it's a backup in a sense.
‫Hey, if all things fail, then just do this.
‫Very, very cool.
‫Now, if you look at this program, you're thinking, hmm, this program doesn't really work that well,
‫does it?
‫I mean, we're checking if the person is old and if person is licensed, but shouldn't we check both?
‫We want somebody who has a license and who's old enough to dry.
‫Hmm.
‫This is.
‫This is a buggy program.
‫If we implement this in a Tesla, well, we're going to get a lot of lawsuits, right?
‫Because we can get somebody who's maybe licensed but isn't old enough, which I guess doesn't make sense.
‫But we can get somebody who is old enough but never got their driver's license and somehow we get access
‫to the car and they can start the engine and drive and.
‫Oh, that's a that's a lawsuit waiting to happen.
‫So how can we fix this?
‫Hmm.
‫You know what the power is?
‫That this is an expression, right?
‫An expression, if you remember, is something that produces a value.
‫And that that means that this doesn't have to you know, just have true here it could be an expression
‫so I could say if is.
‫Old and is licensed.
‫Then I can do that.
‫And this is something new that we haven't seen before.
‫The end.
‫This is another keyword in Python.
‫And in an upcoming video, we're going to talk about more of these keywords.
‫But this should read like English, right?
‫If is old and is licensed, then do this.
‫So we can now remove the LEF.
‫And then here.
‫Both expressions need to evaluate the true.
‫That is, whatever happens here.
‫This has to evaluate to true.
‫And this has to evaluate to true.
‫And only when both are true again as end states, then you're old enough to drive and you have a license.
‫So that if I run this.
‫Well I get an error rate because or not an error, but it goes into the block because both of these
‫are false.
‫If only one of them is true and I click run.
‫You are not of age because both of these have to be true.
‫If I click Run.
‫Hey.
‫All right, the car is starting.
‫You can drive away with your Tesla.
‫Okay.
‫Before I finish this video, because this is a lot, I want to just note one thing that is the l if
‫statement.
‫The live statement is really, really useful because when you have code like this where you have an
‫if statement and an L statement.
‫Usually you only see one of each.
‫I mean, sure, in another part of the program, I could have another if statement and an if statement
‫with another else statement inside of here.
‫But usually when you have this grouping together, you only see one if and one else, but you can have
‫multiple FS so I can have lef another condition here and then I can have another lef another condition
‫here and you can have as many as you want.
‫Eventually the program is going to either evaluate one of these conditions to true or it's going to
‫go into the else.
‫BLOCK But usually you follow this order.
‫All right.
‫That was a lot.
‫Let's take a break and explore this topic a little bit more.
‫I'll see you in the next one.
‫Bye bye.