Python 2.7 Boolean Operators Logic

妖精的绣舞 提交于 2021-02-05 06:39:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!