python-day04_07 逻辑运算符: and or not

# (6)逻辑运算符:  and or not
# and 逻辑与   其他语言用的&&
"""全真则真,一假则假"""
res = True and True    # True
res = True and False   # False
res = False and True   # False
res = False and False  # False
print(res)
 
# or  逻辑或   其他语言用的||
"""一真则真,全假则假"""
res = True or True    # True
res = False or True   # True
res = True or False   # True 
res = False or False  # False
print(res)

# not 逻辑非  其他语言用的!
res = not True
res = not False
print(res)

# 逻辑短路
"""
无论后面的表达式是True 还是False 都已经无法改变最后的结果,那么直接短路,后面的代码不执行;
(1) True or print("程序执行了 ~ 1111") #无值
(2) False and print("程序执行了 ~ 2222") 

True or print("程序执行了 ~ 1111")
True or True => True
True or False => True
False and print("程序执行了 ~ 2222")
False and True  => False
False and False => False
"""

"""
计算规律:
    先脑补计算当前表达式的布尔值是True还是False
    如果出现了 True or 表达式  或者 False and 表达式的情况,直接返回前者,后面代码不执行
    如果没有出现短路效果,直接返回后者
"""

res = 5 and 6 # 6
"""
True and True =>True
True and False => False
"""
res = 5 or 6  # 5
res = 0 and 999 #0  0=>False
res = 0 or "abc" #abc
print(res)

# 逻辑运算符的优先级
""" 优先级从高到低: () > not > and > or   """
res = 5 or 6 and 7 # 5 or 7 => 5
res = (5 or 6) and 7 # 5 and 7
res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False
res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1)
"""
not (False or False) => True
res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1)
res = True or False and True or True and True
res = True or False or True
res = True or True => True
"""
print(res)
posted @ 2023-06-15 18:36  cuimeng  阅读(39)  评论(0)    收藏  举报