Boolean or Logical Operators In Python with Examples

Logical operators in Python also called as Boolean Operators are applied to multiple expressions to check whether they result in True or False value. Here is the list of logical operators in Python.
Operator Name Description
and AND Operator The output is true when both the expressions are true (expression on the left-hand side & right-hand side)
or OR Operator The output is true if either one of the expressions is true.
not NOT Operator Reverses the output.

Where can Logical Operators be Used?

a) In real-time, say, for example, if you had to attend an exam, then the criteria used by the examiners to let you inside the exam hall is You should have paid the exam fee and You should have met the 75% attendance criteria. b) Similarly, assume you have an online cosmetics shop and would want to send out coupons to your active customers, then one criterion you can use to find out your active customers is Should have made a purchase in the past 3 months or Should have some wallet credits. That’s how logical operators can be of great use. Let us now get into understanding how to use these operators to compare various expressions or values.

How To Use Logical Operators in Python (Examples)

1) and (AND Operator)

This operator is similar to a multiplication arithmetic operator. In multiplication operation returns 0, if both the inputs or either one of the inputs is zero. Similarly, AND operator returns True if both the expressions are True. If either one of the expressions is False, then it returns False.
#using and logical operator
a, b = 10, 20
print ( (a < b) and (a != b))
Output: True

Explanation: Here, a < b is one expression and a != b is another expression. Both the expressions are True and hence the output is also True.

#using and logical operator
a, b = 10, 20
print ((a == b) and (a < b))
Output: False
Explanation: Here, the first expression is False and hence it returns False.

2) or (OR Operator)

OR operator is similar to the addition arithmetic operator. The output is True if either one of the expressions is True.
#using or logical operator
a, b = 10, 20
print ((a > b) or (a != b))
Output: True

Explanation: Here, the expression a != b is True and hence the output is also True.

#using or logical operator
a, b = 10, 20
print ((a - b == a + b) or a != b)
Output: True
Explanation: Here, the left-hand side expression is False, but the right-hand side expression is True hence the output is True.

3) not (NOT Operator)

This operator reverses the obtained result and hence it returns False if the result is True and vice versa. For instance,
a, b = 10, 20
print (not(a > b or a != b))
Output: False

Explanation: Here, the actual output from both the expressions is True. Since we have used ‘not’ operator, the output is False, i.e ‘not’ operator reversed the output from True to False.

Precedence & Associativity of Logical Operators in Python

All the logical operators have the same precedence and hence it is important to consider the associativity of these operators. The associativity is left to right. This simply means the logical operator that appears first while reading an expression gets evaluated first

Consider, the below program,

#evaluating logical operators
a, b = 10, 20
print (not(a > b and a != b) or a != b)
Output: True

This is how the evaluation goes,

  • Here, the NOT operator appears first. Hence we need to compute not(a>b and a!=b) first. The result of which is True.
  • Next comes the evaluation of the rest of the expression True or a!=b. The output of this is True.
  • Hence True is returned by the print statement.

This is how precedence & associativity help in evaluating complex expressions with multiple logical operators in them.

Logical Operators in Python FAQs

These are operators used to check if conditional statements are True or False. Logical operators in Python are AND, OR and NOT.

The associativity of logical operators in the same and hence we need to consider the associativity of operators to prioritize them.

Yes. Logical operators can be used on strings. Here are some rules to know before operating with logical operators on strings.

  • Python considers an empty string as False & a non-empty string as True.
  • When AND operator is used between two strings, it first checks if the left-hand side expression is True. If so, it then checks the right-hand side expression and returns it no matter what. But, if the left hand side expression is False, then it returns that string or value.
  • When OR operator is used between two strings, it checks if the left-hand side expression is True. If True, then it returns that string or value. If False, it returns the right-hand-side value. 

For example,

#in case of one empty string
str1 = '' 
str2 = 'FACE Prep'

print ((str1 and str2))    # Returns str1 i.e blank line 
print (str1 or str2)    # Returns str2

#in case of all non-empty strings
str1 = 'Python ' 
str2 = 'FACE Prep'

print (str1 and str2)    # Returns str1 
print (str1 or str2)    # Returns str2
Output:
                  #output1
FACE Prep         #output2
FACE Prep         #output3
Python            #output4