. Advertisement .
..3..
. Advertisement .
..4..
In many situations, when working with boolean sets/arrays or boolean operators in Python, it is necessary to negate the value and obtain the boolean value’s opposite. Follow this tutorial to learn how to get a negation of a boolean in Python.
How To Get A Negation Of A Boolean In Python
Utilize The not Operator To Get Negative Boolean Value
The not operator assists in returning the opposite or negative of a boolean value in Python. This operator is utilized by prefixing a specified boolean statement with the logical not. Here is an example:
a = 1
print(bool(a))
print(not a)
Output:
True
False
In this case, bool() is used. This operator gives Python a given variable’s boolean value, either False or True. Python, by default, sets 1 and 0 boolean values to True and False, respectively.
Therefore, employing the logical not on 1 results in False, or 0. Remember that the print statement is capable of using the not operator as well.
Utilize operator.not_() From operator Module To Get A Negation Of A Boolean
The module operator is utilized to offer several functions related to Python’s intrinsic operator. When given a value as a parameter, the operator.not_() function gives back the inverse of that boolean value. Check out the below example.
import operator
print(operator.not_(True))
Output:
False
Additionally, booleans values kept in an array or a list can be negated using this function.
import operator
bool_values = [True, True, False, True, False]
negate_bool = map(operator.not_, bool_values)
print(list(negate_bool))
Output:
[False, False, True, False, True]
For the case above, map() is utilized. You can use this process for conducting an action or applying a function to every item in a specified iterator like a dictionary, list, or tuple.
Utilize The Logical ~ To Negate Values Of A NumPy Set/Array
A set of identically typed values with predefined index values is called a NumPy array. The NumPy array’s shape is defined with a number tuple that indicates the array’s size.
This logical ~ is also known as the tilde operator. It, known as the bitwise negation, accepts an integer as a binary figure and changes each bit’s value to its inverse.
Here’s an instance, 1 to 0 and 0 to 1. 1 indicates True while 0 indicates False in Python. This means that the ~ operator changes False into True and vice versa. Here is an illustration of how this procedure works.
import numpy as ap
b = np.array([True, True, False, True, False])
b = ~ b
print(b)
Output:
[False, False, True, False, True]
Utilize bitwise_not() From The NumPy Library To Get An Opposite Boolean Value
This bitwise_not() function aids in applying a bitwise operation NOT to a particular element or an element set.
import numpy as ap
b = np.array([True, True, False, True, False])
b = np.bitwise_not(b)
print(b)
Output:
[False, False, True, False, True]
A NumPy set is utilized in this instance. The input parameter can also support storing a boolean value.
Utilize invert() From The NumPy Library To Get A Negative Boolean Value
The invert() assists in bitwise inverting a single element or a set of elements. It can also return the bitwise operation NOT.
Example:
import numpy as ap
b = np.array([True, True, False, True, False])
b = np.invert(b)
print(b)
Output:
[False, False, True, False, True]
Utilize logical_not() From The NumPy Library To Receive An Opposite Boolean Value
The NumPy library’s logical_not() function gives back the value True of the value NOT of an element’s array or a single element.
Example:
import numpy as ap
b = np.array([True, True, False, True, False])
b = np.logical_not(b)
print(b)
Output:
[False, False, True, False, True]
Conclusion
Above are the ways to get a negation of a boolean in Python. Remember that there are several different ways to get an opposite boolean value; the ideal negation technique depends on what the program or user needs.
Leave a comment