I have a little code issue and it works with IDLE and not with Eclipse, can I write this :
if fields[9] != ('A' or 'D' or 'E' or 'N' or 'R'):
instead of this :
if fields[9] != 'A' and fields[9] != 'D' and fields[9] != 'E' and fields[9] != 'N' and fields[9] != 'R':
Thank you.
You want the
in
operator:Or, you could use
any
:Or, alternatively,
all
, which may have a bit more of the same form as the original:As an aside:
is really the same thing as saying:
because
'A' or 'B' or 'C'
evaluates to'A'
(Try it!). The reason is because withor
, python will return the first "non-falsey" value (or the last one if they're all falsey). Since non-empty strings are non-falsey, the first one gets returned.Use
not in
and a sequence:which tests against a tuple, which Python will conveniently and efficiently store as one constant. You could also use a set literal:
but only more recent versions of Python (Python 3.2 and newer) will recognise this as an immutable constant. This is the fastest option for newer code.
Because this is one character, you could even use a string: