Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 == 0 in (0,1) is False; why? [duplicate]

Tags:

python

How does Python parse the first of the following three expressions? (I expected it to be the same as the second, since == and in have the same precedence.)

>>> 1 == 0 in (0,1), (1==0) in (0,1), 1 == (0 in (0,1))
(False, True, True)
like image 678
Alan Avatar asked May 01 '15 21:05

Alan


People also ask

Is false == false true?

False == (False or True) In this expression python would first solve the comparison operator in bracket. => (False or True) is said to be True. Because the 'OR' operator search for the first truthy value, and if found it returns the value else 'False'.

Why is 0 true and 1 false?

Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

Why does true == true return false?

Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . So given this code true == 'true' , it finds this: "If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ."

Does 1 equal True or false?

Like in C, the integers 0 (false) and 1 (true—in fact any nonzero integer) are used.


1 Answers

See the documentation of comparison operators: they are chained rather than grouped. So 1 == 0 in (0,1) is equivalent to (1==0) and (0 in (0,1)), which is obviously false.

like image 162
Alan Avatar answered Sep 26 '22 23:09

Alan