How to Get a Negation of a Boolean in Python
Published on Aug. 22, 2023, 12:15 p.m.
To get the negation of a boolean in Python, you can use the not
operator. The not
operator returns the opposite boolean value of a given expression. Here’s an example:
bool_value = True
negation = not bool_value
print(negation)
This will output False
, which is the opposite value of the original boolean value True
.
Alternatively, you can use the bitwise NOT operator, ~
, to get the negation of a boolean. However, this method is less common and less recommended than using the not
operator. Here’s an example:
bool_value = True
negation = ~bool_value
print(negation)
This will output -2
, which is the negation of the boolean value True
in terms of bitwise operations.