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 called.
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 the.
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, this 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 key word to say if some condition exists.
And this is just me writing it, it doesn't exist.
So you can see that I get a read and 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 this is old, which what does it evaluate too 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 Callon, 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.
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, fight, 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.
Our Python interpreter is going to say, hey, set variable is old to false, hey, that is licensed
equals to true and then it's going to go to line three.
No, nothing there.
I'm going to keep going like for hey, if is old.
Is this true.
No.
If all this false and the interpreter is going to say, OK, 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 did and added 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?
No, it's not true.
OK, I'm going to completely ignore that the interpreter sees else.
OK, 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 if 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 by 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 else 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.
OK, ok.
OK.
As you can see, we're now controlling the flow of our programs where instead of going from one to nine,
just line by line, we're now saying a line for 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 if.
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 through here, so I'm going to ignore this and ignore this and then
print, OK?
OK.
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 I condition Hey, is this person licensed?
Yep, they are.
So I'm going to print.
You can drive now and ignore or 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 no,
this is false.
This no, 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 back up 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 this program doesn't really work that well, does
it?
I mean, we're checking if the person is old and if a 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 in.
Oh, that's a that's a lawsuit waiting to happen.
So how can we fix this?
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 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
key word in Python.
And in an upcoming video, we're going to talk about more of these keywords.
But this should be like English, right?
If is old and is licensed, then do this so we can now remove the L.F..
And then here.
Both expressions need to evaluate to 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 and 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 else 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.
OK, 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 a code like this where you have an
IF statement and an El 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 false statement inside of you.
But usually when you have this grouping together, you only see one F and one else, but you can have
multiple LFS.
So I can have L.F. another condition here and then I can have another L.F., another condition here
and you can have as many as you want.
Eventually the 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 in the next one, Pabai.