How to negate a boolean in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Negate a boolean in Python #

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.

main.py
import numpy as np # ✅ Negate a boolean my_bool = True result = not my_bool print(result) # 👉️ False # ----------------------------------- # ✅ Negate a list of booleans my_list = [True, True, False, False] result = [not item for item in my_list] print(result) # 👉️ [False, False, True, True] # -------------------------------- # ✅ Negate a numpy array of booleans arr = np.array([True, True, False, False], dtype=bool) result = np.logical_not(arr) print(result) # 👉️ [False False True True]

We used the not operator to negate a boolean value.

main.py
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.

main.py
print(not True) # 👉️ False print(not False) # 👉️ True print(not 'hello') # 👉️ False print(not '') # 👉️ True

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type.
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

Negate a list of booleans in Python #

To negate a list of booleans:

  1. Use a list comprehension to iterate over the list.
  2. Use the not operator to negate each boolean.
  3. The new list will contain the negation of the values in the original list.
main.py
my_list = [True, True, False, False] result = [not item for item in my_list] print(result) # 👉️ [False, False, True, True]

We used a list comprehension to iterate over the list of boolean values.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the not operator to flip the boolean value and return the result.

The new list contains the negation of the values in the original list.

Negate a numpy array of booleans #

Use the numpy.logical_not() method to negate a numpy array of booleans, e.g. result = numpy.logical_not(arr). The logical_not() method applies the logical not operator to the elements in the array and returns the result.

main.py
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.logical_not(arr) print(result) # 👉️ [False False True True]

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.