-3

When printing statements like:

print(2 + 3 and 4 > 12)

the output is False. I can't understand why and how 5 and False returns False. Can anyone please explain how its being evaluated? How does integers and booleans interact?

Shounak Das
  • 515
  • 4
  • 19
  • 4
    Because *anything* `and False` is `False` – Nick Feb 01 '21 at 06:00
  • What *would* you expect it to return? – deceze Feb 01 '21 at 06:01
  • 1
    Stack Overflow is not intended to replace existing tutorials and documentation. You need to repeat your materials on basic Boolean logic. – Prune Feb 01 '21 at 06:02
  • @Nick I understood that. But why a number `and False` is `False`. I mean, how is it evaluated by the compiler? – Shounak Das Feb 01 '21 at 06:04
  • 1
    @Shounak because 5 is "truthy". See this [post.](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – ddejohn Feb 01 '21 at 06:06
  • Again, this is already covered in documentation on evaluating Boolean expressions. Please refer to those materials and point out exactly what you don't understand. – Prune Feb 01 '21 at 06:06
  • Because, boolean is a subclass of integer for historical reasons. In python 3.x, 0 is false and 1 is true. Other integers also assert 'true' e.g. if 3: print('yayy true') – Muhammad Junaid Haris Feb 01 '21 at 06:07
  • See the [manual](https://docs.python.org/3/reference/expressions.html#boolean-operations) – Nick Feb 01 '21 at 06:07
  • @Muhammad `bool` being a subclass of `int` is irrelevant here and does not contribute to this result. – deceze Feb 01 '21 at 06:08
  • Also relevant: https://stackoverflow.com/a/9299079/476 – deceze Feb 01 '21 at 06:12

1 Answers1

1

The boolean A and B is only true when both A and B are true. In this case, 5 is "truthy", but False is, well, not true.

ddejohn
  • 8,775
  • 3
  • 17
  • 30