Using the Python not Operator (Summary)
Python’s not
is a logical operator that inverts the truth value of Boolean expressions and objects. It’s handy when you need to check for unmet conditions in conditional statements and while
loops.
You can use the not
operator to help you decide the course of action in your program. You can also use it to invert the value of Boolean variables in your code.
In this course, you learned how to:
- Work with Python’s
not
operator - Use the
not
operator in Boolean and non-Boolean contexts - Use
operator.not_()
to perform logical negation in a functional style - Avoid unnecessary negative logic in your code whenever possible
If you’d like to learn more about Boolean operations in Python, then check out the following tutorials:
- Python Booleans: Optimize Your Code With Truth Values
- Operators and Expressions in Python
- Using the “and” Boolean Operator in Python
- Using the “or” Boolean Operator in Python
To learn more about membership operators, check out Python’s “in” and “not in” Operators: Check for Membership.
You can also check out the video courses on the and
and or
operators:
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 And that’s the course. Let’s go over what you learned.
00:05
In this course, you learned how the Python not
operator works, you learned where to use it in Python programs, and you learned some best practices for using the not
operator. Great work.
00:19 If you’re interested in more information about Boolean operations in Python, see the links below for Real Python tutorials and courses on the other Boolean operators. I hope you enjoyed this course, and I hope to see you in another one here at Real Python.
Become a Member to join the conversation.