Using the "not" Boolean Operator in Python (original) (raw)
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using the Python not Operator
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
not
operator works - How to use the
not
operator 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.
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:
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:
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:
Operator | Logical Operation |
---|---|
and | Conjunction |
or | Disjunction |
not | Negation |
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
:
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
if
statements andwhile
loops - 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
In this tutorial, you’ll find examples that cover all these use cases. To kick things off, you’ll start by learning how the not
operator works with Boolean expressions and also with common Python objects.
A Boolean expression always returns a Boolean value. In Python, this kind of expression returns True
or False
. Say you want to check if a given numeric variable is greater than another:
The expression x > y
always returns False
, so you can say it’s a Boolean expression. If you place not
before this expression, then you get the inverse result, True
.
You can also use not
with common Python objects, such as numbers, strings, lists, tuples, dictionaries, sets, user-defined objects, and so on:
In each example, not
negates the truth value of its operand. To determine whether a given object is truthy or falsy, Python uses bool(), which returns True
or False
depending on the truth value of the object at hand.
This built-in function internally uses the following rules to figure out the truth value of its input:
By default, an object is considered true unless its class defines either a __bool__() method that returns
False
or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:
- constants defined to be false:
None
andFalse
.- zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
(Source)
Once not
knows the truth value of its operand, it returns the opposite Boolean value. If the object evaluates to True
, then not
returns False
. Otherwise, it returns True
.
Now that you know how not
works in Python, you can dive into more specific use cases of this logical operator. In the following section, you’ll learn about using not
in Boolean contexts.
Using the not
Operator in Boolean Contexts
Like the other two logical operators, the not
operator is especially useful in Boolean contexts. In Python, you have two statements that define Boolean contexts:
- if statements let you perform conditional execution and take different courses of action based on some initial conditions.
- while loops let you perform conditional iteration and run repetitive tasks while a given condition is true.
These two structures are part of what you’d call control flow statements. They help you decide a program’s execution path. In the case of the not
operator, you can use it to select the actions to take when a given condition is not met.
if
Statements
You can use the not
operator in an if
statement to check if a given condition is not met. To make an if
statement test if something didn’t happen, you can put the not
operator in front of the condition at hand. Since the not
operator returns the negated result, something true becomes False
and the other way around.
The syntax for an if
statement with the not
logical operator is:
In this example, condition
could be a Boolean expression or any Python object that makes sense. For example, condition
can be a variable containing a string, a list, a dictionary, a set, and even a user-defined object.
If condition
evaluates to false, then not
returns True
and the if
code block runs. If condition
evaluates to true, then not
returns False
and the if
code block doesn’t execute.
A common situation is one where you use a predicate or Boolean-valued function as a condition
. Say you want to check if a given number is prime before doing any further processing. In that case, you can write an is_prime()
function:
In this example, is_prime()
takes an integer number as an argument and returns True
if the number is prime. Otherwise, it returns False
.
You can also use this function in a negative conditional statement to approach those situations where you want to work with composite numbers only:
Since it’s also possible that you need to work with composite numbers only, you can reuse is_prime()
by combining it with the not
operator as you did in this second example.
Another common situation in programming is to find out if a number is inside a specific numeric interval. To determine if a number x
is in a given interval in Python, you can use the and
operator or you can chain comparison operators appropriately:
In the first example, you use the and
operator to create a compound Boolean expression that checks if x
is between 20
and 40
. The second example makes the same check but using chained operators, which is a best practice in Python.
You may also face the need to check if a number is outside of the target interval. To this end, you can use the or
operator:
This or
expression allows you to check if x
is outside the 20
to 40
interval. However, if you already have a working expression that successfully checks if a number is in a given interval, then you can reuse that expression to check the opposite condition:
In this example, you reuse the expression you originally coded to determine if a number is inside a target interval. With not
before the expression, you check if x
is outside the 20
to 40
interval.
while
Loops
The second Boolean context in which you can use the not
operator is in your while
loops. These loops iterate while a given condition is met or until you jump out of the loop by using break, using return, or raising an exception. Using not
in a while
loop allows you to iterate while a given condition is not met.
Say you want to code a small Python game to guess a random number between 1 and 10. As a first step, you decide to use input() to capture the user’s name. Since the name is a requirement for the rest of the game to work, you need to make sure you get it. To do that, you can use a while
loop that asks for the user’s name until the user provides a valid one.
Fire up your code editor or IDE and create a new guess.py
file for your game. Then add the following code:
In guess.py
, you first import randint() from random. This function allows you to generate random integer numbers in a given range. In this case, you’re generating numbers from 1
to 10
, both included. Then you print a welcoming message to the user.
The while
loop on line 10 iterates until the user provides a valid name. If the user provides no name by just pressing Enter, then input()
returns an empty string (""
) and the loop runs again because not ""
returns True
.
Now you can continue with your game by writing the code to provide the guessing functionality. You can do it by yourself, or you can expand the box below to check out a possible implementation.
The second part of the game should allow the user to enter a number from 1 to 10 as their guess. The game should compare the user’s input with the current secret number and take actions accordingly. Here’s a possible implementation:
You use an infinite while
loop to take the user’s input until they guess the secret
number. In every iteration, you check if the input matches secret
and provide clues to the user according to the result. Go ahead and give it a try!
As an exercise, you can restrict the number of attempts before the user loses the game. Three attempts could be a nice option in this case.
How was your experience with this little game? To learn more about game programming in Python, check out PyGame: A Primer on Game Programming in Python.
Now that you know how to use not
in Boolean contexts, it’s time to learn about using not
in non-Boolean contexts. That’s what you’ll do in the following section.
Using the not
Operator in Non-Boolean Contexts
Since the not
operator can also take regular objects as an operand, you can use it in non-Boolean contexts too. In other words, you can use it outside of an if
statement or a while
loop. Arguably, the most common use case of the not
operator in a non-Boolean context is to invert the truth value of a given variable.
Suppose you need to perform two different actions alternatively in a loop. In that case, you can use a flag variable to toggle actions in every iteration:
Every time this loop runs, you check the truth value of toggle
to decide which course of action to take. At the end of each code block, you change the value of toggle
so you can run the alternative action in the next iteration. Changing the value of toggle
requires you to repeat a similar logic twice, which might be error-prone.
You can use the not
operator to overcome this drawback and make your code cleaner and safer:
Now the highlighted line alternates the value of toggle
between True
and False
using the not
operator. This code is cleaner, less repetitive, and less error-prone than the example you wrote before.
Using the Function-Based not
Operator
Unlike the and
operator and the or
operator, the not
operator has an equivalent function-based implementation in the operator module. The function is called not_(). It takes an object as an argument and returns the same outcome as an equivalent not obj
expression:
To use not_()
, you first need to import it from operator
. Then you can use the function with any Python object or expression as an argument. The result is the same as using an equivalent not
expression.
Using the not_()
function instead of the not
operator is handy when you’re working with higher-order functions, such as map(), filter(), and the like. Here’s an example that uses the not_()
function along with sorted() to sort a list of employees by placing empty employee names at the end of the list:
In this example, you have an initial list called employees
that holds a bunch of names. Some of those names are empty strings. The call to sorted()
uses not_()
as a key
function to create a new list that sorts the employees, moving the empty names to the end of the list.
Working With Python’s not
Operator: Best Practices
When you’re working with the not
operator, you should consider following a few best practices that can make your code more readable, clean, and Pythonic. In this section, you’ll learn about some of these best practices related to using the not
operator in the context of membership and identity tests.
You’ll also learn how negative logic can impact the readability of your code. Finally, you’ll learn about some handy techniques that can help you avoid unnecessary negative logic, which is a programming best practice.
Test for Membership
Membership tests are commonly useful when you’re determining if a particular object exists in a given container data type, such as a list, tuple, set, or dictionary. To perform this kind of test in Python, you can use the in operator:
The in operator returns True
if the left-side object is in the container on the right side of the expression. Otherwise, it returns False
.
Sometimes you may need to check if an object is not in a given container. How can you do that? The answer to this question is the not
operator.
There are two different syntaxes to check if an object is not in a given container in Python. The Python community considers the first syntax as bad practice because it’s difficult to read. The second syntax reads like plain English:
The first example works. However, the leading not
makes it difficult for someone reading your code to determine if the operator is working on "c"
or on the whole expression, "c" in ["a", "b", "c"]
. This detail makes the expression difficult to read and understand.
The second example is much clearer. The Python documentation refers to the syntax in the second example as the not in operator. The first syntax can be a common practice for people who are starting out with Python.
Now it’s time to revisit the examples where you checked if a number was inside or outside a numeric interval. If you’re working with integer numbers only, then the not in
operator provides a more readable way to perform this check:
The first example checks if x
is inside the 20
to 40
range or interval. Note that you use 41
as the second argument to range() to include 40
in the check.
When you’re working with integer numbers, this small trick about where exactly you use the not
operator can make a big difference regarding code readability.
Check the Identity of Objects
Another common requirement when you’re coding in Python is to check for an object’s identity. You can determine an object’s identity using id(). This built-in function takes an object as an argument and returns an integer number that uniquely identifies the object at hand. This number represents the object’s identity.
The practical way to check for identity is to use the is operator, which is pretty useful in some conditional statements. For example, one of the most common use cases of the is
operator is to test if a given object is None:
The is
operator returns True
when the left operand has the same identity as the right operand. Otherwise, it returns False
.
In this case, the question is: how do you check if two objects don’t have the same identity? Again, you can use two different syntaxes:
In both examples, you check if obj
has the same identity as the None
object. The first syntax is somewhat difficult to read and non-Pythonic. The is not
syntax is way more explicit and clear. The Python documentation refers to this syntax as the is not operator and promotes its use as a best practice.
Avoid Unnecessary Negative Logic
The not
operator enables you to reverse the meaning or logic of a given condition or object. In programming, this kind of feature is known as negative logic or negation.
Using negative logic correctly can be tricky because this logic is difficult to think about and understand, not to mention hard to explain. In general, negative logic implies a higher cognitive load than positive logic. So, whenever possible, you should use positive formulations.
Here is an example of a custom_abs()
function that uses a negative condition to return the absolute value of an input number:
This function takes a number as an argument and returns its absolute value. You can achieve the same result by using positive logic with a minimal change:
That’s it! Your custom_abs()
now uses positive logic. It’s more straightforward and understandable. To get this result, you removed not
and moved the negative sign (-
) to modify the input number
when it’s lower than 0
.
You can find many similar examples in which changing a comparison operator can remove unnecessary negative logic. Say you want to check if a variable x
is not equal to a given value. You can use two different approaches:
In this example, you remove the not
operator by changing the comparison operator from equal (==
) to different (!=
). In many cases, you can avoid negative logic by expressing the condition differently with an appropriate relational or equality operator.
However, sometimes negative logic can save you time and make your code more concise. Suppose you need a conditional statement to initialize a given file when it doesn’t exist in the file system. In that case, you can use not
to check if the file doesn’t exist:
The not
operator allows you to invert the result of calling .exists() on file
. If .exists()
returns False
, then you need to initialize the file. However, with a false condition, the if
code block doesn’t run. That’s why you need the not
operator to invert the result of .exists()
.
Now think of how to turn this negative conditional into a positive one. Up to this point, you don’t have any action to perform if the file exists, so you may think of using a pass statement and an additional else
clause to handle the file initialization:
Even though this code works, it violates the “You aren’t gonna need it” (YAGNI) principle. It’s an especially determined attempt to remove negative logic.
The idea behind this example is to show that sometimes using negative logic is the right way to go. So, you should consider your specific problem and select the appropriate solution. A good rule of thumb would be to avoid negative logic as much as possible without trying to avoid it at all costs.
Finally, you should pay special attention to avoiding double negation. Say you have a constant called NON_NUMERIC
that holds characters that Python can’t turn into numbers, such as letters and punctuation marks. Semantically, this constant itself implies a negation.
Now say you need to check if a given character is a numeric value. Since you already have NON_NUMERIC
, you can think of using not
to check the condition:
This code looks odd, and you probably won’t ever do something like this in your career as a programmer. However, doing something similar can sometimes be tempting, such as in the example above.
This example uses double negation. It relies on NON_NUMERIC
and also on not
, which makes it hard to digest and understand. If you ever get to a piece of code like this, then take a minute to try writing it positively or, at least, try to remove one layer of negation.
Conclusion
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 tutorial, 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
To these ends, you coded a few practical examples that helped you understand some of the main use cases of the not
operator, so you’re now better prepared to use it in your own code.
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using the Python not Operator