Python3,逻辑运算符
优先级 ()>not>and>or 1.or 在python中,逻辑运算符or,x or y, 如果x为True则返回x,如果x为False返回y值。因为如果x为True那么or运算就不需要在运算了,因为一个为真则为真,所以返回x的值。如果x的值为假,那么or运算的结果取决于y,所以返回y的值。2 1 print(1 or 2) # 1 2 print(3 or 2) # 3 3 print(0 or 2) # 2 4 print(0 or 100) # 100 5 print(0 or 0) #0 2.and 在python中,逻辑运算符and,x and y,如果x为True则返回y值。如果x为False则返回y值。如果x的值为True,and的运算不会结束,会继续看y的值,所以此时真与假取决于y的值,所以x如果为真,则返回y的值。如果x为假,那么and运算就会结束运算过程了,因为有一个为假则and为假,所以返回x的值。 1 print(1 and 2) # 2 2 print(3 and 0) # 0 3 print(0 and 2) # 0 4 print(3 and 2) # 2 5 print(0 and 0) # 0 3.混合解析 按照从左向由,优先级高的先执行优先级高的规则,首先因为 比较运算符优先级高于逻辑运算符 ,很简单