-1

I try some samples in python console. And was confused for the following:

>>> (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

I was confused for what is the why (1 and None) does not return any thing is line 1? It should be None? And why (2 and 1) equals to 1 but not 2. Sorry for new with Python.

Pai
  • 327
  • 1
  • 3
  • 10

1 Answers1

1

In python, empty string, dict, tuple, list are False. The others are True

(1 and None) is same as if 1 is False return 1 else None that is why (1 and None) return None

same reason == > (2 and 1) return 1 so, it's not 2.

My English isn't good, you can find more information from below links

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