Python 的逻辑运算符
- and :
x and y
若x为False,返回x;
若x为True,返回y;
- or :
x or y
若x为True,返回x;
若x为False, 返回y;
- not
not x
若x为True,返回False;
若x为False,返回True;
也就是Python中逻辑运算和数学中的逻辑运算是有区别的,返回的不一定就是布尔值。只有not一定会返回布尔值。
另外要注意的就是:
0、0.0、0L、0.0+0.0j、None、False、空字符串、空列表、空元组、空字典,视为False;
非空视为True。
a_list = [True, False, True]
b_list = [True, True, False]
print(a_list and b_list) # 输出[True, True, False],即为b_list
print(a_list or b_list)# 输出[True, False, True], 即为a_list
python 逻辑运算符的优先级顺序
or < and < not
逻辑运算符在所有运算符中优先级最低。