Python Boolean built-in data type

The Boolean in Python represent a subclass of integers consisting only of two values ‘True’ and ‘False’.The two value True and False is represented by the integer 1 and 0.You can use the values in any ways you want say in mathematical operation.However,the main purpose of Boolean value are for “iterating and Making Decisions” which we will see in the next chapter.

Boolean can be also used in expression consisting of logical expression like ‘and’,’or’ and ‘not’.Since Boolean values directly represent 1 and 0,what 1 and 0 can do they can also do,to sum it up Boolean values can do pretty much anything.

A simple code example using Boolean value is given below.

>>> int(True)
1
>>> int(False)
0

 

>>> bool(1)
True
>>> bool(0)
False

Example with logical operation.

>>> not True
False
>>> False or False
False
>>> not False
True

I am sure you know what each of the logical value and,nor and or mean.

Another example using Boolean for mathematical operation.

>>> True + True
2
>>> True - False
1
>>> 10 + True
11
>>> 10/True
10.0




Leave a Reply

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