0 or 1 and True
为什么返回True,而不是1
a = 0 or 1 and True
print(a)
0 or 1 and True
为什么返回True,而不是1
a = 0 or 1 and True
print(a)
在python中,and, or运算符不返回布尔值。他们返回最后评估的东西。
x or y:如果x为假,则为y,否则为x
x and y:如果x为假,则为x,否则为y,x、y都真则为y。
也就是说,0 or 1 and True:
and优先级高级or :
1 and True:它先判断1是不是真,是,然后判断True是不是真,是,返回最后的真值(最后评估的东西):True。
0 or True:它先判断1是不是真,不是,再去判断True是不是真,是,返回最后的真值。
当你变成1 or 1 and True:同理反1(1 or True,1为真了,后面的条件不需求去判断了,直接返回评估的东西)