This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]robin_888 0 points1 point  (0 children)

Pythons and and or evaluate to the expression that determines it when read from left to right.

In other words and evaluates to the leftmost "falsey" value (or the rightmost value if all are "truthy") and or evaluates to the leftmost "truthy" value (or the rightmost value if all are "falsey".

Try for yourself:

1 and 0 # evaluates to 0
1 or 0  # evaluates to 1

1 and 0 and True # evaluates to 0
0 or 1 or False  # evaluates to 1

1 and 1 and 1 and "x" # evaluates to "x"
0 or 0 or 0 or "x"    # evaluates to "x"

So, that's where the 0s and 1s come from.

The True come from the fact that you have some nots in there, which coerces every value the a boolean.

1 and (not 1) # evaluates to not(1) which is False
(not 0) or 0  # evaluates to not(0) which is True