Let's take a look at multiple comparison operators
within an 'if' statement.
So in the last two videos I said, if something is True, do a thing.
So I am just going to write 'pass', because indentation is important.
If we just wrote 'if True', we're going to see 'SyntaxError',
so we just write 'pass'
if we don't want anything to happen. And this will evaluate.
Python will say, "Yep, this works.
So I'm going to execute whatever code is indented in here".
But what if we want to look up two things at the same time?
What we could do, 'course = "Python for Everybody"',
and we could also set the 'student', name '= "Ezra"'.
And so now we can compare these.
We can say 'if course == "Python for Everybody"',
and then we can nest it in here and say 'if student ==
"Ezra": print(
"Welcome to p4e,
Ezra")' And it works because 'course' is "Python for Everybody",
'student' is "Ezra". We have now successfully nested an 'if' statement,
but we can actually merge these together.
This is really easy, too.
So if we just pull this up to this line here and write the
word, 'and', so 'if course == "Python for Everybody"
and if student = "Ezra": "Welcome to the course".
I'll just change the text just to prove that this will work.
And yep, I made a syntax error.
And let's run that. There we go.
So you don't need two 'if' statements in there, that's actually,
I'm glad that one came up.
So it's just 'if' your statement, and some other statement, is
True. Now what this is a lot like saying, is saying, 'if True
and True, do a thing, and we can actually 'print("Do a thing")'
just to make sure that this is working.
So this one is True, and this statement here is True, and
that 'and' operator is saying that both of them need to be True.
Now we can actually swap this out entirely.
We can say that only one of them needs to be True. With the
'and' statement they both need to be True.
And you can put as many 'and' statements together as you want.
But if you only wanted one of these to be True, you could
say either the 'course' is "Python for Everybody", or the 'student'
is "Ezra", "Welcome to the course,
Ezra". It's not necessarily what we're looking for anymore, because
the 'student' might not be "Ezra", they just might be in the
Python course.
So say, "You are either in Python for Everybody, or you are
Ezra". Okay. Now let's go ahead and change this.
Let's change this to something that we know is going to evaluate as False.
But Ezra is still going to stay the same, and that still
works. And if we get rid of this, it doesn't work.
And so what this statement is saying is if something is True,
or something else is True, just one of them needs to match.
And I was going to undo that. Rerun that.
Alright, let's check out a more complicated type of 'if' statements in here.
So this 'if' statement is going to look for one of two things
or something else.
Actually, that's wrong.
I explained that wrong.
It's going to look for a True statement and a True statement,
or another True statement.
And so we can actually split these out by wrapping parentheses
around these. So this is going to get a little bit wild,
and I'll do my best to explain this,
and hopefully this turns out pretty well in the video.
So we've got 'course', 'student', and let's put a 'grade' in here.
So let's just say the 'grade' is going to be, I don't know, grade 12.
So now we can say 'if', with parentheses in here 'student
== "Ezra" and 'course == "Python for everybody")
or grade == 12: print("Success")'.
And sure enough, this is going to show up.
Now, that's because all of these are True. This is True and
this is True.
And because these are together in parentheses, they both
have to be True.
If one fails, this whole thing fails, everything inside the
parentheses fails.
That comes back as False.
And even if that is False, it's going to say, 'or grade == 12'.
So this is a lot like saying,
[no audio]
'if (True
[no audio]
and True) or, 'grade
== 12', True.
So here's the thing.
One of these can fail.
[no audio]
If there is a True and False, this one, it's gone.
Python says, "Get rid of it,
but let's see if there is an or statement in there".
And if there was an 'or' statement, which was True, this is
going to boil down to 'print("Success")'.
So let's just undo that, and change a few variables in here.
Let's change "Ezra" to nothing, "".
So we know that this is going to be False.
This one here is going to be False, but this is still going
to be True, and 'grade == 12', is going to be 'True'.
So let's rerun each of these cells, still works.
'If grade = 11', let's go ahead and change.
This no longer works.
Nothing shows up in there.
And that's because one of these was False, and this one was
False. So neither this one nor this grouped 'if' statement was
True, and because nothing in there was True, when it came to
this 'or' statement, this code was not executed.
Now that's a little more complicated.
You don't really have to fully understand how that works right now
If you're brand new to programming, that's going to be a
little bit confusing.
But you are going to see things like this in the wild.
But over time it does get a little bit easier to understand
this. Now when these get really, really, really big, that
gets very complicated.
And we use a lot more nesting with just simple parentheses.
So just go ahead,
and there we go.
I just undid my work there.
So that's how we work with multiple comparison operators
when it comes to the 'or' statement, or the 'and' statement, we
can do either one.
And in here we can group these together to make sure that
both of these need to be True.
If one of them is not True, this whole thing results in False,
and then it looks for 'if grade == 12', 'if grade == 12',
then it would be True.
But actually, it would look like that.
Now it's saying 'if False or True',
and if we just run this in our interpreter, we can say True
or False, comes back as True.
False or true, also comes back as True,
because True is in there with an 'or' statement
it always comes back.
We can do the opposite as well with an 'and' statement.
So 'True and False', one of them is not True,
it turns out False.
We can do the opposite as well.
False and True, again,
one of those is False,
it's going to return False.
It's always looking for Tuue all the way through.
Hey, that rhymed. I'm going to start using that.
It's always looking for True all the way through.
So I'm going to undo that.
And that is multiple comparison operators.
So when you're learning this honestly, just start small.
Say if one thing is something and another thing is something,
and then you can make it a little more complicated.
And you can say if something is something and something else
is not something.
But for now, like always, take it slow, take it easy,
and over time, this just becomes second nature.