python 中 逻辑运算符的使用

问题:

not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9

这个问题首先涉及到优先级,or<and<not,所以问题变成

(not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)

再看 and 和 or 的定义:

如果and前面是真值,就输出后面的值,不然就输出0;

如果or前面是真值就输出前面的值,如果是0就输出后面得值。

所以问题变成

0 or 0 or 4 or 6 or 9

从而得到答案4。

另外

3和4调换位置后答案不同是因为

可以通过and的定义解释。

猜你喜欢

转载自blog.csdn.net/Phoebe_D/article/details/82970825