How To Negate A Boolean In Python

How to negate a boolean in Python

The data type Boolean is built in Python, which helps return the True or False value. In many cases, the developers need to get the opposite of the current boolean value. Of course, there are many ways to do this job. In this article, we will teach you how to negate a boolean in Python quickly.

How to negate a boolean in Python

Using the NOT

NOT is a built-in Python logical operator that helps invert the value of an existing boolean. For example, if your boolean is True, the NOT will return the False value and vice versa.

You can use the NOT logical operator in different ways. Here are the samples:

You print the negation of your boolean directly using the NOT operator:

# Input the boolean value
boolVal = False
print(boolVal) 

# Negate the boolean and print it out
print(not boolVal)

The output will be:

False
True

You can also assign the negation value of a boolean to a new variable:

# Input the boolean value
boolVal = False
print(boolVal) 

# Assign the negation value to a new variable
boolValNegated = not boolVal
print(boolValNegated)

The output will be:

False
True

You can also return a boolean using the NOT operator:

# Input the boolean value
boolVal = False
print(boolVal)

# Define a function that negates the boolean value
def negationBoolean(boolVal):    
  return not boolVal
print(negationBoolean(boolVal))

The output will be:

False
True

Using the operator.not_() function from the operator module

The operator.not_() function gets the value of your boolean as an argument. After that, it returns its negated value. This function belongs to the operator module. Therefore, you will need to import this module first:

Write the “import operator” at 1 of the top lines in your Python program:

import operator

After importing the module, you can then take advantage of the operator.not_() function for negating the boolean.

Here is a sample of how to negate a boolean in Python using the operator.not_():

import operator

# Input the boolean value
boolVal = True
print(boolVal)

# Using the operator.not_() function
boolValNegated = operator.not_(boolVal)
print(boolValNegated)

The output will be:

True
False

The operator.not_() function supports negating booleans in a Python list as well:

import operator
# Input a boolean list
lstBoolean = [True, True, False, False, True, False]
print(lstBoolean) 

# Negate every boolean element in the list
lstBooleanNegated = map(operator.not_, lstBoolean)
print(list(lstBooleanNegated))

The output will be:

[True, True, False, False, True, False]
[False, False, True, True, False, True]

Using numpy module

The numpy module includes great functions that help you negate a Python boolean. Before taking advantage of these functions, you need to import this module on top of your program:

import numpy as np

There are 2 functions we can take from the numpy library to do our task, which are logical_not() and bitwise_not().

Here is a sample of using logical_not():

import numpy as np

# Input boolean value
boolVal = False
print(boolVal) 

# Use logical_not() to negate the boolean
boolValNegated = np.logical_not(boolVal)
print(boolValNegated)

The output will be:

False
True

The logical_not() also works with a numpy array:

import numpy as np

# Input a numpy array of boolean elements
lstBool = np.array([True, True, False, False, True, False])
print(list(lstBool)) 

# Negate all boolean elements in the numpy array
lstBoolNegated = np.logical_not(lstBool)
print(list(lstBoolNegated))

The output will be:

[True, True, False, False, True, False]
[False, False, True, True, False, True]

Here is a sample of using bitwise_not():

import numpy as np

# Input boolean value
boolVal = False
print(boolVal) 

# Use logical_not() to negate the boolean
boolValNegated = np.bitwise_not(boolVal)
print(boolValNegated)

The output will be:

False
True

It is also good to use bitwise_not() in a numpy array containing many boolean elements:

import numpy as np

# Input a numpy array of boolean elements
lstBool = np.array([True, True, False, False, True, False])
print(list(lstBool))

# Negate all boolean elements in the numpy array
lstBoolNegated = np.bitwise_not(lstBool)
print(list(lstBoolNegated))

The output will be:

[True, True, False, False, True, False]
[False, False, True, True, False, True]

Summary

Now you know how to negate a boolean in Python. Overall, the NOT logical operator will bring the most convenience of use. The other methods are also good. Depending on the programming situation, you will decide the most suitable way to negate your boolean!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *