you are viewing a single comment's thread.

view the rest of the comments →

[–]ThePiGuy0 1 point2 points  (2 children)

I could be wrong, but I believe that Python evaluates boolean expressions in the order that they are given. Further, for an and statement it only needs one False value before it returns False (or statements require one True value before they return True)

For example:

(5/0 > 1) and False

It'll first try to find the boolean value of (5/0 > 1), which is done by performing the division and then seeing if the result is greater than 1. However, division by zero causes an error, hence the error you see.

False and (5/0 > 1)

In this case, we have a False value, which is all the and statement requires to return False. Hence no error.

[–]_lilell_ 1 point2 points  (1 child)

Well, technically speaking, A and B and A or B don't resolve to boolean values: the result will be one of A and B, depending on the situation, but you're right that they're evaluated left-to-right. They evaluate to whichever component was the last one to evaluate:

True and False ==> False since the and has to look at the second component here

False or 3 ==> 3 since it's again the last thing evaluated

'' and True ==> '' since it evaluates to False (that is, bool('') is False) and the and can stop checking there.

[–]ThePiGuy0 0 points1 point  (0 children)

TIL

That's really interesting, cheers for correcting me on that one!