# Boolean Data Type in Python

## Boolean Data Type in Python

Boolean data type in Python is one of the primitive data types in Python which takes one of the two values of True and False. It has a class of bool. It is returned as a result of evaluation of expression(s) that may be connected with logical operators to bind two or more expressions together.

### Example of Boolean Data Type in Python:

# Python program to exemplify boolean data type

var1=True
print(var1," is of ",type(var1))

var2=False
print(var2," is of ",type(var2))

Output:

True  is of  <class 'bool'>
False  is of  <class 'bool'>


## Values of Boolean Data Type in Python

Boolean data type has two values, True and False. When an expression is evaluated or values are compared, then the evaluation or comparison results in either of the two boolean values which could be True or False.

### Example of values of Boolean Data Type in Python:

# Python program to exemplify boolean values

num1=23
num2=45
num3=num1<num2
print(num1," < ",num2," is ",num3)

Output:

23  <  45  is  True


## Python bool() Function

Python bool() function returns True or False values which are the boolean values of an expression or a value. It either returns or converts an object into a boolean value. It returns False if the object passed to it is empty like (), [], {}, it is False, it is 0 or it is None.

# Python program to exemplify Python bool() function

num1=23
num2=45
print(bool(num1<num2))

var=None
print(bool(var))

var=()
print(bool(var))

var=[]
print(bool(var))

var={}
print(bool(var))

var="StudyExperts!"
print(bool(var))

Output:

True
False
False
False
False

## Integers and Floats as Booleans

Numbers can be represented as booleans using bool() function in Python. If an integer, a floating point number or a complex number is 0, then it represents a False boolean value, otherwise True boolean value.

# Python program to exemplify int and float as boolean

num1=23
print(bool(num1))

var=0
print(bool(var))

var=0.0
print(bool(var))



Output:

True
False
False


## Boolean Operators in Python

Boolean operators work on boolean operations that have boolean values of True and False. They are:

• or
• and
• not
• == (equivalent)
• != (not equivalent)

### Boolean or Operator

Boolean or operator returns True if any of the operand is True otherwise False. It’s truth table is provided below where A and B are the inputs or the operands:

 A B A or B True True True True False True False True True False False False

### Boolean and Operator

Boolean and operator returns True if all the operands are True otherwise False. It’s truth table is provided below where A and B are the inputs or the operands:

 A B A and B True True True True False False False True False False False False

### Boolean not Operator

Boolean not operator is applied on a single operand. It returns True if it is False otherwise False. It’s truth table is provided below where A and B are the inputs or the operands:

 A not A True False False True

### Boolean == and != Operator

They compare two objects. == return True if they are equal otherwise False while != return True if they are not equal otherwise False.