python逻辑运算符的一些小技巧


def fun():
    print('i am a fun...')
    return 'i am a fun!!!'


def funFalse():
    print('i am false...')
    return False



# or
var1 = None or 'is me'    # is me
var2 = 0 or 'is me'    # is me
var3 = False or 'is me'  # is me
print(var1)
print(var2)
print(var3)

var4 = 'is me' or None  # is me
print(var4)


var5 = 'is me' or fun()     # 前面是真 ,所以并不会执行 fun()
print(var5)

var6 = fun() or 'is me'     # i am a fun...
print(var6)

print('===========================')


# and
var7 = fun() and funFalse() # 两个函数都会执行   var7 为False
print(var7)

var8 = True and fun()   # fun()会执行     var8 为i am a fun!!!
print(var8)

var9 = False and fun()   # fun()不会执行     var9 为False
print(var9)

你可能感兴趣的:(python)