I don't know how Python gets this answer

@Maho,

When in the same comparison expression there are both and and 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 False.

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 or operator).

Hence, we have True only in the second if-statement, that’s why “B” is printed.

1 Like

The evaluation is according to truthy and falsy values, am I right?

Yes, absolutely. Let’s say, the functionality of and and 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

will be:

(True and False) or (False and False)

then

False or False

equals finally to False.

Adding the parenthesis is always a good practice in such cases, in order to avoid confusion.

1 Like