Question
Asked By – amyassin
For the following sample:
def fuctionName(int, bool):
if int in range(...):
if bool == True:
return False
else:
return True
Is there any way to skip the second if-statement? Just to tell the computer to return the opposite of the boolean bool
?
Now we will see solution for issue: How do I get the opposite (negation) of a Boolean in Python?
Answer
To negate a boolean, you can use the not
operator:
not bool
Or in your case, the if
/return
blocks can be replaced by:
return not bool
Be sure to note the operator precedence rules, and the negated is
and in
operators: a is not b
and a not in b
.
This question is answered By – jtbandes
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0