You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么布尔表达式print(False == True and not True)运行结果为False?

布尔表达式结果不符合预期的原因说明

你产生预期偏差的核心原因是对Python的运算符优先级规则理解有误,按官方规则拆解False == True and not True的计算过程就能明确问题:

  • 首先明确相关运算符的优先级从高到低排序为:not > 比较运算符(==)> and
  • 正确计算步骤:
    1. 优先计算优先级最高的not True,得到结果False,表达式简化为 False == True and False
    2. 其次计算优先级次高的比较运算False == True,得到结果False,表达式简化为 False and False
    3. 最后计算and运算:and左侧为假时直接返回假,最终结果为False
  • 你预期得到True,大概率是默认按照错误的运算顺序做了计算,比如误以为运算顺序是False == (True and not True):
    按这个错误顺序计算时,True and not True结果为False,False == False结果为True,刚好匹配你的预期,但这个运算顺序不符合Python的官方优先级规则。
    如果要得到你预期的结果,需要手动给要优先计算的部分加上括号,即修改表达式为print(False == (True and not True))。

内容的提问来源于stack exchange,提问作者Emily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 13:27:03