Python’s not operator allows you to invert the truth value of Boolean expressions and objects. You can use this operator in Boolean contexts, such as if statements and while loops. It also works in non-Boolean contexts, which allows you to invert the truth value of your variables.
Using the not operator effectively will help you write accurate negative Boolean expressions to control the flow of execution in your programs.
In this tutorial, you’ll learn:
- How Python’s
notoperator works - How to use the
notoperator in Boolean and non-Boolean contexts - How to use the
operator.not_()function to perform logical negation - How and when to avoid unnecessary negative logic in your code
You’ll also code a few practical examples that will allow you to better understand some of the primary use cases of the not operator and the best practices around its use. To get the most out of this tutorial, you should have some previous knowledge about Boolean logic, conditional statements, and while loops.
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Working With Boolean Logic in Python
George Boole put together what is now known as Boolean algebra, which relies on true and false values. It also defines a set of Boolean operations: AND, OR, and NOT. These Boolean values and operators are helpful in programming because they help you decide the course of action in your programs.
In Python, the Boolean type, bool, is a subclass of int:
>>> issubclass(bool, int)
True
>>> help(bool)
Help on class bool in module builtins:
class bool(int)
bool(x) -> bool
...
This type has two possible values, True and False, which are built-in constants in Python and must be capitalized. Internally, Python implements them as integer numbers:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> int(True)
1
>>> int(False)
0
Python internally implements its Boolean values as 1 for True and 0 for False. Go ahead and execute True + True in your interactive shell to see what happens.
Python provides three Boolean or logical operators:
With these operators, you can build expressions by connecting Boolean expressions with each other, objects with each other, and even Boolean expressions with objects. Python uses English words for the Boolean operators. These words are keywords of the language, so you can’t use them as identifiers without causing a syntax error.
In this tutorial, you’ll learn about Python’s not operator, which implements the logical NOT operation or negation.
Getting Started With Python’s not Operator
The not operator is the Boolean or logical operator that implements negation in Python. It’s unary, which means that it takes only one operand. The operand can be a Boolean expression or any Python object. Even user-defined objects work. The task of not is to reverse the truth value of its operand.
If you apply not to an operand that evaluates to True, then you get False as a result. If you apply not to a false operand, then you get True:
>>> not True
False
>>> not False
True
The not operator negates the truth value of its operand. A true operand returns False. A false operand returns True. These two statements uncover what is commonly known as the truth table of not:
operand |
not operand |
|---|---|
True |
False |
False |
True |
With not, you can negate the truth value of any Boolean expression or object. This functionality makes it worthwhile in several situations:
- Checking unmet conditions in the context of
ifstatements andwhileloops - Inverting the truth value of an object or expression
- Checking if a value is not in a given container
- Checking for an object’s identity
Read the full article at https://realpython.com/python-not-operator/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Real Python
read more
No comments:
Post a Comment