Skip to content
Advertisement

How to “negate” value: if true return false, if false return true?

JavaScript

Is there a better way to do a toggle in python, like a nyvalue = not myval ?

Advertisement

Answer

Use the not boolean operator:

JavaScript

not returns a boolean value (True or False):

JavaScript

If you must have an integer, cast it back:

JavaScript

However, the python bool type is a subclass of int, so this may not be needed:

JavaScript
Advertisement