python代表什么和代表什么

时间:2016-12-16 13:01:51

标签: python

我在python控制台中尝试了一些示例。并且对以下内容感到困惑:

>>> (1 and None)
>>> (1 and None) == None
True
>>> (1 or None) == None
False
>>> (1 and 2) == 2
True
>>> (2 and 1) == 2
False
>>> (2 and 1) == 1
True

我很困惑,为什么(1和None)没有返回任何东西是第1行?它应该是None?为什么(2和1)等于1但不是2.抱歉新的Python。

1 个答案:

答案 0 :(得分:1)

在python中, 空字符串,字典,元组,列表都是假的。 其他都是真的

(1 and None)if 1 is False return 1 else None相同 这就是为什么(1和无)返回无

同样的原因==> (2和1)返回1,所以它不是2。

我的英语不好,您可以从以下链接中找到更多信息

  1. https://docs.python.org/3/library/stdtypes.html#truth-value-testing
  2. https://docs.python.org/3/reference/expressions.html#boolean-operations