How do I get the opposite (negation) of a Boolean in Python?

amyassin picture amyassin · Aug 11, 2011 · Viewed 182.1k times · Source

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?

Answer

jtbandes picture jtbandes · Aug 11, 2011

You can just use:

return not bool