PythonNewHand PythonNewHand - 1 year ago 33
Python Question

Why a comparison with a comma at the end "(1,) == 1," gives a tuple "(False,)" in Python?

I'm testing the tuple structure, and I found it's strange when I use the '==' operator like:

>>> (1,) == 1,
Out: (False,)

When I assign these two expressions to a variable, the result is true:

>>> a = (1,)
>>> b = 1,
>>> a==b
Out: True

This questions is different from Python tuple trailing comma syntax rule in my view. I ask the group of expressions between '==' operator.


This is just operator precedence. Your first

(1,) == 1,

groups like so:

((1,) == 1),

so builds a tuple with a single element from the result of comparing the one-element tuple 1, to the integer 1 for equality They're not equal, so you get the 1-tuple False, for a result.