Boolean data type in Python [Explained with Examples]
Table of Contents
Introduction to Boolean data type in Python
Boolean is an inbuilt data type in python that can have only two values i.e, True or False. The boolean data types are mainly used in conditional statements; it can be evaluated as True or False and loops to control the flow of the program based on whether a condition is true or false.
Boolean data type can increase the efficiency, readability and flexibility of the code. Overall the boolean data type is a simple and powerful tool in programming.
Table of Contents
Why are boolean data types mainly used for?
Booleans are good for logical operations and decision making. By checking the conditions with these boolean data types it can prove the efficiency of the programme. It is having two values that is “True” and “False”
In an e-commerce website the role of boolean is more. The boolean data type can be used in various ways to improve user experience and make the website more efficient.
- Availability of product: boolean data type can be used to determine whether a product is in stock or not. So, when a product comes to zero it will show the boolean as False and in the website it will display as “out of stock”.
- User active or not: The boolean data type will show whether the user is active or not. For example when a user is logged in the active state will become true and it will display the user is active.
- Order status: Boolean data types can be used to represent the status of an order, such as whether it has been placed or not. For example, a Boolean variable can be associated with each order to indicate whether it has been placed by the user or not.
- Cancellation of order: It can be used to represent the cancellation of orders. Such as whether the user has cancelled the order or not. For example a boolean variable can be associated with each order to indicate whether the user has cancelled order or not.
- Recommendation of products: Boolean data types can be used to represent product recommendations, such as whether a product is recommended for a user or not. For example, a Boolean variable can be associated with each recommended product to indicate whether it is recommended for the user or not.
Boolean Operators
- and : It will return the value as True when both the operands are true.
- or : Or operator returns True if any of one operand is True.
- not : Returns true if the operant is False and vice versa
- equal to == : Returns True if both the operands are True
- not equal to ! = Returns True if the opponents are not equal.
- greater than (>): Returns True if the left operand is greater than the right operand
- l ess than (<): Returns True if the operand on the left is less than the operand on the right.
- greater than or equal to (>=): Returns True if the operand on the left is greater than or equal to the operand on the right.
- less than or equal to (<=): Returns True if the operand on the left is less than or equal to the operand on the right.
Let’s move with some examples of these operators;
1. AND operator
AND operator returns True if both the operands are true and False otherwise.
x = 2
y = 5
if x > 1 and y < 10:
print("Both conditions are true")
In the above example, the condition will execute if both the conditions are same. Here x = 2 and y = 5 and the condition is x > 1 and y < 10. So, both the conditions are True.
2 is greater than 1 and 5 is less than 10. The AND operator returns True if both the operands are True. It will print the conditions as True
We can see one more example;
x = 2
y = 5
if x > 3 and y < 10:
print("TRUE")
else:
print("FALSE")
In this example, we can see x is 2 and y is 5 and the condition comes like if x is greater than 3 and y is less than 10. Here x is 2 so, 2 can’t be greater than 3 and this condition is wrong but 5 is less than 10 and this condition is correct.
The And operator will only work when both the operands are True. Here one condition is True and the other is False. So, it will print False only
2. OR operator
OR operator returns True if at least one operand is true
x = 2
y = 5
if x > 0 or y < 4:
print("One condition is true")
From the example shown, x is 2 and y is 5 and the condition is like x is greater than 0 i.e, 2 is greater than zero or y is less than 4 i.e, 5 is less than 4. Here the first condition is True and the other is False. But the OR operator returns True if any of the conditions is True so, the output is TRUE.
x = 2
y = 5
if x > 10 or y < 4:
print("True")
else:
print("FALSE")
In the above example, both the conditions are False so the output will be False.
3. NOT operator
NOT operator returns the opposite boolean value of the operand. If the operand is True, the NOT operator will return False and vice versa.
x = 5
y = 2
if not x < y:
print("x is not greater than y")
From the example, x is 5 and y is 2 and the condition is x less than y. The 5 can’t be less than 2 and the condition is incorrect but, here we are using the NOT operator it will return the wrong condition.
So, the if condition will execute because the NOT operator returns True when applied to the expression “ x < y”.
x = 5
y = 2
if not x > y:
print("x is greater than y")
else:
print("FALSE")
In this example, the if condition is True. But, the NOT operator will not return the True condition it will return the opposite. So, the output in the above example is “FALSE”.
4. Equal to operator
Equal to operator returns True if both the operands are True.
x = 10
if x == 10:
print("x is equal to 10")
From this example, the if condition will execute because x is 10 and the condition is checking whether the x is equal to 10. So, it will return True
x = 10
if x == 5:
print("x is equal to 5")
else:
print("x is not equal to 5")
In this example, it will go to the else part because the if condition is incorrect so, the Output is False.
5. Not equal to
Not equal to operator is used to compare two operands and returns True if they are not equal.
x = 10
if x == 5:
print("x is equal to 5")
else:
print("x is not equal to 5")
In this example the if condition will execute because the Not equal to operator returns True when comparing the values of x with 5. Because, 2 is not equal to 5.
6. Greater than(>)
It will return True if the left operand is greater than right operand
x = 10
y = 5
print(x > y)
#output is True
Here x is greater than y so, the output is True
7. Less than(<)
Returns True if the operand of the left is less than right
x = 5
y = 10
print(x < y)
#output is True
In the example, x < y returns True because 5 is less than 10
8. Greater than or equal to (>=)
Return True if the operand of the left is greater than or equal to the operand of right.
x = 5
y = 10
print(x >= 5)
#output is True
In the above example, x >= 5, returns True because x is equal to 5, which is greater than or equal to 5.
9. Less than or equal to (<=)
Returns True if the operand on the left is less than or equal to the operand on the right.
x = 5
y = 10
print(y <= 10)
#output is True
From the example, y <= 10, returns True because y is equal to 10, which is less than or equal to 10.
Bool() function
The bool() function is used to convert other data type values to their corresponding boolean value. Any value can be used to convert to a boolean value using the bool() function.
The bool() function returns False for empty sequences or collections, such as empty strings, empty lists, empty dictionaries, and empty tuples.
It will return True for non-empty sequences or collections like non-empty strings, non-empty lists, non-empty dictionaries, and non-empty tuples as well as for any non-zero numeric value.
Here are a few examples of using the bool() function in Python:
1. Converting an empty list to a boolean value
my_list = []
print(bool(my_list))
#output is FALSE
In this example, the variable “my_list” has an empty list and it is converted to a boolean. Here, the output will print as “False” an empty list should be considered as a False value in python so, the bool() function returns ‘False’.
2. Converting a non-empty tuple in to boolean
my_tuple = (10, 20, 30)
print(bool(my_tuple))
#output is TRUE
From this example the variable “my_tuple” has a non-empty tuple and it is converted to boolean. We know that the non-empty tuple will give True value in python so the bool() function returns True.
3.Converting integers to boolean values
num1 = 0
print(bool(num1))
#output is FALSE
num2 = 1
print(bool(num2))
#output is TRUE
num3 = 50
print(bool(num3))
#output is TRUE
In the example, the bool function converts three different integers to boolean values. The first variable “num1” have 0 value when it is converting to bool() function it will return FALSE because,
0 should be considered as a FALSE value in python
In the second variable “num2” has a value 10 when it is converted to bool() function it will return as TRUE. In python 1 is considered as TRUE value so, the output will be TRUE.
The third integer is 50 so, when it is converting to bool() function, the output will be a TRUE value.
4.Converting floating point numbers to boolean value
num1 = 0.0
print(bool(num1))
#output is FALSE
num2 = 2.12
print(bool(num2))
#output is True
In this example, we use the bool() function to convert two different floating point numbers to boolean values. The variable “num1” has the value 0.0, which is considered to be a FALSE value in Python, so the bool() function returns FALSE.
The second variable “num2” has the value 2.12, which is a TRUE value, so the bool() function returns TRUE as the output.
5.Converting string to boolean
string1 = ' '
print(bool(string1))
#output is False
string2 = 'Goom'
print(bool('string2'))
#output is True
string3 = '0'
print(bool(string3))
# Output: True
string4 = None
print(bool(string10))
# Output: False
In this example, we use the bool() function to convert two different strings to boolean values.
The first variable “string1” has an empty string, which is considered to be a False value in Python, so the bool() function returns False.
The second variable “string2” is the non-empty string ‘Goom’, which is a TRUE value, so the bool() function returns True.
In the 3rd variable “string3” contains a non-empty character (‘0’), so its boolean value is True. In Python, any non-empty string, even if it contains a single character, evaluates to True when converted to a boolean value using the bool() function.
The variable “string4” has the value None, which is a special value in Python that represents the absence of a value. When it is converting to a boolean value using the bool() function, None always evaluates to False.