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.
Python concatenate a boolean to a string Here, str(boolean_v) is used to convert boolean to string. The “+” operator is used to concatenate the value to a string.
Convert bool to string: str() You can convert True and False to strings 'True' and 'False' with str() . Non-empty strings are considered True , so if you convert False to strings with str() and then back to bool type with bool() , it will be True .
The concat() method of Booleans Class in the Guava library is used to concatenate the values of many arrays into a single array. These boolean arrays to be concatenated are specified as parameters to this method. For example: concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c} returns the array {a, b, c}.
Use int for casting a Boolean value in Python. Using int() method will convert Boolean to Int, 1 for True and 0 for False.
Because In Python, bool is the subclass/subtype of int.
>>> issubclass(bool,int)
True
Update:
From boolobject.c
/* Boolean type, a subtype of int */
/* We need to define bool_print to override int_print */
bool_print
    fputs(self->ob_ival == 0 ? "False" : "True", fp);
/* We define bool_repr to return "False" or "True" */
bool_repr
    ...
/* We define bool_new to always return either Py_True or Py_False */
    ...
// Arithmetic methods -- only so we can override &, |, ^
bool_as_number
    bool_and,       /* nb_and */
    bool_xor,       /* nb_xor */
    bool_or,        /* nb_or */
PyBool_Type
    "bool",
    sizeof(PyIntObject),
    (printfunc)bool_print,          /* tp_print */
    (reprfunc)bool_repr,            /* tp_repr */
    &bool_as_number,                /* tp_as_number */
    (reprfunc)bool_repr,            /* tp_str */
    &PyInt_Type,                    /* tp_base */
    bool_new,                       /* tp_new */
                        Replace "concatenate" with "add" and True/False with 1/0, as you've said, and it makes perfect sense.
To sum up True and False in a sentence: they're alternative ways to spell the integer values 1 and 0, with the single difference that str() and repr() return the strings 'True' and 'False' instead of '1' and '0'.
See also: http://www.python.org/dev/doc/maint23/whatsnew/section-bool.html
True is 1
False is 0
+ is ADD
Try this:
IDLE 2.6.4      
>>> True == 1
True
>>> False == 0
True
>>> 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With