How to Get a Negation of a Boolean in Python
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.
Method 1: Using the “~” operator
This is the Bitwise NOT operator which can be used to return the negation of the operand.
Python3
# Input boolean variable a = False # Print a print (a) # Printing negation print ( bool (~a)) |
Output :
False True
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: Using “not” Operator
Not operator is the logical operator which returns the complementary of the Boolean value of the operand.
Python3
# Input boolean variable a = False # Print a print ( bool (a)) # Printing negation 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
# Input string a = "GeeksforGeeks" # Converting string into boolean # Printing boolean value print ( bool (a)) # Printing negation print ( not a) |
Output :
True False
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3: 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
# Input string a = "GeeksforGeeks" # Converting string into boolean # Printing boolean value print ( bool (a)) # Printing negation 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 of boolean elements input_list = [ True , True , False , True , False ] # Printing list print ( list (input_list)) # Converting into negation values # Mapping them with their input # index values output_list = map (operator.not_, input_list) # Printing output 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)
Method 4: 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
# Input array of boolean elements b = np.array([ True , True , False , True , False ]) # Printing input list print ( list (b)) # Converting using bitwise_not function b = np.bitwise_not(b) # Printing output list 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
# Input boolean variable b = True # Printing input print (b) # Converting using logical_not() # function b = np.logical_not(b) # Printing output print (b) |
Output :
True False
Time Complexity: O(1)
Auxiliary Space: O(1)
Method #5: 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
# Input boolean variable b = True # Printing input print (b) # Converting by subtracting 1 # function b = bool ( 1 - b) # Printing output print (b) |
Output:
True False
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...