Shortest way in Python to set boolean to False if True, and set to True if False

Viewed 603
def setBoolean(status):
    if status:
        status = False
    else:
        status = True

status = True
setBoolean(status)

I want a button click to set a variable to False if True, and True if False, i.e. the opposite of what it is. How do I do this with the shortest length of code?

4 Answers

You can use not as

def setBoolean(status):
    status = not status
def setBoolean(status):
    return not status

Try

status = True
status = not status

The shortest way is

status ^= True
Related