Instructor: 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_old
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_licensed
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 wanna 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, it 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 column, and then
you'll see here that the indentation is different.
As a matter of fact, when I press enter after the column,
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 if block.
And if you're using the repl, 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 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 to true
and then it's going to go to line three.
Nope, nothing there, I'm gonna keep going.
Line four.
Hey, if is alt, is this true?
No, if old is false
and the interpreter is going to say, okay
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's 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.
Okay, well this wasn't true.
So I'm going to run, whenever this evaluates 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 gonna go to line nine and I'm going to print,
okay, okay, okay.
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 at 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 wanna show you.
And it's the elif, and I know this kind of looks weird.
You would think that it'll be else if, but no, no, no.
It's elif 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.
Hmm, 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 gonna 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 catchall.
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, Jen, 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 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 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 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 another key word in Python.
And in an upcoming 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 we can now remove the elif.
And then here, both expressions
need to evaluate the true.
That is whatever happens here, this has to evaluate the true
and this has to evaluate the 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, right?
Or not an error, but it goes
into the LS 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 wanna just note one thing.
That is the elif statement.
The elif 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 elifs.
So I can have elif another condition here,
and then I can have another elif another condition here,
and you can have as many as you want.
Eventually the program's 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.