Boolean data type in Python - GeeksforGeeks

28 Mar, 2022
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. 
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.
Python3
a = Truetype(a) b = Falsetype(b)
Output:
<class 'bool'>
<class 'bool'>
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. 
Syntax:
bool([x])
Python3
# Python program to illustrate# built-in method bool() # Returns False as x is not equal to yx = 5y = 10print(bool(x==y)) # Returns False as x is Nonex = Noneprint(bool(x)) # Returns False as x is an empty sequencex = ()print(bool(x)) # Returns False as x is an empty mappingx = {}print(bool(x)) # Returns False as x is 0x = 0.0print(bool(x)) # Returns True as x is a non empty stringx = 'GeeksforGeeks'print(bool(x))
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.
Python3
# Declaring variablesa = 10b = 20 # Comparing variablesprint(a == b)
Output:
False
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.
Python3
var1 = 0print(bool(var1)) var2 = 1print(bool(var2)) var3 = -9.7print(bool(var3))
Output:
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 –
or
and
not
== (equivalent)
!= (not equivalent)
The Boolean or operator returns True if any one of the inputs is True else returns False.
Python3
# Python program to demonstrate# or operator a = 1b = 2c = 4 if a > b or b < c:    print(True)else:    print(False) if a or b or c:    print("Atleast one number has boolean value as True")
True
Atleast one number has boolean value as True
In he 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).
The Boolean and operator returns False if any one of the inputs is False else returns True.
Python3
# Python program to demonstrate# and operator a = 0b = 2c = 4 if a > b and b<c:    print(True)else:    print(False)     if a and b and c:    print("All the numbers has boolean value as True")else:    print("Atleast one number has boolean value as False")
False
Atleast one number has boolean value as False
The Boolean Not operator only require one argument and returns the negatation of the argument i.e. returns the True for False and False for True.
Python3
# Python program to demonstrate# not operator a = 0 if not a:    print("Boolean value of a is False")
Boolean value of a is False
Both the operators are used to compred two results. == (equivalent operator returns True if two results are equal and != (not equivalent operator returns True if the two results are not same.
Python3
# Python program to demonstrate# equivalent an not equivalent# operator a = 0b = 1 if a == 0:    print(True)     if a == b:    print(True)     if a != b:    print(True)
True
True
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.
Python3
# Python program to demonstrate# is keyword  x = 10y = 10 if x is y:    print(True)else:    print(False) x = ["a", "b", "c", "d"]y = ["a", "b", "c", "d"] print(x is y)
True
False
in operator checks for the membership i.e. checks if the value is present in a list, tuple, range, string, etc.
Python3
# Python program to demonstrate# in keyword # Create a listanimals = ["dog", "lion", "cat"] # Check if lion in list or notif "lion" in animals:    print(True)
True
nikhilaggarwal3
gulshankumarar231
adipratapthakur
varshagumber28
Picked
Python-datatype
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python