Why does concatenating a boolean value return an integer?

Posted by joshhunt on Stack Overflow See other posts from Stack Overflow or by joshhunt
Published on 2010-03-09T05:55:01Z Indexed on 2010/03/09 6:06 UTC
Read the original article Hit count: 235

Filed under:

In python, you can concatenate boolean values, and it would return an integer. Example:

>>> True
True
>>> True + True
2
>>> True + False
1
>>> True + True + True
3
>>> True + True + False
2
>>> False + False
0

Why? Why does this make sense?

I understand that True is often represented as 1, whereas False is represented as 0, but that still does not explain how adding two values together of the same type returns a completely different type.

© Stack Overflow or respective owner

Related posts about python