If by now you've been following through the last few videos
of basically learning about comparison operators and multiple
comparison operators together, you might have already run
into a particular issue where you could say, 'name = "Kalob"',
and then what do you do if you're looking for a specific
'name', or what do you do if you're looking for a specific
'name', and nothing else matches?
So right now, we could do 'if name == "Kalob": print(
"Name is Kalob")'
But what if the 'name' is not "Kalob"?
Then we would say, 'if name == "Henry": print("Name is Henry")'.
But what if 'name' is not "Kalob" or "Henry"?
Then we could say, "if name == "Zephyr": print("Name is Zephyr")'.
And let's run that. It says, "Name is Kalob".
And if it is "Zephyr", then cool,
it's going to show up as "Name is Zephyr".
But what if it's none of those names?
What if the name is "Ezra"?
Nothing happens.
What if we wanted to catch that,
or what if we want our program to not have to evaluate every
single one of these lines?
We just want it to match one.
So if the 'name = "Henry"', well, it should only match this one,
but still it's going to try to match this one,
it's going to try to match this one,
and even if it does find a match, it's going to find
this ones. We've actually run into two problems here, where it's
going to continue to execute these 'if' statements, even though
they are sort of related, and we don't want them to be.
And if it does not match "Kalob", "Henry", or "Zephyr", what do we do?
So, I'll just rerun those, and we'll say the 'name = "Henry"'.
Now, when we get two chaining operators together, we have
these things called 'if-else' statements, or 'else-if' statements.
So we can say 'if name == "Kalob": print("Kalob")'.
We can also say 'elif name == "Henry": print("Meow")',
because that's one of my cats.
'elif name == "Zephyr": print("Little meow")', because
he has a tiny little meow.
He's got a tiny voice. And we're going to see that it just says "Meow".
Now, the nice thing about this is Python is going to try
to evaluate this and says, "You know what,
the name was not Kalob,
the name is Henry", as we can see here.
Okay, so it's going to execute that one, and then it gets to the 'elif',
and it says, by the way, 'elif' is short for 'else-if' in Python.
So the 'elif' is saying, "If the name is then equal to "Henry",
print me out". Now, this turned out to be True.
The name was "Henry".
And so Python said, "Oh, okay, the name was Henry.
I do not need to execute this.
I don't need to check it".
Now, Python is very fast when it comes to checking 'if' statements.
So like, you're not going to get massive performance boosts,
but when you are writing code that is connected, this is
a lot cleaner, because now if I wrote this and another Dev
came in and said, "Oh, what is this looking for?"
They could say it's looking for "Kalob", or it's looking for
"Henry", or it's looking for "Zephyr".
We're up here.
It's looking for 'name' is "Kalob",
'name' could also be "Henry",
'name' could also be "Zephyr".
There is a little bit of a syntactical difference there,
but it is also important just for reading the code.
Now, what happens if the name was not "Kalob", "Henry", or "Zephyr"?
What if it was any other name?
What we could add in here, an 'else' statement, and an 'else'
statement does not take a comparison operator.
It just says if it is something cool, if it's not, if it
is literally anything else, execute this code. It will say,
print in here with an 'f""' statement.
'print(f" The name is ")'.
Now this still result in "Meow", because I have to go and change that
'name', and I'm not going to change it up here because I
like this example, but I am going to overwrite it right here.
'name = "Ted"'.
And let's go ahead and run this.
It says, "The name is Ted".
[no audio]
"Kalob". Yep, shows up as, "Kalob". "Zephyr", it's "Little meow" because he's got
a little meow or 'name' is "Literally anything else in the entire
world". That does not match "Kalob", "Henry", or "Zephyr".
Just go ahead and run this.
And it says, "The name is Literally anything else in the entire world".
Now, you don't actually need these 'elif' statements either.
You could say 'name =', let's not do 'name',
let's do 'age = 25',
and you could say, 'if age < 21':
print("Nope, not allowed to have alcohols")'.
Otherwise, if you are older than 21, 'print("Welcome to the bar,
what are you drinking tonight?")'
[no audio]
And just like that, Python is going to say, "Okay, the age is 25.
if age", which looks like this, "if 25 < 21",
Nope, it is not.
So it's not going to execute this.
It just says, "I'm going to get here.
Nope. That's False.
If False, no". Comparison operator is always looking for True.
So it skips over this and then goes straight to the 'else' statement,
and the 'else' statement says, if it is literally any other answer,
doesn't matter what the answer is, it is the opposite of
this. And so to catch any sort of special cases, we use an
'elif' statement.
But if you're just looking for something simple, like if
you're allowed to drink alcohol or if you're allowed to vote,
all you need is an 'if-else' statement in here.
Now you're going to be writing a lot of these
in, well every programming language. Python is no different in that
case, just because this is how you tell a program what to do.
If something is something, do something, if it is not
do something else.
And programs are made up of hundreds, thousands, millions,
maybe even billions of 'if' statements, depending how big the
program is.
Google probably has a good example of billions of 'if' statements
in all of their source code.
But what I'd like you to do as quick little hands on example,
is I want you to set a number, check if that number is a certain
number. So I'm going to let you write your own 'if' statement
here, and also write an 'else' statement,
and then I want you to change your variable number so that
the first statement is triggered, and then the second statement
is triggered, but not at the same time.
So one should run, and then you change your variable, and
the other one should then run.
Go ahead and give that a shot,
and when you're done let's head on over to that next lesson.