【问题标题】:Does ''not'' affect the whole if statement in python? [duplicate]''not''会影响python中的整个if语句吗? [复制]
【发布时间】:2020-01-21 04:20:11
【问题描述】:

基本上我的问题是说not 会影响整个if 语句,即使我在其中添加了orand,或者它是否只影响不以@987654327 开头的部分@ 或 and

编辑

有些人试图向我发送另一个类似的问题(感谢您的帮助!),但它是在谈论层次结构,而我在谈论 not 是否会影响整个 if 语句。

【问题讨论】:

标签: python python-3.x if-statement negation


【解决方案1】:

所以python语句的工作方式是not只会影响它遇到的下一个真/假语句。 not a and b 将寻找 a 为假且 b 为真的情况,而not (a and b) 将寻找a and b 不同时为真的情况。你甚至可以做b and not a,这会得到与not a and b相同的结果

【讨论】:

  • 虽然b and not a 可能产生与not a and b 相同的结果,但它们并不相同。 Python 有短路布尔运算符:and 是一个短路运算符,所以它只在第一个参数为真时计算第二个参数。
  • @jarmod 是的,请编辑它以使其更清晰
  • 你写的是 NAND (not (a and b)),但你描述的是 NOR(当 a 和 b 都为假时返回真)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
相关资源
最近更新 更多