From the documentation:
The following table summarizes the operator precedence in Python, from
lowest precedence (least binding) to highest precedence (most
binding).lambda if – else or and not x in, not in, is, is not, <, <=, >, >=, !=, == ...
So, to answer your question,
a == b or c
Is equivalent to
(a == b) or (c)
The code if gender == "m" or "M"
will work like this: Is gender == 'm"
? If yes, the result is True
. Otherwise, test the “truthiness” of "M"
. Is "M"
“true”? If it is, the result is true. To understand how this works, you should know that all objects have a truthiness associated to it. All non-zero integers, non-empty strings and data structures are True
. 0
, 0.0
, ''
, None
, False
, []
, {}
and set()
are all False
.
For more details, visit How do I test one variable against multiple values?
2
solved Why doesn’t this conditional expression generate a SyntaxError?