Tuple equality in python suprises [closed]
Solution 1:
In the first example, Python is making a tuple of two things:
- The expression:
(1,1) == 1
(Which evaluates toFalse
) - The integer:
1
Adding parentheses around the order in which actions are performed might help you understand the output:
(1,1) == 1,1
is the same thing as
((1,1) == 1), 1)
Here, (1,1) == 1)
evaluates to False
, so you get the output
(False, 1)
In the same way,
1,1 == (1,1)
translates to 1, (1 == (1,1))
which becomes
(1, False)
and
1,1,1 == (1,1,1)
translates to 1,1, (1 == (1,1,1))
which is
(1, 1, False)
In this example, Python is making a tuple of three things:
- The two
1
's - The expression:
1 == (1,1,1)
(Which evaluates toFalse
)