How to Get a Negation of a Boolean in Python
Last Updated :
27 Jul, 2023
Boolean datatype is the built-in data type in Python. It represents the True or False values. Like 0<1 is True and 7>10 is False. The task is to print the negation of a Boolean variable
Input: True
Output: False
Input: False
Output: True
Input: "GeeksforGeeks"
Output: False
Input: [True, True, False, True, False]
Output: [False, False, True, False, True]
To implement this task, Python has many built-in operators and functions. Let’s explore them one by one.
How to Get a Negation of a Boolean in Python Using the “~” operator
This is the Bitwise NOT operator which can be used to return the negation of the operand.
Python3
a = False
print (a)
print ( bool (~a))
|
Output :
False
True
Time Complexity: O(1)
Auxiliary Space: O(1)
Negation of a Boolean in Python Using “not” Operator
Not operator is the logical operator which returns the complementary of the Boolean value of the operand.
Python3
a = False
print ( bool (a))
print ( not a)
|
Output :
False
True
Time Complexity: O(1)
Auxiliary Space: O(1)
Here, we are using print(bool(a)), as if the “a” is not a Boolean value then it will be converted into it.
Let’s understand this with another example :
Python3
a = "GeeksforGeeks"
print ( bool (a))
print ( not a)
|
Output :
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Get a Negation of a Boolean in Python Using Operator Module
Before implementing the code, import the Operator module using below code.
import operator
This method uses the operator.not_() function to return the negation of the Boolean value. It has a constant time complexity and requires constant auxiliary space.
Python3
a = "GeeksforGeeks"
print ( bool (a))
print (operator.not_(a))
|
Output:
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
We can also implement the negation in list elements using operator.not_() function
Python3
input_list = [ True , True , False , True , False ]
print ( list (input_list))
output_list = map (operator.not_, input_list)
print ( list (output_list))
|
Output :
[True, True, False, True, False]
[False, False, True, False, True]
Note: To negate the values in the python list, always use the above syntax. As operator.not_(input_list) will consider the whole list as an input and perform accordingly.
Time Complexity: O(n)
Auxiliary Space: O(n)
Get a Negation of a Boolean in Python Using Numpy Module
Before implementing the code, import the Numpy library using below code.
import numpy as np
This method uses the numpy library and has two functions: bitwise_not() and logical_not().
1. bitwise_not() function returns the negation value of the given Boolean argument.
Python3
b = np.array([ True , True , False , True , False ])
print ( list (b))
b = np.bitwise_not(b)
print ( list (b))
|
Output :
[True, True, False, True, False]
[False, False, True, False, True]
Time Complexity: O(n)
Auxiliary Space: O(n)
2. We can also use logical_not() function, it is also the function of Numpy library and returns the Boolean value.
Python3
b = True
print (b)
b = np.logical_not(b)
print (b)
|
Output :
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Get a Negation of a Boolean in Python Minus the value with ‘1’:
This method subtracts the boolean data-type value to 1 and typecasts it back to boolean to get the negation of the original boolean value. It has a constant time complexity and requires constant auxiliary space.
Python
b = True
print (b)
b = bool ( 1 - b)
print (b)
|
Output:
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...