Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active January 18, 2021 19:03
Show Gist options
  • Save rpivo/5f7ed655e20540dfff495f7d2a27e56d to your computer and use it in GitHub Desktop.
Save rpivo/5f7ed655e20540dfff495f7d2a27e56d to your computer and use it in GitHub Desktop.
Flipping a Value Between 0 and 1 in Python

Flipping a Value Between 0 and 1 in Python

You can convert a 1 to its boolean opposite (and vice versa) using int(not bool(n)). This converts n to a boolean value using bool(), uses the not keyword to get the boolean opposite of this value, and then converts this value to an integer with int().

print(
  int(not bool(1)), # 0
  int(not bool(0)), # 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment