Boolean operations have the lowest priority of all Python operations:
expression | ::= | or_test |lambda_form |
or_test | ::= | and_test |or_test "or"and_test |
and_test | ::= | not_test |and_test "and"not_test |
not_test | ::= | comparison | "not"not_test |
lambda_form | ::= | "lambda" [parameter_list]:expression |
In the context of Boolean operations, and also when expressions areused by control flow statements, the following values are interpretedas false:None, numeric zero of all types, empty sequences(strings, tuples and lists), and empty mappings (dictionaries). Allother values are interpreted as true.
The operatornot yields1 if its argument is false,0 otherwise.
The expressionx andy first evaluatesx; ifx is false, its value is returned; otherwise,y isevaluated and the resulting value is returned.
The expressionx ory first evaluatesx; ifx is true, its value is returned; otherwise,y isevaluated and the resulting value is returned.
(Note that neitherand noror restrict the valueand type they return to0 and1, but rather return thelast evaluated argument.This is sometimes useful, e.g., ifs is a string that should bereplaced by a default value if it is empty, the expressions or 'foo' yields the desired value. Becausenot has toinvent a value anyway, it does not bother to return a value of thesame type as its argument, so e.g.,not 'foo' yields0,not''.)
| Python Reference Manual |