Phyton Why is `True is False == False`, False in Python? [duplicate]

This question already has an answer here:
Answer:1

Based on python documentation about operator precedence :

Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section.

So actually you have a chained statement like following :

>>> (True is False) and (False==False)
False

You can assume that the central object will be shared between 2 operations and other objects (False in this case).

And note that its also true for all Comparisons, including membership tests and identity tests operations which are following operands :

in, not in, is, is not, <, <=, >, >=, !=, ==

Example :

>>> 1 in [1,2] == True
False
Answer:2

Python has a unique transitive property when it comes to the comparison operators. It will be easier to see in a simpler case.

if 1 < x < 2:
    # Do something

This does what it looks like. It checks if 1 < x and if x < 2. The same thing is happening in your non-parenthesized code.

>>> True is False == False
False

It is checking whether True is False and False == False, only one of which is true.

Answer:3

This is a double inequality which gets expanded as (True is False) and (False == False). See for instance What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)

Answer:4



Is there a simple way to code in one line a sequence of map/filter/reduce functions? For example instead of: reduce(lambda x, y: x*y, filter(lambda x: x>0, map(lambda x: x - 1, some_list))) I ...

Is there a simple way to code in one line a sequence of map/filter/reduce functions? For example instead of: reduce(lambda x, y: x*y, filter(lambda x: x>0, map(lambda x: x - 1, some_list))) I ...

I use: Python 2.7.15, OpenSSL 1.1.0h (27 Mar 2018), MS Exchange 2007. My MS exchange allows to send login/pass only after STARTTLS. In python I try to connect to server like: from stmplib import ...

I use: Python 2.7.15, OpenSSL 1.1.0h (27 Mar 2018), MS Exchange 2007. My MS exchange allows to send login/pass only after STARTTLS. In python I try to connect to server like: from stmplib import ...