02-补充:逻辑运算符

#0,None,空 三种值对应的布尔值为false,其余为true

#1、三者的优先级关系:not>and>or,同一优先级默认从左往右计算。
>>> 3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3
False

#2、最好使用括号来区别优先级,其实意义与上面的一样
'''
原理为:
(1) not的优先级最高,就是把紧跟其后的那个条件结果取反,所以not与紧跟其后的条件不可分割

(2) 如果语句中全部是用and连接,或者全部用or连接,那么按照从左到右的顺序依次计算即可

(3) 如果语句中既有and也有or,那么先用括号把and的左右两个条件给括起来,然后再进行运算
'''
>>> (3>4 and 4>3) or (1==3 and 'x' == 'x') or 3 >3
False 

#3、短路运算:逻辑运算的结果一旦可以确定,那么就以当前处计算到的值作为最终结果返回 =》偷懒原则
>>> 10 and 0 or '' and 0 or 'abc' or 'egon' == 'dsb' and 333 or 10 > 4
我们用括号来明确一下优先级
>>> (10 and 0) or ('' and 0) or 'abc' or ('egon' == 'dsb' and 333) or 10 > 4
短路:       0      ''            'abc'                    
            假     假              真

返回:                            'abc'

#4、短路运算面试题:
>>> 1 or 3
  >>> 1 and 3
>>> 3
>>> 0 and 2 and 1
>>> 0 >>> 0 and 2 or 1 >>> 0
>>> 0 and 2 or 1 or 4 >>> 0
>>> 0 or False and 1 False
原文地址:https://www.cnblogs.com/zhubincheng/p/12341470.html