Why is (()) equal to ()?

>>> (()) == () True >>> (()) () 
+7
source share
3 answers

() is a 0-tuple. (foo) results in a foo value. Therefore, (()) leads to a 0-tuple.

From the textbook :

; a tuple with one element is constructed following the value with a comma (it is not enough for the value in parentheses).

+12
source

For the same reason that (4) == 4 : adding parentheses around an expression does not change its value (if it had not been otherwise grouped differently, of course).

Note that ( foo ) not a 1-tuple. Otherwise, things like 3 * (4 + 5) would be a mistake, since (4 + 5) would be a 1-tuple containing 9, and you cannot add a number to the 1-tuple.

+6
source

Now I see. From the textbook .

; a tuple with one element is constructed following the value with a comma (it is not enough for the value in parentheses).

So (()) is not a tuple that contains an empty tuple - it is that tuple: ((),)

+2
source

All Articles