Let's talk about conditional logic.
An important concept.
All over programming.
What do I mean by conditional logic.
We've learned about bullying.
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 billions 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 a 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 read on 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 witch 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 call in and then you'll see here that the indentation is different.
As a matter of fact when I press enter after the colon 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 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 rep you see over here I get a little minus sign.
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 check our Python interpreter is going to say hey.
Set Variable is old to false Hey set is licensed equals too true.
And then it's going to go to line three.
No nothing there.
I'm going to keep going.
Line four.
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 for two lines six.
How cool is that so we learnt 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 checked check because it's saying hey is old is that true.
No it's not true.
Okay I'm going to completely ignore that.
The interpreter sees else.
Okay.
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 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 8.
Nothing.
So I'm going to go to line 9 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.
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 jumped 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.
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 gonna 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 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 backup in a sense.
Hey if all things fail Jane 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 person is licensed but shouldn't we check both.
We want somebody who has a license and who's old enough to dry.
Mm hmm.
This is this is a buggy program.
If we implement this in a Tesla Well we're gonna 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.
Mm hmm.
You know what.
The power is that this is an expression right.
An expression if you remember is something that produces a value end up 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 the now coming video we're gonna talk about more of these keywords but this should read like
English right.
If is old and is licensed then do this so it can now remove the L if 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 an unknown 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 l 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 F and one else but you can have multiple
l FS so I can have L if another condition here and then I can have another L if another condition here
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 in the next one public.