Boolean data type in Python
Python boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions. For example, 1==1 is True whereas 2<1 is False.
Python Boolean Type
The boolean value can be of two types only i.e. either True or False. The output <class ‘bool’> indicates the variable is a boolean data type.
Example: Boolean type
<class 'bool'> <class 'bool'>
Evaluate Variables and Expressions
We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.
Example: Python bool() method
False False False False False True
We can also evaluate expression without using the bool() function also. The Booleans values will be 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.
Example: Boolean value from the expression
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)
Boolean OR Operator
The Boolean or operator returns True if any one of the inputs is True else returns False.
|A||B||A or B|
Example: Python Boolean OR Operator
True Atleast one number has boolean value as True
In the above example, we have used Python boolean with if statement and OR operator that check if a is greater than b or b is smaller than c and it returns True if any of the condition is True (b<c in the above example).
Boolean And Operator
The Boolean and operator returns False if any one of the inputs is False else returns True.
|A||B||A and B|
Example: Python Boolean And Operator
False Atleast one number has boolean value as False
Boolean Not Operator
The Boolean Not operator only require one argument and returns the negation of the argument i.e. returns the True for False and False for True.
Example: Python Boolean Not Operator
Boolean value of a is False
Boolean == (equivalent) and != (not equivalent) Operator
Both the operators are used to compared two results. == (equivalent operator returns True if two results are equal and != (not equivalent operator returns True if the two results are not same.
Example: Python Boolean == (equivalent) and != (not equivalent) Operator
The is keyword is used to test whether two variables belong to the same object. The test will return True if the two objects are the same else it will return False even if the two objects are 100% equal.
Example: Python is Operator
in operator checks for the membership i.e. checks if the value is present in a list, tuple, range, string, etc.
Example: in Operator
Please Login to comment...