1

I made a very simple function in Python 3.9 (f(x, y, z) = (x and y) or (not x and z)) that prints the result of this operation. What weirds me out is that in some cases it prints 0 and in some others it prints False. For example, f(0, 1, 0) prints 0 but f(1, 0, 1) prints False. Why is that?

Pedro Heck
  • 59
  • 3
  • Even if you consider it very simple, simplifying further (e.g. trying one side of the `or`, then the other) will help find the answer! – Ry- Dec 12 '20 at 21:40
  • you an typecast to integer, `(x and y) or (int(not x) and z)` – Shijith Dec 12 '20 at 21:45

1 Answers1

3

and returns the first value if it's falsey, otherwise it returns the second value. or returns the first value if it's truthy, otherwise it returns the second value. So, and and or won't transmute types. not, however, will always convert to a boolean of the opposite truthiness. So, not 1 becomes False, False and z becomes False, and if x and y is not truthy, then the expression will evaluate to False. That's why you can get different data types.

Aplet123
  • 33,825
  • 1
  • 29
  • 55