Boolean operators such as ‘and‘, ‘or‘, and ‘not‘ are referred to as boolean operators in Python. And, True and False are referred to as Boolean values. These boolean operators return either True or False after evaluating the expression.
The and operator returns True if both operands are true, and False otherwise. And, the or operator returns True if at least one operand is true, and False otherwise.
Usually, we will use the boolean operators with comparison operators such as ==, !=, <, >, <=, >= when we wanted to evaluate multiple conditions.
The boolean operators AND, OR, and NOT allows program flow to be modified based on the results of logical operations in a variety of contexts, including conditional statements, loops, and functions.
In this article, with the help of clear-cut examples, I will explain what are Python Boolean operators, and when to use them to evaluate multiple conditions and their syntax.
Table of contents
1. What are Python Boolean Operators?2. Overview of Python Boolean Operators2.1 AND operator 2.2 OR operator 2.3 NOT operator3. Examples of Python Boolean Operators in action3.1 AND operator example 3.2 OR operator example 3.3 NOT operator example4. Conclusion
1. What are Python Boolean Operators?
Python Boolean operators are the type of operators that are used to perform logical operations on Boolean values which are either True or False. The AND, OR, and NOT operators are the three primary Boolean operators used in Python.
2. Overview of Python Boolean Operators
In this section, you will get to know each Boolean operator in detail. We will start with the AND, then the OR operator, and will finish with the NOT operator.
2.1 AND operator
The first operator from the Python Boolean operators is the AND operator. This operator is used when evaluating multiple conditions and the result is True only if all the conditions evaluated are True. Here is the AND operator in a truth table:
ABA and BTrueTrueTrueTrueFalseFalseFalseTrueFalseFalseFalseFalseAND Operator TRUTH Table
The AND operator syntax is as below:
# Syntax or Usage
expression1 and expression2 and …… and expressionN
2.2 OR operator
The second operator from the Python Boolean operators is the OR operator. Just like the AND operator, the OR operator is used to evaluate multiple conditions as well and the result is True if at least one of the conditions is True. The truth table for the OR operator is as follows:
ABA or BTrueTrueTrueTrueFalseTrueFalseTrueTrueFalseFalseFalseOR Operator TRUTH Table
The OR operator syntax looks as follows:
# Syntax or Usage
expression1 or expression2 or …… or expressionN
2.3 NOT operator
The NOT operator is the last operator from the Python Boolean operators. This operator works differently as compared to the AND operator and OR operator. It is used to evaluate a single condition whilst AND and OR operators are for evaluating two or more conditions. The NOT operator inverts the Boolean value of an expression. For example, if the expression is True, the NOT operator will make it False and vice versa. Below is the NOT operator in a truth table:
Anot ATrueFalseFalseTrueNOT Operator TRUTH Table
The NOT operator has the following syntax:
# Syntax or Usage
not expression
3. Examples of Python Boolean Operators in action
To solidify your understanding of these Boolean operators, this section will focus on the code example for each operator.
3.1 AND operator example
Example 1: Consider the marks of four students (Integers) and use the ‘and’ operator to evaluate some expressions as boolean values in Python.
# Consider the marks of four students
ravi=78
shyam=90
giri=90
mounika=56
# Use the ‘and’ operator to evaluate below expressions
print(shyam == giri and mounika < ravi)
print(shyam == giri and mounika > ravi
In the first statement we are checking if marks of ‘shyam’ == ‘giri’ as the first expression and marks of ‘mounika’ is less than ‘ravi’. Both are combined with the and operator. Both expressions are True. So this statement is True. Finally, the result is True.
In the second statement, we are checking if marks of ‘shyam’ == ‘giri’ as the first expression and marks of ‘mounika’ is greater than ‘ravi’. Both are combined with the and operator. The second expression is False (56 > 78). So this statement is False. Finally, the result is False.
The output for the above example will be:
# Output:
# True
# False
Example 2: Let’s look at the usage of the AND operator on if statement.
# Creating variables a, b, and c
a = 7
b = 5
c = 10
# Checking if both expressions/conditions are True
if (a > b) and (c > a):
print(f”Both conditions are True. {a} > {b} and {c} > {a}”)
else:
# The else will run when the above check fails
print(f”At least one condition is False.”)
Here is what we are doing in the code snippet, we are creating three variables a, b, and c. Then using an if statement, we are checking if a is greater than b and c is greater than a. If both these conditions are True, the code inside the if statement will run. If one or none of these conditions is True, then the code inside the else statement will run.
The output for the above example will be:
# Output:
Both conditions are True. 7 > 5 and 10 > 7
3.2 OR operator example
Example 3: Let’s evaluate some expressions combined with the ‘or’ operator.
# Use the ‘or’ operator to evaluate below expressions
print(shyam == giri or mounika > ravi)
print(shyam != giri or mounika > ravi)
In the first statement we are checking if marks of ‘shyam’ == ‘giri’ as the first expression and marks of ‘mounika’ is greater than ‘ravi’. Both are combined with the or operator. The first expression is True and the second expression is False. At least one statement is True. The final result is True.
In the first statement, we are checking if marks of ‘shyam’ != ‘giri’ as the first expression and marks of ‘mounika’ is greater than ‘ravi’. Both are combined with the or operator. Both expressions are False. So the final result is False.
The output for the above example will be:
# Output:
# True
# False
Example 4: Let’s look at the usage of the OR operator on if statement.
# Creating variables a, b, and c
a = 8
b = 5
c = 10
# Checking if one of the expressions/conditions is True
if a > b or c < a:
print(f”At least one condition is True. {a} > {b}”)
else:
# The else will run if all the expressions are False
print(f”Both conditions are False.”)
To be on the same page, I will break down the code for you. We have created three variables a, b, and c. Using the if statement and the OR operator, we are checking if a is greater than b and c is less than a. If one of these conditions is True, the code inside the if statement will run and if all the conditions are False, the code inside the else statement will run.
If you run the above code the output will be:
# Output:
At least one condition is True. 8 > 5
3.3 NOT operator example
As I pointed out earlier, the NOT operator is different from the other Boolean operators as it is used to inverse a Boolean value of an expression. An example code looks like this:
# Creating variables a and b
a = 10
b = 5
# The not negates (a > b)
if not (a > b):
print(f”The condition is True.”)
else:
# The else runs when the above check fails
print(f”The condition is False.”)
Here, we have created two variables a and b. Our expression (a > b) is True but the NOT operator inverts the value to False thus causing the code inside the else statement to run.
You will get the following output after running the code:
# Output:
The condition is False.
4. Conclusion
These Python Boolean operators allow program flow to be modified based on the results of logical operations in a variety of contexts, including conditional statements, loops, and functions. We can refer to Comparison and Logical operators as Boolean operators. A comparison operator is used to compare two values. It returns True or False based on the operator and variables. Logical operators combine the other operators like comparison operators etc. It will also return the boolean value based on the expression evaluated.
Boolean operators such as ‘and‘, ‘or‘, and ‘not‘ are referred to as boolean operators in Python. And, True and False are referred to as Boolean values. These boolean operators return either True or False after evaluating the expression. The and operator returns True if both operands are true, and False otherwise. And, the or operator Read More Python, Python Tutorial, Python Operators