Data Type - 'bool' for Boolean Values

This section describes the 'bool' data type, which only has two objects, the True and False objects.

What Is the "bool" Data Type? "bool" is the Boolean data type in Python. It only has two objects, the True and False objects, with the following features:

1. The Boolean objects can be referred by the keywords, True and False, in Python code.

2. The Boolean objects, True and False, are actually mapped to numeric values when used in comparison operations with int, float and complex data types.

```bool    int   float   complex
-----   ---   -----   -------
True      1     1.0      1+0j
False     0     0.0      0+0j
```

Here are some sample code of using Boolean objects in comparison operations:

```>>> True == 1
True
>>> False == 0
True
>>> False < 1
True
>>> False < 2
True
>>> True < 2
True
>>> True < -2
False

>>> True == 1.0
True
>>> True < 2.0
True
>>> True < 0.5
False

>>> True == 1+0j
True
```

3. When Boolean objects are used in the equality operation, ==, with any other non-numeric data type, the result is False. Similarly, the result is True, when used in non-equality operation, !=, with any other non-numeric data type.

```>>> True == '1'
False
>>> True == 'True'
False

>>> True != 'True'
True
>>> False != 'True'
True
>>> False != None
True
>>> False == None
False
```

4. You use the bool(object) function to cast any objects of any type into a Boolean object. Here is the casting logic for an object represented by "obj":

• Casted to the returned value of obj.__bool__() function, if it is defined.
• Else, casted to obj.__len__() > 0, if it is defined.
• Else, casted to False, if obj == None.
• Else, casted to obj, if type(obj).__name__ == 'bool'.
• Else, casted to obj == 0, if type(obj).__name__ == 'int'.
• Else, casted to obj == 0.0, if type(obj).__name__ == 'float'.
• Else, casted to obj == 0j, if type(obj).__name__ == 'complex'.
• Else, casted to True.

Here are some example of using the bool(object) function.

```>>> bool([])
False
>>> bool([0])
True

>>> bool(None)
False

>>> bool(10)
True
>>> bool(0.0)
False

>>> bool(0j)
False
>>> bool(10+0j)
True

# "bool" is the variable name referring to the bool() function object
>>> bool(bool)
True
```