【问题标题】:computation of multiple logical operators "or" ,"and" in one statement [duplicate]在一个语句中计算多个逻辑运算符“或”“和”[重复]
【发布时间】:2020-04-13 12:45:45
【问题描述】:
print(6 and 0 or 5==9 and 4 and '7' or 0 and 8)
print(4 or 8 and 56==0 or 5 and 0 or 5 and 'hoe' or 0 and 'f')

python中多个逻辑运算符的计算顺序是什么? 是从两边依次吗?

【问题讨论】:

标签: python output logical-operators


【解决方案1】:

它遵循operator precedence 中定义的优先顺序。

简而言之,NOT > AND > OR。

您的代码转换为:

(6 and 0) or (5==9 and 4 and '7') or (0 and 8)
(4) or (8 and 56==0) or (5 and 0) or (5 and 'hoe') or (0 and 'f')

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多