python 逻辑运算

'''逻辑运算not and or
例题:求出下列逻辑语句的值。
    8 or 4
    0 and 3
    0 or 4 and 3 or 7 or 9 and 6
'''
print(8 or 4)
8

print(0 and 3)
0

print(0 or 4 and 3 or 7 or 9 and 6)
3

'''逻辑运算的优先级:not and or
对于or:
    如果第一个值转化为bool值为True,则结果=第一个值
    如果第一个值转化为bool值为False,则结果=第二个值
    
对于and:
    如果第一个值转化为bool值为True,则结果=第二个值
    如果第一个值转化为bool值为False,则结果=第一个值

例题:
        3 > 4 or 4 < 3 and 1 == 1
        1 < 2 and 3 < 4 or 1 > 2 
        2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
        not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
'''
print(3 > 4 or 4 < 3 and 1 == 1)
False

print(1 < 2 and 3 < 4 or 1 > 2)
True

print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
True

print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
False

'''数字类型的,只有0的bool值为False
字符串中,只有""空的bool值为False
字符串中," "空白的bool值为True
'''
print(bool(-2))
print(bool(0))
True
False

print(bool(""))
print(bool(" "))
False
True

猜你喜欢

转载自www.cnblogs.com/lilyxiaoyy/p/11924886.html