I’m just starting to learn Python and was playing around with the in operator in the interpreter and discovered something interesting. Why is this False:
# case 1
>>> [1] in [1,2,3]
False
but these are True?
# case 2
>>> [1] in [[1],2,3]
True
# case 3
>>> (1) in [(1),2,3]
True
# case 4
>>> (1) in [1,2,3]
True
# case 5
>>> 1 in [(1),2,3]
True
I can understand why #1 is False, since [1] is a list object and it does not occur in the list [1,2,3]. I also get why #2 is True. However, if tuples are immutable lists (as Norm Matloff describes them in his tutorial) or are immutable sequences (as the Python documentation for TUPLES says), why is (1) equated to 1 instead of [1]?
Brackets do not make a tuple, a comma does.
So where you do
(1), it actually just means1.