Arindam Roychowdhury Arindam Roychowdhury - 1 month ago 17
Python Question

Why is True + 2 , 3 or False + 2, 2

>>> True + 2
3
>>> False + 2
2


I can understand that somehow, True means 1 and False means 0 . So
does it mean, a Boolean and integer operation always gives an integer?

Answer

In python bool is a subclass of int, and therefor satisfies the "is-a" relation, meaning a bool is-a int.

To demonstrate:

issubclass(bool, int)
=> True
isinstance(True, int)
=> True

In practice this means that in any operation which works on an int, the int can be substituted with a bool.