Python Boolean is one of the built-in data types. It is used to represent the truth value of an expression. For example, the expression 3 <= 5 is True, while the expression 2 == 4 is False.
Python Boolean Type
A Boolean type in Python has only two possible values:
- True
- False
The variables under the Boolean data type belong to the ‘bool’ class, as shown in the following example:
Example
# Python Boolean type example
# defining and initializing the variables
x = True
y = False
# printing the data type of the variables
print(x, "->", type(x))
print(y, "->", type(y))
Output:
True -> <class 'bool'>
False -> <class 'bool'>
Explanation:
In this example, we have initialized a few variables with Boolean values (True and False) and used the type() function to return their types. As a result, these variables belong to the ‘bool’ class.
Note: The variable initialized with Boolean values must be capitalized. The lowercase values such as true or false will raise a NameError exception.
Evaluating Variables and Expressions
The evaluation of the variables and values can be done with the help of the Python bool() function. This function allows us to convert a value to a Boolean value – True or False, using the standard way of testing truth.
Python bool() Function
In Python, the bool() function is used to convert a value or expression to its corresponding Boolean value, either True or False. The syntax of this function is shown below:
Syntax:
bool(value)
Here, the ‘value’ parameter can be any Python object that needs to be converted into Boolean. If it is omitted, the function will default to False.
Let us take a look at a simple example given below:
Example
# Python example to show the use of the bool() function
# numbers
print("0 ->", bool(0)) # Returns False as value is 0
print("0.0 ->", bool(0.0)) # Returns False as value is 0.0 (float)
print("1 ->", bool(1))
print("-5 ->", bool(-5))
# strings
print("'' ->", bool('')) # # Returns False as value is an empty string
print("'Tpoint Tech' ->", bool('Tpoint Tech'))
# lists
print("[] ->", bool([])) # Returns False as value is an empty list
print("[11, 12, 13] ->", bool([11, 12, 13]))
# tuples
print("() ->", bool(())) # Returns False as value is an empty tuple
print("(11, 12, 13) ->", bool((11, 12, 13)))
# sets
print("{} ->", bool({})) # Returns False as value is an empty set
print("{11, 12, 13} ->", bool({11, 12, 13}))
# None
print("None ->", bool(None)) # Returns False as value is None
Output:
0 -> False
0.0 -> False
1 -> True
-5 -> True
'' -> False
'Tpoint Tech' -> True
[] -> False
[11, 12, 13] -> True
() -> False
(11, 12, 13) -> True
{} -> False
{11, 12, 13} -> True
None -> False
Explanation:
In the above example, we have used the bool() function to convert the specified values of different data types like numbers, strings, lists, tuples, sets, and None types into their corresponding Boolean values. As a result, the numerical values, such as 0 and 0.0, along with the empty sequences and None Type, have returned False, while the other values are converted to True, respectively.
Boolean Operators in Python
In Python, Boolean operators are used to perform logical operations on Boolean expressions. They return True or False on the basis of the logical relationships.
There are mainly three Boolean operators in Python:
| Operator | Name | Description |
|---|---|---|
| and | Boolean AND | This operator returns True if both operands are true. |
| or | Boolean OR | This operator returns True if at least one operand is true. |
| not | Boolean NOT | This operator reverses the Boolean value. |
Let us understand the following these operators with the help of examples.
Boolean AND Operator
Boolean AND Operator returns True if both the operands are true. In case any one of the inputs turns out to be false, the expression will return False.
The following truth table represents the working of the AND operator:
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
The following is a simple example of the Boolean AND Operator:
Example
# Python program to show Boolean AND operator
# printing results
print("True and True =>", True and True)
print("True and False =>", True and False)
print("False and True =>", False and True)
print("False and False =>", False and False)
print("5 > 2 and -4 < 0 =>", 5 > 2 and -4 < 0)
print("0 > 1 and 6 < 8 =>", 0 > 1 and 6 < 8)
Output:
True and True => True
True and False => False
False and True => False
False and False => False
5 > 2 and -4 < 0 => True
0 > 1 and 6 < 8 => False
Explanation:
Here, we have performed various operations using the AND operator. This example shows that the Boolean AND operator returns True only when both the conditions are true; otherwise, it returns False.
Boolean OR Operator
Boolean OR operator returns True if at least one of the operands is true. If both the operands turn out to be false, the result will be False.
The following truth table represents the working of the OR operator:
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
The following is a simple example of the Boolean OR Operator:
Example
# Python program to show Boolean OR operator
# printing results
print("True or True =>", True or True)
print("True or False =>", True or False)
print("False or True =>", False or True)
print("False or False =>", False or False)
print("-4 > 2 or -7 < 0 =>", -4 > 2 or -7 < 0)
print("4 > 6 or 9 < 4 =>", 4 > 6 or 9 < 4)
Output:
True or True => True
True or False => True
False or True => True
False or False => False
-4 > 2 or -7 < 0 => True
4 > 6 or 9 < 4 => False
Explanation:
In the above example, we have performed various operations using the OR operator. Here, we can clearly see that the Boolean OR operator returns True if at least one of the conditions is true. It returns False only when both are false.
Boolean NOT Operator
Boolean NOT operator is a Boolean operator that requires only one argument (operand) and returns the negation of it. It means, this operator will return True for False and vice versa.
The following truth table represents the working of the NOT operator:
| A | not A |
|---|---|
| True | False |
| False | True |
Let us see a simple example of the NOT operator in Python
Example
# Python program to show Boolean NOT operator
# printing results
print("not 0 =>", not 0)
print("not (6 > 1) =>", not (6 > 1))
print("not True =>", not True)
print("not False =>", not False)
Output:
not 0 => True
not (6 > 1) => False
not True => False
not False => True
Explanation:
In this example, we can observe that the Boolean NOT operator has reversed the truth value of the given operands. As a result, it returns False for true expressions and True for the false ones.
Boolean Equality (==) and Inequality (!=) Operators in Python
The equality (==) and inequality (!=) operators in Python are used to compare the values and determine equality or inequality between them. These operators return a Boolean value, either True or False, on the basis of the results of the comparison.
The equality (==) operator checks if two values are the same. This operator returns True if the values are equal; otherwise False.
Whereas the inequality (!=) operator checks if two values are different. This operator returns True if the values are not equal and False in case they are equal.
Let us see an example showing how these operators work in Python.
Example
# Python program to show Boolean Equality and Inequality operators
# equality (==) operator
print("(8 == 8) =>", 8 == 8)
print("(True == 1) =>", True == 1) # since non-zero value is considered as True
print("(False == 0) =>", False == 0) # 0 is considered as False
print("(6 == -4) =>", 6 == -4)
print()
# inequality (!=) operator
print("(8 != 6) =>", 8 != 6)
print("(True != 1) =>", True != 1)
print("(False != 0) =>", False != 0)
print("(4 != 4) =>", 4 != 4)
Output:
8 == 8) => True
(True == 1) => True
(False == 0) => True
(6 == -4) => False
(8 != 6) => True
(True != 1) => False
(False != 0) => False
(4 != 4) => False
Explanation:
In this example, we can observe that the equality operator has returned True when two values are equal, while the inequality operator returned True when they are not equal.
Python ‘is’ Operator
The ‘is’ operator in Python is used to check if two variables point to the same object in memory. This operator does not check whether they have the same value.
Here is an example showing the usage of the ‘is’ operator in Python:
Example
# Python program to show 'is' operator
# initializing variables
a = [13, 26, 39]
b = a
c = [13, 26, 39]
# checking identities of the variables
print("a is b =>", a is b)
print("a is c =>", a is c)
Output:
a is b => True
a is c => False
Explanation:
In the case of a is b, the ‘is’ operator returns True as both variables point to the same object. Whereas, in case of a is c, it returns False as both variables points to the different objects with the same value.
Python ‘in’ Operator
The ‘in’ operator in Python is used to check whether a value exists within a container, such as a list, tuple, string, dictionary, or set.
Let us see an example of the ‘in’ operator in Python.
Example
# Python program to show 'is' operator
# initializing variables
int_list = [12, 17, 23, 34, 41] # list
fruits = {'apple', 'plum', 'mango'} # set
sample_str = 'tpointtech' # string
# checking elements in given list
print("17 in int_list =>", 17 in int_list)
print("19 in int_list =>", 19 in int_list)
print()
# checking items in given set
print("apple in fruits =>", 'apple' in fruits)
print("banana in fruits =>", 'banana' in fruits)
print()
# checking letter in given string
print("t in sample_str =>", 't' in sample_str)
print("q in sample_str =>", 'q' in sample_str)
Output:
17 in int_list => True
19 in int_list => False
apple in fruits => True
banana in fruits => False
t in sample_str => True
q in sample_str => False
Explanation:
In the above example, we can observe that the ‘in’ operator is used to check if a given element exists in a given list, set, or string. As a result, it returns True if found and False otherwise.
Conclusion
Python Booleans represent truth values using True and False and are important for decision-making in code. With Boolean operators such as and, or, and not, we can build logical expressions. Python also uses other operators like in, is, ==, and != for comparisons, making Boolean logic key to writing clear, effective programs.
Leave a Reply