python中的“and”、“or”运算规则

# 1、所有变量的位操作都是通过强制转换成bool实现
# 2、在没有括号的情况下,and优先级高于or
# 3、计算逻辑:
"""
x and y 表示: if x is false, then x, else y
x or y 表示: if x is false, then y, else x
not x 表示: if x is false, then ture, else false
"""

v1 = 1 or 3
v2 = 1 and 3
v3 = 0 and 2 and 1
v4 = 0 and 2 or 1
v5 = 0 and 2 or 1 or 4
v6 = 0 or False and 1

print(v1)  # bool(1)==true, bool(3)==true, 1
print(v2)  # bool(1)==true, bool(3)==true, 3
print(v3)  # bool(0)==false, bool(2)==ture, bool(1)==true, 0and2==0, 0and1==0, 0
print(v4)  # false and ture or ture, 0and2==0, 0or1==1, 1
print(v5)  # false and ture or ture or ture, 0and2==0, 0or1==1, 1or4==1, 1
print(v6)  # false or false and true, 0 or False==False, False and 1==False, False

posted @ 2022-10-24 11:57  随风飘-挨刀刀  阅读(900)  评论(0编辑  收藏  举报
Top