How To Get a Negation of a Boolean in Python?
Boolean is a built-in data type provided by Python. The Boolean value represents True or False values. Consider an example, 5 < 20 is True and 10 > 20 is False. Let’s see the negation of a Boolean variable.
There are various methods to find the negation of a Boolean in Python:
- Using the "~" Operator.
- Using the "not" Operator.
- With the help of the operator module.
- Using Minus from ‘1’
- Use bitwise_not(), logical_not() provided by NumPy module.
Method 1: Using The “~” Operator:
There are the following steps to get the negation of Boolean in Python that are given as follows:
- Made a variable that stores the input Boolean value.
- Print the value of the Boolean.
- Use the ~ Operator to get the negation of the Boolean value and print the output value.
- The bool() function in Python converts a Boolean value if it is not already a Boolean value.
Code:
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# Printing the negation of the input boolean value using the ~ Operator
print("Negation of the input boolean value:", bool(~inputBool))
Output:
Input boolean value: False
Negation of the input boolean value: True
Explanation:
In the above, code a Boolean variable is made and that variable is printed and the '~' Operator is used with input value that will return the negation of the input value in the numeric value and the bool() function is used to get the negation of the input Boolean value.
Method 2: Using “Not” Operator:
Python's operator "not" is a logical operator that gives the output as the negation or complement of the operand's Boolean values. The not Operator is used to get the negation of the input Boolean value and print the output value.
Code:
#input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", bool(inputBool))
# Printing the negation of the input boolean
# value using 'not' Operator
print("Negation of the input boolean value:", not inputBool)
Output:
Input boolean value: False Negation of the input boolean value: True
Explanation:
In the above, code a Boolean variable is made with the False value and the variable is printed after converting it to Boolean and the negation of the variable is find with the help of the not Operator in Python and the result is printed. Let's find the negation of the value of the string.
Code:
# input string
inputStr = "negation"
# converting the input string into a boolean datatype
# using bool() function and printing a boolean value
print("Input boolean value:", bool(inputStr))
# Printing the negation of the Boolean
# value using 'not' Operator
print("Negation of the input string:", not inputStr)
Output:
Input boolean value: True Negation of the input string: False
Explanation:
In the above code, negation is the string and this string is converted into Boolean value that is True and printed and the negation of the string is extracted that is False.
Method 3: Using Operator Module:
Negation of the Boolean can also be found with the help of the operator module. First, import the operator module by typing the command in Python:
import operator
Code:
# Importing operator module
import operator
# input string
inputStr = "negation"
# converting the input string into a boolean datatype
# using bool() function and printing a boolean value
print("Input boolean value:", bool(inputStr))
# Printing the negation of the Boolean
# value using the Operator.not_() function
print("Negation of the input string:", operator.not_(inputStr))
Output:
Input boolean value: True Negation of the input string: False
Explanation:
In the above code, the operator module is imported, and a string is taken as input, the string is converted to the Boolean and then the string is passed to the not_ function provided by the operator module.
Method 4: Using Minus FROM 1:
Code:
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# getting the negation of the input boolean value
# by subtracting it from 1 and converting it to a boolean value
outputBool = bool(1-inputBool)
# printing the resultant boolean value
print("Output boolean value:", outputBool)
Output:
Input boolean value: False
Output boolean value: True
Explanation:
In the above code, a Boolean False value is taken as input and that Boolean value is printed and this Boolean False value is subtracted from 1 and the resultant value is printed.
Method 5: Using Bitwise_not(), logical_not() of NumPy Module:
The bitwise_not() function provided by the NumPy module returns the negation of the given Boolean input. Let's see a code to get the negation of the value:
Code:
# importing NumPy module with an alias name
import numpy as np
# input NumPy array containing boolean elements
inputArray = np.array([False, True, True, False, False])
# converting input array to list and printing it
print("Input List:", list(inputArray))
# Getting the negation values of the input array
# using the bitwise_not() function of NumPy module
outputArray = np.bitwise_not(inputArray)
# Converting output array to list and printing it
print("Output List:", list(outputArray))
Output:
Input List: [False, True, True, False, False]
Output List: [True, False, False, True, True]
Explanation:
In the above code, the NumPy module is imported and the array of Boolean is made with True and False value and the array is printed. The array is then passed to the bitwise_not() function of the NumPy module and the result negation array is printed.
Using numpy.logical_not() method:
The logical_not() method can also be used to find the negation of the Boolean that is provided by the NumPy module. Let's see a code to get the negation of the Boolean with the help of the logical_not() function provided by the NumPy module.
Code:
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# getting the negation of the input boolean value using logical_not() function
outputBool = np.logical_not(inputBool)
# printing the resultant boolean value
print("Output boolean value:", outputBool)
Output:
Input boolean value: False
Output boolean value: True
Explanation:
In the above code, a Boolean value False is taken as input and that value is printed and then the value is passed to the np.logical_not() function provided by NumPy to get the negation of the value and the output value is printed.
Conclusion:
There are five methods to find the negation of the Boolean in Python and how a value is converted to Boolean in Python.