Swapping 1 with 0 and 0 with 1 in a Pythonic way
The shortest approach is using the bitwise operator XOR.
If you want val
to be reassigned:
val ^= 1
If you do not want val
to be reassigned:
val ^ 1
This isn't pythonic, but it is language neutral. Often val = 1 - val
is simplest.