Adding Some Logic to Your Code
00:04 You’re trying to find out whether something is true or false in Python. Now, how do you do that? Conditional logic allows you to check if something is true or false, so it involves questions like Does Bobby have more apples than Bill? Is Jane faster than June? Is 5 larger than 8? Well, that’s something that we know to be false. Is n before p in the alphabet? That’s something we know to be true.
Basically, when you’re writing conditional logic in Python, you’re looking to reduce some values into a
False, and you usually do that by comparing them and combining them with other statements.
00:43 In this lesson, you’re going to cover a few operators and how they fit together: Boolean comparators, conditional statements, logical operators, and operator precedence—that is, in what order things get resolved. In the same way that mathematical operators have a certain precedence—multiplication happens before addition, for instance—Boolean comparators and logical operators also have a precedence.
The greater than operator (
>), a chevron facing right, asks whether the value on the left is greater than the value on the right. The less than operator (
<), which is a left-facing chevron, asks whether the value on the left is less than the value on the right.
<= operators are similar in that they do the same thing as the
< operators, except if the values are equal, they will also evaluate to
True, whereas the simpler counterparts will actually evaluate to
False if the values are equal. The not equal to operator (
!=), which is an exclamation mark and an equal sign, is just asking whether the values are not equal. So if they are equal, that will give you a
I’m going to read out what characters they are. So the first example will be
10 > 5. But when you’re actually reading code, if you were to read it out loud, you wouldn’t say 10 right- facing chevron 5. You would say 10 greater than 5.
But after these examples, I’m going to start calling them by their real name. So
10 > 5 asks is 10 greater than 5, which is true.
1 < 2 asks is 1 less than 2, which is also true. Remember, all of these examples on this page will evaluate to
1 is equal to 0. Is that a true statement or a false statement?
100 != 100 asks is 100 not equal to 100, which is false. All right, so from here on out, I’m not going to talk about chevrons, equal signs, or exclamation points.
Now that you’ve had a look at Boolean comparators, now it’s time to add logical operators to the mix. There are three logical operators in Python: the
and operator, the
or operator, and the
not operator. The
and operator and the
or operator require values on either side of the operator.
not operator only requires a value on the right side of it. The values that the logical operators work on will be evaluated as Boolean values—that is, it doesn’t matter what you put on either side of the
It will always try to see the values as
False. So it’s like asking, Is it dinner time? and Am I hungry? On either side of the
and keyword, you have questions that can evaluate
not only needs an expression on the right-hand side, and basically it will just invert it. So if you have
not and an expression, and that expression evalues to
True, then the total result of everything will be
So now you’re going to take a look at a couple examples of some logical operators in action. Say you had an expression of
1 > 2, then you added the
and logical operator and then had another expression on the right side of it, that
5 > 3.
So you have a false statement and a true statement on either side of the
and operator. That gives you
False because for the
and operator, both sides need to be true. Take a look at another example.
That’s a false statement. And then you have
10 != 9, which is a true statement. So what will the whole thing give you? It gives you
True because with the
or operator, either side needs to be true.
and then add a
not in front of the whole expression? What do you think this will give you? Now, you might be tempted to think that this would give you
False, but in fact, the
not operator is operating on this expression (
"Python" == "Pythonic") before the whole
or expression is evaluated.
not keyword is operating on the expression of
"Python" == "Pythonic" before the
or operator operates on either side, so before the
or operator gets a chance to evaluate,
not "Python" ==
"Pythonic" evaluates to
10 != 9 evaluates to
True as well. Since
or, either side or both sides can be true, it gives you
True. You get a
True value. Now, the way that
not operates first before
or is to do with operator precedence.
Some parts will evaluate before other parts. The Boolean comparators are the first to evaluate. After, it’s
and, and finally
or. You can work around this behavior if it’s not convenient for your use case by using brackets. Whatever’s in brackets will evaluate first.
So if you run into any strange bugs and the expression is not behaving as you think it should, make sure it’s not to do with the operator precedence. Break up your statement into small pieces and see if it still works there. All right, in this lesson, you’ve covered Boolean comparators, conditional statements, logical operators, and operator precedence. Remember, this is all just about taking values, comparing them with each other, and getting a
False value at the end of it. Now, you might be wondering about the precedence between the Boolean comparators. That wasn’t mentioned in the previous slide. They were just listed all on one line.
Chaining Boolean comparators in this way can be useful for ranges. You usually don’t use chained Boolean comparators, opposing each other like this. A statement like that could easily just be written
50 > 20 because checking if 50 is greater than 10 is redundant in that case.
And therefore, the Boolean comparators don’t have any precedence between them because there is an implicit
and inserted between them. So no matter what, there’s never any ambiguity as to which Boolean comparator to evaluate first, because they are separated by
and keywords, so they all operate independently, and then the
and can evaluate.
> "aaz", what do you think that would give you? That would give you
False because it looks at the first letter, which is
"a", so they’re equivalent, looks at the second letter,
"a", which is equivalent. Then it looks at the third letter, and since
"a" has a smaller value than
"z" does, that gives you
Become a Member to join the conversation.