Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using not With Other Boolean Operators

If you’d like to learn more about Boolean operators and short-circuit evaluation, then check out the following:

00:00 It’s very likely you’ll use not in conjunction with other operations. In this lesson, you’ll learn how that works in Python.

00:10 Python uses something called operator precedence, sometimes referred to as order of operations, to determine which operation to perform first in an expression with multiple operators.

00:22 Specifically, Boolean operators have a lower precedence than comparison operators. That means comparisons will be done first, before things like not, and, and or. So for an example, in an expression like not True == False, the comparison will be done first. True does not equal False, so the comparison is False.

00:49 But then the not is applied, and not False becomes True. Here’s another.

00:59 Now this expression probably makes sense to you. False is not True, so would expect this to be True. But this actually gives an error, which you can see at the bottom of the screen. What’s happening?

01:13 Python is trying to do the == comparison first, but the first thing it sees on the right is the word not. It can’t apply not to True yet because it has to do the == first.

01:27 It’s like the word True isn’t even there. False is equal to not. Since Python can’t make sense of this expression, it produces a Syntax Error.

01:41 Since you want to do the not True part first, you need to use parentheses, just like you would a mathematical expression, to indicate that operation should be performed first.

01:56 This way gives the intended result. Why is this important to know? Because many times, you’ll want to connect comparisons with Boolean operators, and you need to understand how Python will evaluate them.

02:11 Also note, when mixed with other Boolean operators, not has the highest precedence, followed by and and then or. Another thing to keep in mind, the result of the not operation is always True or False.

02:29 You saw that in the last lesson. No matter what type of operand not was applied to, the result was either True or False.

02:40 But if you’re familiar with the other Boolean operators, perhaps you know that’s not always the case with and and or. It is true that often the result of and or or is True or False, as these examples show, but Python uses something called short-circuit evaluation.

03:01 And the way it does this means the result of an and or or operation could be one of the operands. So in an expression like 0 and 42, Python will note that the first operand is false and provide that as the result.

03:18 But if you use or instead, Python again noticing that 0 is false, that means for or, the result is whatever the second operand is—in this case, 42. So even though the results for and and or can be any number of things, that is not the case for not.

03:40 not always gives a result of either True or False. For a more detailed look at and and or and short-circuit evaluation, see the Real Python tutorials and courses on these operators.

03:57 Now you’re ready to see places where the not operator can be used.

Become a Member to join the conversation.