Python Boolean Values

Python Boolean Values

In Python, bool is a subtype of the int type. A bool object has two possible values and is initialized using the Python keywords True and False.

>>> a=True
>>> b=False
>>> type(a), type(b)
(<class 'bool'>, <class 'bool'>)

Boolean objects are accepted as arguments to type conversion functions. With True as an argument, the int() function returns 1 and the float() function returns 1.0; with False, they return 0 and 0.0, respectively. There is also a version of the complex() function that takes a single argument.

If the argument is a complex number, it is treated as a real number with the imaginary coefficient set to 0.

a=int(True)
print ("bool to int:", a)
a=float(False)
print ("bool to float:", a)
a=complex(True)
print ("bool to complex:", a)

Run this code and you will get the following Output

bool to int: 1
bool to float: 0.0
bool to complex: (1+0j)

Leave a Reply

Your email address will not be published. Required fields are marked *