In some part of my Python program I have a val variable that can be 1 or 0. If it\'s 1 I must change to 0, if it\'s 0 I must change to 1.
How do you do it in a Pytho
After seeing all these simpler answers i thought of adding an abstract one , it's Pythonic though :
val = set(range(0,2)).symmetric_difference(set(range(0 + val, val + 1))).pop()
All we do is return the difference of 2 sets namely [0, 1] and [val] where val is either 0 or 1.
we use symmetric_difference() to create the set [0, 1] - [val] and pop() to assign that value to variable val.