先说结论:从左到右,哪个可以得出结论就输出哪个。
短路逻辑规则如下:
表达式从左至右运算
1. or逻辑
若 or 的左侧逻辑值为 True ,则直接输出 or 左侧表达式 。
若 or 的左侧逻辑值为 False ,则直接输出or右侧的表达式。
2. and逻辑
若 and 的左侧逻辑值为 False ,则直接输出 and 左侧表达式
若 and 的左侧逻辑值为 True,则直接输出and右侧的表达式。
注意:
1、在Python中and的优先级是大于or的,而且and和or都是会返回值的并且不转换为True和False。当not和and及or在一起运算时,优先级为是not>and>or
2、在Python中,None、任何数值类型中的0、空字符串“”、空元组()、空列表[]、空字典{}都被当作False,还有自定义类型,其他对象均为True。
>>> 0 and 2
0
>>> [] and 2
[]
>>> 2 and {}
{}
>>> [] and {}
[]
>>> 2 and 5
5
>>> 5 and 2
2
>>> 0 or 2
2
>>> 2 or []
2
>>> False or 5
5
>>> [] or {}
{}
>>> {} or False or []
[]