Operator Precedence/Boolean Logic

Lawrence D’Oliveiro lawrencedo99 at gmail.com
Wed Jun 22 03:42:10 EDT 2016


On Wednesday, June 22, 2016 at 3:40:22 PM UTC+12, Elizabeth Weiss wrote:
> I am a little confused as to how this is False:
> 
> False==(False or True)
> 
> I would think it is True because False==False is true. 
> 
> I think the parenthesis are confusing me. 

No, it is the meanings of the boolean operators in Python. The rules are:

* boolean operators don’t have to operate on boolean values. The language spec <https://docs.python.org/3/reference/expressions.html#boolean-operations> says:

    “...the following values are interpreted as false: False, None, numeric
    zero of all types, and empty strings and containers (including strings,
    tuples, lists, dictionaries, sets and frozensets). All other values are
    interpreted as true.”

I feel that’s a needlessly complicated rule. It would have been simpler if boolean operators (and conditional expressions like in if-statements and while-statements) only allowed values of boolean types. But that’s one of the few warts in the design of Python...

* the meaning of “A or B” is: “return A if it evaluates to true, else return B”. Correspondingly, the meaning of “A and B” is: “return A if it evaluates to false, else return B”.

Does that give you enough clues to understand what is going on?



More information about the Python-list mailing list