-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?

shx2
  • 57,957
  • 11
  • 121
  • 147
Arindam Roychowdhury
  • 4,939
  • 5
  • 53
  • 57

1 Answers1

3

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.

shx2
  • 57,957
  • 11
  • 121
  • 147