Reputation: 15
I notice a strange behavior of "in" operation when comparing a string with a tuple containing only 1 string.
'monday' in ('not monday')
the result is True
as if we were comparing 2 strings
but if I change the expression by adding another element in the tuple.
'monday' in ('not monday', 'not monday neither')
it returns False as expected.
any idea why?
Upvotes: 0
Views: 289
Reputation: 579
Because the ('not monday')
does not mean tuple, it means string as same as 'not monday'
, if you want to create an only one unit tuple you can write ('not monday',)
(add a ,
to the end).
good luck!
Upvotes: 0
Reputation: 24691
>>> 'monday' in ('not monday')
True
>>> 'monday' in ('not monday',)
False
A single-element tuple must have the trailing comma. Otherwise, it gets interpreted as regular order-of-operations parentheses, which are meaningless in this case So, 'monday' in ('not monday')
is syntatically identical to 'monday' in 'not monday'
.
Upvotes: 6