问题
I am currently in the course of learning Python 2.7 and have come across the Equality and Boolean operators
My question is:
Why False and 1
is False
but True and 1
is 1
Likewise, False or 1
is 1
but True or 1
is True
Can someone kindly explain why this is happening
Many thanks
回答1:
and returns the first 'falsy' (False, zero, empty string or list, etc.) value it sees, or the final value if none were falsy. Further values are not even evaluated, since they can't change the result.
or likewise returns the first 'truthy' (True, non-zero, non-empty string or list, etc.) value it sees (or the final one if there were none), and doesn't evaluate the rest.
This behavior is sometimes more convenient than strictly returning only True or False.
来源:https://stackoverflow.com/questions/37889196/python-2-7-boolean-operators-logic