Now, up to this point you've seen us use if statements,
else statements, elif statements, multiple if statements as well as nested if statements,
but the thing that we haven't been able to do thus far is to check for multiple conditions in the same
line of code.
So how would we be able to combine different conditions and say, well, is the pizza large, and the
user wants pepperoni, and extra cheese all in the same line of code.
Well, to do this, we would need to learn about the logical operators.
So there's three of them that are really useful: "and", "or", and "not".
So let's take a look at the first one.
When you combine two different conditions using an and operator they both have to be True both A and
B for the entire line of code to be True.
If just one of them is True, say A is True and B is False, or A is False and B is True, then the
overall thing evaluates to False.
Now let's try out some of that in actual code.
So let's go ahead and go into the Python Console that we see, right here.
And here we can write Python code from scratch,
so completely independent of everything else and independent of our code files.
So let's test out some Python code in here.
Let's say that, a = 12.
Right? Now, let's say that I wanted to check whether if is a > 15?
Well, that is going to be False.
Now what about is a > 10?
Well, that's going to be True.
Now, if I combined this using an "and" statement, I could say, well, is a > 10 and a < 13
and hit Enter, then I would get, True, because both a is greater than ten and a is less
than 13 are True.
So in this case, when both conditions are true, then this entire line gets evaluated to True.
But see what happens when just one of them is False.
So is a > 15 and a < 13,
a is greater than 15 is False, a is less than 13 is True,
and now we get False
.
So that's what happens when you combine different conditions using and.
So using the "and" logical operator it looks at two conditions,
and to see if they are both True.
And if they are both True it becomes True as the final outcome.
But if either of these are False, so False and True becomes False.
True and False also becomes False.
And this is how the and logical operator works.
Now if you only needed one of the conditions to be True, then you could use the "or" operator instead.
So if C or D were True, or if they're both True, then it will evaluate to True.
It's only when both C and D are False, does this statement actually become False.
Now the final one is the "not" operator.
And all that this does is it basically reverses a condition.
So if the condition is False, then it becomes True.
If it's True, then it becomes False.
So coming back to our code, we can refresh and restart the Python Console and start with the same a = 12
.
And we can use the or statement by saying, well, is a > 10.
That's True,
or is a < 10, which is not True.
So here we have a condition that is True and a condition that's not True,
but because this time instead of using an and, we have an or, between these two conditions, then this
will actually evaluate to True.
So in this case, using the or True or True will become True.
True or False will become True.
False or True becomes true and it's only False or False when both of the conditions are False does it
actually evaluate to False.
So this is quite different from the "and" operator.
Now finally with the "not" operator, it only works when the condition that's being checked is False.
So effectively it flips the True to the False and False to True.
Let me show you what I mean.
So we know that a is 12, so is a < 0?
Well it's not because it's 12.
It's definitely not less than zero.
But by putting the not in front of it, it reverses the condition.
So a < 0,
this will evaluate to False, but putting the not in front of it it becomes True.
So not False is True, and not True is False.
Coming back to our roller coaster ticketing, let's say that the roller coaster company decided that
for everybody who is having a midlife crisis, they would give them free tickets.
And according to Wikipedia, midlife crises typically occur when you are 45 to 55 years old.
Let's see if we can incorporate this into our code.
So let's say that in addition to these existing price categories, what if you had to add a separate
price category for those people who are aged between 45 and 55, and those people get to ride for free?
Do you think you would be able to change the code using what you've learned about logical operators,
in order to incorporate this addition to our program?
Pause the video and give that a go.
So we've currently got three conditions:
age less than 12,
age between 12 and 18,
and finally everybody else.
Now instead of just finishing up there, let's go ahead and add another elif.
And here we're going to combine two conditions.
We're going to say if the age >= 45 and age <= 55,
then we get to catch that midlife crisis window.
Well, in this case, we're going to print something like...we're going to say everything's going to be
okay, have a free ride on us.
And in fact, we don't need to modify the bill in any way because we know that with if, elif, and else
statements is that once this condition matches, then everything that's inside this block.
So everything that's indented inside this elif is going to be carried out, namely printing this out,
and then it's going to skip the rest of this if else block and continue on.
Now, if they want a photo, they still have to pay $3, but at least their ticket is free.
And we've been able to do this because we know about the "and" logical operator.
Now, for the keen eyed amongst you, you might have noticed with this condition check, we have a warning
underneath it.
And this says that there's actually a simpler way of writing this expression, and it tells us that
we can actually simplify it. Now because we're checking two conditions and we have the "and" logical operator
in between,
sometimes you might want to make this code less wordy.
So if you hover over this warning and you click on Simplified chained comparison, it will give you a
simpler way of doing this comparison using two of these checks, either side of the age,
and it does exactly the same thing, but in less words of code.
But here is the big but, looking at this, is this easier to understand and logic through versus this?
Because to somebody who's just starting Python programming, I think this is easier to understand
what's going on. Is age greater or equal to 45,
and is age less than or equal to 55?
So even though there is this warning in here, I'm going to leave it in this format because a lot of
you are just starting out learning how to code, and this is a much easier way of reasoning about what's
going on in this line of code.
So the thing with warnings are that they're really helpful once you get going as a programmer,
but in the learning stage, I wouldn't worry too much about all of it.
Look at it, read it, see if you understand what it's trying to tell you, and then you decide which
version you prefer.