-2

I though I understood Python the "and" operator. But after seeing

assert (None and None) is None

it was apparently that my understanding was not precise. Which was that None and None would be the same as bool(None) and bool(None).

Does anybody have definition of the "and" operator and can explain the logic.

Peter Mølgaard Pallesen
  • 1,470
  • 1
  • 15
  • 26

2 Answers2

2

and returns the first value if it is "falsey"*. Otherwise it returns the second one.

For example:

3 and 6 -> 6
0 and 7 -> 0
[] and 'abc' -> []

* a thing is falsey if bool(thing) is False

zvone
  • 18,045
  • 3
  • 49
  • 77
2

According to the official documentation:

help('and')

[...] The expression "x and y" first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. [...]

asmartin
  • 459
  • 1
  • 3
  • 12