Get a skill gap analysis, personalised roadmap, and AI-powered resume optimisation.
Introduction
Python is a very useful and easy programming language used for various applications. One of the fundamental concepts in Python is the Boolean data type. Booleans represent the truth values, which can be either True or False. They are used for logical operations, conditionals, & comparisons.
In this article, we will learn about Python Booleans, their usage, & how they can be manipulated using different operators & functions. We will also discuss the evaluation of variables & expressions as Booleans & the usage of Boolean operators such as AND, OR, & NOT.
Python Boolean Type
In Python, the Boolean data type is a built-in type that represents the truth values. It has two possible values: True & False. These values are case-sensitive, so you need to capitalize the first letter.
Example
pythonCopyis_valid = True
is_completed = False
In the above code, we assign the value True to the variable is_valid & False to the variable is_completed. We can use these variables in logical operations, conditionals, or comparisons.
Evaluate Variables & Expressions:
Python allows you to evaluate variables & expressions as Booleans. When you use a variable or an expression in a Boolean context, Python automatically converts it to a Boolean value based on certain rules.
Any non-zero numeric value is considered True, while zero is considered False.
An empty string, list, tuple, set, or dictionary is considered False, while a non-empty one is considered True.
The special value None is considered False.
For example :
Python
Python
num = 5 if num: print("num is True") else: print("num is False")
my_list = [] if my_list: print("my_list is True") else: print("my_list is False")
You can also try this code with Online Python Compiler
In the above code, the variable num is evaluated as True because it is a non-zero value. The empty list my_list is evaluated as False.
Python bool() Function
Python provides a built-in function called bool() that allows you to explicitly convert a value to a Boolean. The bool() function takes a single argument & returns either True or False based on the truthiness of the argument.
Here are the main false values in Python that are considered False when evaluated:
None
False
Zero of any numeric type, for example, 0, 0.0, 0j
Empty sequences and collections, such as [], (), {}, '', set(), range(0)
In the above code, we pass different values to the bool() function & it returns the corresponding Boolean value. Non-zero numbers, non-empty strings, & non-empty lists are considered True, while zero, empty strings, & empty lists are considered False.
Note: The bool() function is particularly useful when you need to explicitly convert a value to a Boolean for logical operations or comparisons.
Boolean Value from the Expression
In Python, you can obtain a Boolean value from an expression using comparison or logical operators. Comparison operators such as == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), & <= (less than or equal to) return a Boolean value based on the comparison result.
For example :
Comparison Operators
These include == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). For example:
Python
Python
x = 5
y = 10
print(x > y)
print(x < y)
You can also try this code with Online Python Compiler
In Python, integers & floats can be used as Booleans in certain contexts. When an integer or float is used in a Boolean context, Python follows certain rules to determine its truthiness.
For integers
Any non-zero integer is considered True.
Zero (0) is considered False.
For floats
Any non-zero float is considered True.
Zero (0.0) is considered False.
Example :
Python
Python
if 1: print("1 is True")
if 0: print("0 is True") else: print("0 is False")
if 3.14: print("3.14 is True")
if 0.0: print("0.0 is True") else: print("0.0 is False")
You can also try this code with Online Python Compiler
In the above code, the non-zero integer 1 & the non-zero float 3.14 are considered True, so the corresponding print statements are executed. The integer 0 & the float 0.0 are considered False, so the else blocks are executed.
Note: It's important to note that using integers & floats as Booleans can sometimes lead to unexpected behavior, especially if the values are the result of calculations or comparisons. It's generally recommended to use explicit Boolean comparisons for clarity & readability.
Boolean Operators
Python provides three main Boolean operators: and, or, & not. These operators allow you to combine or negate Boolean values based on certain conditions.
and operator
The and operator returns True if both operands are True, & False otherwise.
It evaluates the operands from left to right & stops as soon as it finds a False operand.
or operator
The or operator returns True if at least one of the operands is True, & False otherwise.
It evaluates the operands from left to right & stops as soon as it finds a True operand.
not operator
The not operator is a unary operator that negates the Boolean value of its operand.
It returns True if the operand is False, & False if the operand is True.
Example
Python
Python
x = 5
y = 10
print(x > 0 and y > 0)
print(x > 0 and y < 0)
print(x > 0 or y < 0)
print(not (x > 0))
You can also try this code with Online Python Compiler
The expression x > 0 and y > 0 evaluates to True because both conditions are True.
The expression x > 0 and y < 0 evaluates to False because the second condition is False.
The expression x > 0 or y < 0 evaluates to True because at least one of the conditions is True.
The expression not (x > 0) evaluates to False because the condition x > 0 is True, & the not operator negates it.
Note: Boolean operators allow you to create more complex conditions & control the flow of your program based on multiple conditions.
Boolean OR Operator
The Boolean OR operator in Python is represented by the keyword or. It returns True if at least one of the operands is True, & False otherwise. The OR operator evaluates the operands from left to right & stops as soon as it finds a True operand.
Here's the truth table for the OR operator:
Operand 1
Operand 2
Result
True
True
True
True
False
True
False
True
True
False
False
False
As you can see from the truth table, the OR operator returns True if either operand is True. It returns False only when both operands are False.
Example :
Python
Python
x = 5
y = 10
print(x < 0 or y > 0)
print(x < 0 or y < 0)
print(x > 0 or y < 0)
print(x > 0 or y > 20)
You can also try this code with Online Python Compiler
The expression x < 0 or y > 0 evaluates to True because the second condition is True.
The expression x < 0 or y < 0 evaluates to False because both conditions are False.
The expression x > 0 or y < 0 evaluates to True because the first condition is True.
The expression x > 0 or y > 20 evaluates to True because the first condition is True, even though the second condition is False.
Note: The OR operator is useful when you want to check if at least one of the conditions is satisfied. It allows you to combine multiple conditions & perform logical operations based on their truthiness.
Python Boolean OR Operator
The Python Boolean OR operator is represented by the symbol or. It is used to combine two or more Boolean expressions & returns True if at least one of the expressions is True. If all the expressions are False, it returns False.
Here's the general syntax of the OR operator:
pythonCopyexpression1 or expression2 or ... or expressionN
The OR operator evaluates the expressions from left to right. As soon as it finds a True expression, it stops evaluating the remaining expressions & returns True. If all the expressions are False, it returns False.
Here are a few examples:
Python
Python
is_sunny = True
is_warm = False
print(is_sunny or is_warm)
is_weekend = False
is_holiday = False
print(is_weekend or is_holiday)
x = 5
y = 10
print(x > 10 or y > 5)
print(x < 0 or y < 0)
You can also try this code with Online Python Compiler
In the first example, is_sunny or is_warm evaluates to True because is_sunny is True.
In the second example, is_weekend or is_holiday evaluates to False because both is_weekend & is_holiday are False.
In the third example, x > 10 or y > 5 evaluates to True because y > 5 is True, even though x > 10 is False.
In the fourth example, x < 0 or y < 0 evaluates to False because both x < 0 & y < 0 are False.
Note: The OR operator is useful when you want to check if any of the given conditions is True. It allows you to combine multiple conditions & make decisions based on their truthiness.
Boolean And Operator
The Boolean AND operator in Python is represented by the keyword and. It returns True if both operands are True, and False otherwise. The AND operator evaluates the operands from left to right and stops as soon as it finds a False operand.
Here's the truth table for the AND operator:
Operand 1
Operand 2
Result
True
True
True
True
False
False
False
True
False
False
False
False
As you can see from the truth table, the AND operator returns True only when both operands are True. It returns False in all other cases.
Example :
Python
Python
x = 5
y = 10
print(x > 0 and y > 0)
print(x > 0 and y < 0)
print(x < 0 and y > 0)
print(x < 0 and y < 0)
You can also try this code with Online Python Compiler
The expression x > 0 and y > 0 evaluates to True because both conditions are True.
The expression x > 0 and y < 0 evaluates to False because the second condition is False.
The expression x < 0 and y > 0 evaluates to False because the first condition is False.
The expression x < 0 and y < 0 evaluates to False because both conditions are False.
Note: The AND operator is useful when you want to check if all the given conditions are True. It allows you to combine multiple conditions and perform logical operations based on their truthiness.
Boolean Not Operator
The Boolean NOT operator in Python is represented by the keyword not. It is a unary operator that takes a single operand and returns the opposite Boolean value. If the operand is True, the NOT operator returns False, and if the operand is False, it returns True.
Here's the truth table for the NOT operator:
Operand
Result
True
False
False
True
The NOT operator is used to negate or invert the Boolean value of an expression.
Example :
Python
Python
is_valid = True
is_expired = False
print(not is_valid)
print(not is_expired)
x = 5
y = 10
print(not (x > 0))
print(not (y < 0))
You can also try this code with Online Python Compiler
not is_valid evaluates to False because is_valid is True, and the NOT operator inverts it.
not is_expired evaluates to True because is_expired is False, and the NOT operator inverts it.
not (x > 0) evaluates to False because x > 0 is True, and the NOT operator inverts it.
not (y < 0) evaluates to True because y < 0 is False, and the NOT operator inverts it.
Note: The NOT operator is useful when you want to invert the Boolean value of an expression. It allows you to check for the opposite condition or negate a Boolean result.
Python Boolean Not Operator
The Python Boolean NOT operator is represented by the keyword not. It is a unary operator that takes a single operand and returns the opposite Boolean value. If the operand is True, the NOT operator returns False, and if the operand is False, it returns True.
Here's the general syntax of the NOT operator:
not expression
The NOT operator negates the Boolean value of the expression. It reverses the truthiness of the operand.
Here are a few examples:
Python
Python
is_valid = True
is_expired = False
print(not is_valid)
print(not is_expired)
x = 5
y = 0
print(not (x > 0))
print(not (y > 0))
result = (x > 0) and (y > 0)
print(not result)
You can also try this code with Online Python Compiler
In the first example, not is_valid evaluates to False because is_valid is True, and the NOT operator inverts it.
In the second example, not is_expired evaluates to True because is_expired is False, and the NOT operator inverts it.
In the third example, not (x > 0) evaluates to False because x > 0 is True, and the NOT operator inverts it.
In the fourth example, not (y > 0) evaluates to True because y > 0 is False, and the NOT operator inverts it.
In the fifth example, result is assigned the value of (x > 0) and (y > 0), which evaluates to False. Then, not result evaluates to True because it inverts the value of result.
Note: The NOT operator is useful when you want to invert the Boolean value of an expression or check for the opposite condition. It allows you to negate Boolean values and perform logical operations based on the inverted values.
In Python, the == (equivalent) & != (not equivalent) operators are used to compare two values for equality or inequality, respectively. These operators return a Boolean value indicating whether the comparison is True or False.
The == operator checks if two values are equal. It returns True if the values are equal & False otherwise.
For example:
Python
Python
x = 5
y = 10
print(x == y)
print(x == 5)
print("Hello" == "Hello")
print("Hello" == "hello")
You can also try this code with Online Python Compiler
x == y evaluates to False because x & y have different values.
x == 5 evaluates to True because x is equal to 5.
"Hello" == "Hello" evaluates to True because both strings are identical.
"Hello" == "hello" evaluates to False because the strings are not equal (case-sensitive).
The != operator, on the other hand, checks if two values are not equal. It returns True if the values are not equal & False otherwise. Here's an example:
Python
Python
x = 5
y = 10
print(x != y)
print(x != 5)
print("Hello" != "Hello")
print("Hello" != "hello")
You can also try this code with Online Python Compiler
x != y evaluates to True because x & y have different values.
x != 5 evaluates to False because x is equal to 5.
"Hello" != "Hello" evaluates to False because both strings are identical.
"Hello" != "hello" evaluates to True because the strings are not equal (case-sensitive).
Note: The == & != operators can be used with various data types, including numbers, strings, & even objects. They provide a way to compare values & make decisions based on the comparison results.
Python is Operator
The is operator in Python is used to check if two variables refer to the same object in memory. It returns True if the variables point to the same object and False otherwise. The is operator compares the identity of the objects rather than their values.
For example
Python
Python
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is y)
print(x is z)
print(x == y)
You can also try this code with Online Python Compiler
x is y evaluates to False because x and y are two different list objects, even though they have the same values.
x is z evaluates to True because z is assigned the same object as x, so they refer to the same list object in memory.
x == y evaluates to True because the values of x and y are equal, even though they are different objects.
It's important to note that the is operator checks for object identity, not object equality. In most cases, you would use the == operator to compare the values of objects and the is operator to check if two variables refer to the same object.
The is operator is commonly used to compare a variable with the None keyword, which represents a special null value in Python. For example:
Python
Python
x = None
if x is None:
print("x is None")
else:
print("x is not None")
You can also try this code with Online Python Compiler
In the above code, the is operator is used to check if x is None. It evaluates to True because x is assigned the value None.
Note: The is operator provides a way to check the identity of objects and is particularly useful when comparing variables with singleton objects like None.
Python in Operator
The in operator in Python is used to check if a value exists in a sequence (such as a string, list, tuple, or dictionary). It returns True if the value is found in the sequence and False otherwise.
Here are a few examples of using the in operator:
Checking if a value exists in a list
Python
Python
fruits = ["apple", "banana", "orange"]
print("banana" in fruits)
print("grape" in fruits)
You can also try this code with Online Python Compiler
The in operator is useful when you want to check if a value is present in a collection without the need for a manual iteration. It provides a concise and efficient way to perform membership tests.
You can also use the not in operator to check if a value does not exist in a sequence. It returns True if the value is not found in the sequence and False otherwise.
For example:
Python
Python
pythonCopyfruits = ["apple", "banana", "orange"]
print("grape" not in fruits)
print("banana" not in fruits)
You can also try this code with Online Python Compiler
Note: The in and not in operators are versatile and can be used with various types of sequences in Python, making it convenient to check for the presence or absence of values in a collection.
Frequently Asked Questions
Can a string be converted to a Boolean in Python?
Yes, any string can be converted to a Boolean. In Python, an empty string ('') will convert to False, while any non-empty string will convert to True.
How do Boolean values interact with arithmetic operations?
In Python, True and False can also act as numbers, equating to 1 and 0, respectively. This means you can perform arithmetic operations where True adds 1 and False adds 0.
Is it better to use == or is for comparing strings for equality?
For string comparison, it's recommended to use == rather than is. The == operator checks for value equality, while is checks whether both variables point to the same object, which isn't typically the desired check in string comparisons.
Conclusion
In this article, we discussed the fundamentals of Python Booleans, including the Boolean data type, evaluating variables & expressions as Booleans, and using Boolean operators like AND, OR, & NOT. Understanding Python Booleans is very important for writing conditional statements & controlling program flow.