In python, is 'foo == (8 or 9)' or 'foo == 8 or foo == 9' more correct?
2 answers
You probably want foo == 8 or foo == 9
, since:
In [411]: foo = 9
In [412]: foo == (8 or 9)
Out[412]: False
In [413]: foo == 8 or foo == 9
Out[413]: True
In the end, (8 or 9)
equals 8
:
In [414]: (8 or 9)
Out[414]: 8
Alternatively, you can also write
foo in (8, 9)
This works for Python3 as well as Python2.
+13
source to share