When in the same comparison expression there are both
or operators, then the
and operator has priority and is executed first. You can add the parenthesis to this expression, to avoid confusion.
In the first if-statement, we practically have
False or (True and False), which, after doing the comparison in the parenthesis becomes actually
False or False, which finally is
In the second if-statement, we have
True or (False and False), which becomes
True or False, which is equal to
True (according to the functionality of the
Hence, we have
True only in the second if-statement, that’s why “B” is printed.
The evaluation is according to truthy and falsy values, am I right?
Yes, absolutely. Let’s say, the functionality of
or remains the same as usual (with regards to their estimation of truthy and falsy values), only that the execution of
and has priority, when these two operators appear together. It is fair also for the cases when there are several truthy and falsy values and several operators:
True and False or False and False
(True and False) or (False and False)
False or False
equals finally to
Adding the parenthesis is always a good practice in such cases, in order to avoid confusion.