0

When I run print(25 and 3) in python, the output is always the latter value. In this case, it prints 3, except when one of the values(at the place of 25 or 3) is a 0

print(0 and 3) would print 0

Why isn't it returning a boolean value? How exactly is it being processed?

Pushpak Ruhil
  • 176
  • 1
  • 1
  • 9
  • Check this out, it answers your question https://stackoverflow.com/questions/18195322/pythons-logical-operator-and – Iduoad Jan 19 '22 at 15:46

2 Answers2

3

Because and isn't a Boolean operator. It uses Boolean logic, but does not necessarily produce a Boolean value as a result.

It uses the Boolean value of its first operand to determine which operand to evaluate to.

That is, x and y is basically equivalent to

y if bool(x) else x

rather than

bool(y) if bool(x) else bool(x)
chepner
  • 497,756
  • 71
  • 530
  • 681
-1

The short answer would be that these numbers have a boolean value. So 0 is False and anything else is True. For a longer explanation you can see that: https://realpython.com/python-and-operator/

haxor789
  • 604
  • 6
  • 18