This question already has an answer here:
I was doing a leetcode question while I had the following python code:
pairs = [(1,3)]
addend = 3
sum = 4
if ((addend, sum-addend) or (sum-addend,addend)) in pairs:
print("Pair exists")
The expected output when I ran this code should be
Pair exists
But instead this prints nothing, which I assumed ((addend, sum-addend) or (sum-addend,addend)) evaluates to False.
Then I removed the outer parentheses and made it
if (addend, sum-addend) or (sum-addend,addend) in pairs:
print("Pair exists")
This gave me the right output.
My second guess was this pair of redundant parentheses actually calculates ((addend, sum-addend) or (sum-addend,addend)), so I put ((1,3) or (3,1)) in the Python3.7 console directly and that's the output
>>> ((1,3) or (3,1))
(1, 3)
But still this won't make sense since (1,3) is indeed in pairs.
Could anybody explain why putting these parentheses invalidates the statement? Thanks!
Aucun commentaire:
Enregistrer un commentaire