Last updated: Apr 9, 2024
Reading timeยท4 min
Use the not operator to negate a boolean, e.g. result = not my_bool
.
The not operator will negate the boolean value, returning True
if the value
is False
and False
if the value is True
.
my_bool = True result = not my_bool print(result) # ๐๏ธ False
We used the not
operator to negate a boolean value.
print(not True) # ๐๏ธ False print(not False) # ๐๏ธ True
The not
operator returns True
if the value is falsy and False
if the value
is truthy.
print(not True) # ๐๏ธ False print(not False) # ๐๏ธ True print(not 'bobbyhadz.com') # ๐๏ธ False print(not '') # ๐๏ธ True
All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type.""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).You can also use the operator.not_ method to negate a boolean.
import operator print(operator.not_(True)) # ๐๏ธ False print(operator.not_(False)) # ๐๏ธ True
The operator.not_
method negates the given object and returns the result.
The operator.not_
method is mostly used when you need to flip a list of
boolean values with map()
.
import operator list_of_booleans = [True, True, False, False] new_list = list(map(operator.not_, list_of_booleans)) print(new_list) # ๐๏ธ [False, False, True, True]
Note that you shouldn't use the bitwise ~
not operator to negate boolean
values.
print(~True) # ๐๏ธ -2 print(~False) # ๐๏ธ -1
The bitwise ~
not operator returns unexpected results when used with boolean
values because booleans are a subclass of
int.
print(isinstance(True, int)) # ๐๏ธ True
The operator doesn't return the negated boolean value, it returns the negated integer value.
To flip the boolean values in a list:
not
operator to flip each boolean value.my_list = [True, True, False, False] new_list = [not item for item in my_list] print(new_list) # ๐๏ธ [False, False, True, True]
We used a list comprehension to flip the boolean values in a list.
On each iteration, we use the not
operator to flip the current boolean value
and return the result.
my_list = [True, True, False, False] new_list = [not item for item in my_list] print(new_list) # ๐๏ธ [False, False, True, True]
The not
operator returns True
if the value is falsy and False
if the value
is truthy.
print(not True) # ๐๏ธ False print(not False) # ๐๏ธ True print(not 'bobbyhadz.com') # ๐๏ธ False print(not '') # ๐๏ธ True
All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type.""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).Alternatively, you can use the map()
function.
This is a three-step process:
map()
function.map()
function will call the lambda function with each boolean in the
list.list()
class to convert the map
object to a list.my_list = [True, True, False, False] new_list = list(map(lambda x: not x, my_list)) print(new_list) # ๐๏ธ [False, False, True, True]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
Lastly, we used the list()
class to convert the map
object to a list.
The list
class takes an iterable and returns a list object.
If you need to negate the boolean values in a NumPy Array, use the
numpy.logical_not()
method.
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.logical_not(arr) print(result) # ๐๏ธ [False False True True]
Make sure you have NumPy installed to be able to run the code sample.
pip install numpy # ๐๏ธ or with pip3 pip3 install numpy
We used the numpy.logical_not method to negate a NumPy array.
The logical_not
method applies the not
operator to the elements of the
array.
The method returns a new array containing the negation of each element in the original array.
The bitwise not ~
operator can be used to negate a NumPy array of booleans.
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = ~arr print(result) # ๐๏ธ [False False True True]
You can also use the invert
method to achieve the same result.
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.invert(arr) print(result) # ๐๏ธ [False False True True]
The numpy.invert method computes bit-wise inversion, or bit-wise NOT, element-wise.
The method takes an array of booleans and inverts the values in the array.
There is also a bitwise_not
method in NumPy.
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.bitwise_not(arr) print(result) # ๐๏ธ [False False True True]
The bitwise_not
method is an alias for invert
.
You can learn more about the related topics by checking out the following tutorials: