-1

I think understand that print('a' or 'b') returns 'a', since x or y gives x if x is true, which 'a' can be interpreted as? But why does print('a' and 'b') return 'b'?

1 Answers1

1

or returns the first value if it's truthy, otherwise it returns the second value. Meanwhile, and returns the first value if it's falsy, otherwise it returns the second value. This is consistent with the definition of the operators:

True or _ = True
False or x = x
True and x = x
False and _ = False

Also, all non-empty strings are truthy.

Aplet123
  • 33,825
  • 1
  • 29
  • 55