Nov-21-2019, 04:35 PM
Nov-21-2019, 05:15 PM
Must have something to do with the "in" keyword
print('b' in 'abc' == 'a' in 'abc') print(('b' in 'abc') == 'a' in 'abc') print('b' in 'abc' == ('a' in 'abc')) print(('b' in 'abc') == ('a' in 'abc'))
Output:False
False
False
True
Nov-21-2019, 11:45 PM
It has to do with chaining comparison operators.
So what's really happens is:
If most use it so dos
So what's really happens is:
>>> 'b' in 'abc' == 'a' in 'abc' False >>> # The chaining is >>> ('b' in 'abc') and ('abc' == 'a') and ('a' in 'abc') False >>> # After ('abc' == 'a') the result is False and last ('a' in 'abc') is not evaluated >>> ('abc' == 'a') FalseShould avoid to use
==
like this as it can give unexpected result.If most use it so dos
()
break the chaining.>>> ('b' in 'abc') == ('a' in 'abc') True