Adding Some Logic to Your Code
00:00 Add 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.
01:09 Boolean comparators are some of the most common operators you’ll use for comparing values. All these operators require an item on the left and the right, and they compare the values.
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.
01:38 A nice way to remember these is to look at the chevron as a mouth, and whatever the mouth is trying to eat should be the larger of the two values for that expression to evaluate to true.
The greater than or equal to operator (
>=) is a right-facing chevron with an equal sign. The less than or equal to operator (
<=) is a left-facing chevron and an equal sign.
<= 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
The counterpart to that is the equal to operator (
==), which is two equal signs. The
== operator will only give you a
True value if the values on either side are equal to each other.
02:45 It can be difficult to grasp what these operators are really doing by looking at them in isolation like this. So coming up, you’re going to be looking at some examples.
All the following conditional statements will result in a
True value. For these examples, I’m not going to call the operators by their name.
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
10 >= 9 asks is 10 greater than or equal to 9, which is true. The next example is
5 <= 5, which asks is 5 less than or equal to 5, which is true.
1 != 0 asks is 1 not equal to 0, which is also true. And finally,
100 == 100 asks is 100 equal to 100, which is true again.
Now you’ve seen a bunch of conditional statements, all of which evaluate to
True. Now it’s time to look at some that will evaluate to
The first example is
10 < 5, which asks is 10 less than 5, which is false. The next example is
1 > 2, which asks is 1 greater than 2, which is false.
10 <= 9 asks is 10 less than or equal to 9, which again, false. All of these examples are false.
4 >= 5. Is 4 greater than or equal to 5? False.
1 == 0.
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.
05:33 I’m just going to call the conditional operators by their name.
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
So in the case of
and, if both are true, then you’ll have dinner. On the subject of
and, that’s the first keyword that you’ll be taking a look at.
So if you have a statement with which the left side or the right side evaluates to
True, and one of them evaluates to
False, then that will give you a
If both the left and right sides are true, then you’ll get a
True value. If both sides are false, then you’ll get a
False because none of them are true.
And again, if one side is false, then
and will always return
False. So the only case in which
and can return
True is if both sides of it are true.
Now it’s time for
or, only one side of the operator needs to be true. If both sides are true, then that’s also
The only case in which it will be
False is if both sides are false.
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
If your code says
not False, then that expression evaluates to
True. If your code says
not True, then that expression evaluates to
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 what is your best guess at what this will produce? Well, you can break it up at the bits and say,
1 > 2, is that true or false? That’s a false statement.
5 > 3, that’s a true statement.
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.
Say you have a string,
"Python", and you want to see if it’s equal to another string,
Then you have the
or keyword and you have
10 != 9. So what do you think this will give you? Again, take it by bits. You’ve got
"Python" == "Pythonic". Is that a true or false statement?
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.
09:34 So now what happens if you just copy this
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.
Say you had an expression like this
10 < 50
> 20. What would evaluate first in this situation?
This gives you
True, but why does it give you
True? Because if you do,
10 < 50, you get
True. But then would you be saying
True > 20?
That gives you
False. So what if it was
50 > 20? That gives you
True, but is
10 < True? That’s
So why is this giving you a
True value? Well, the reason is that in Python, whenever you chain comparators like this, it will implicitly add an
and operator in here.
So then the expression on the first line,
10 < 50 > 20 is actually evaluated as
10 < 50 and 50 > 20, which will give you the
True value that you got at the beginning.
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.
But it is useful for checking ranges. So say you had
age = 20, and then you want to check if that age is between 10 and 30. So you can go
10 < age
True because that is evaluated like this:
10 < age and age < 30, which will give you
True. So you don’t need to chain operators, but it can be a nice shorthand for ranges.
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.
14:19 One final thing to bear in mind about comparisons is when you’re dealing with strings, Python uses alphabetical order.
"a" < "z" is true because
"a" has a smaller index than
"z" does because
"a" comes before
"z" in the alphabet.
It also sorts strings lexicographically. Now that’s just a fancy word for saying alphabetical order. So if you had
> "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
15:10 So that’s just something to bear in mind when comparing strings in Python.
Become a Member to join the conversation.