Python boolean type is one of the built-in data types provided by Python, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. The output <class ‘bool’> indicates the variable is a boolean data type.
<class 'bool'> <class 'bool'>
Generally, booleans values are returned as a result of some sort of comparison.
In the example below the variable res will store the boolean value of False after the equality comparison takes place.
Integers and Floats as Booleans
Numbers can be used as bool values by using Python’s built-in bool() method. Any integer, floating-point number, or complex number having zero as a value is considered as False, while if they are having value as any positive or negative number then it is considered as True.
False True True
Boolean Operations are simple arithmetic of True and False values. These values can be manipulated by the use of boolean operators which include AND, Or, and NOT. Common boolean operations are –
- == (equivalent)
- != (not equivalent)
True False False True False True
Booleans are commonly used in the code to make it behave differently. Boolean can be used in conjunction with conditional statements to make it simpler. In many cases multiple conditions are needed to evaluate, for this purpose, AND and OR keywords are used. The AND returns True only if both the conditions are true, while OR returns true if any one of the conditions is true.
geater or equal to 10
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course