Python 逻辑运算符优先级(not,and,or)

先看案例:

>>> not 1 or 0 and 1 or 2 and 3 or 4 and 5

这条语句的结果是:3

而不是:5

因为 Python 中逻辑运算符有优先级:

not > and > or

所以上述语事实上是:

>>> (not 1) or (0 and 1) or (2 and 3) or (4 and 5)

这里需要注意,在 Python 中,4 and 5 的结果是 5,4 or 5 的结果是 4。

发布了44 篇原创文章 · 获赞 0 · 访问量 1699

猜你喜欢

转载自blog.csdn.net/qq_42067550/article/details/105492375