Introduction

Logical operators are an essential part of any programming language and play a fundamental role in decision making for the program. In Python, there are three main logical operators:  and or , and  not . These operators allow us to combine multiple conditions and create complex logical expressions. Understanding how to use these logical operators in Python is crucial for any software developer who wants to write efficient and effective code. Throughout this tutorial, we will explore the concepts of logical operators in Python and demonstrate how to use them in practical coding examples.

Table of Contents :

  • and operator in python
  • or operator in python
  • not operator in python
  • Precedence of Logical Operators
  •  Using any() and all() built-in functions in Python
  • Logical operators are also called Boolean operators.
  • Python uses English words to denote logical operators
  • Logical operators are used to evaluate multiple logical conditions simultaneously
  • Logical operators combine multiple boolean expressions and values to form one elaborate expression.
  • Python has three logical operators:
    • and
    • or
    • not

The and operator in python :

  •  and  operator takes two operands.
  • The operands of  and  operator can be a boolean expression, object or a combination of both.
  • It is used to evaluate if the two operands are both True simultaneously or not.
  • The  and  operator is the operator for conjunction.
  • Return values of  and  operator
    • it returns True if both conditions are True. 
    • it returns False if one of the conditions is False.
    • If one or more operands of  and  operator is an arithmetic value, it returns the second value as a result
  • Code Sample : 

# If both conditions are True
# it returns True
x = True
y = True
res = x and y
print(res)

# Output
# True



# If one of the conditions is False
# it returns False
x = True
y = False
res = x and y
print(res)

# Output
# False



# If one or more operands is an arithmetic value, 
# it returns the second value
x = True
y = 100
res = x and y
print(res)

# Output
# 100


# If one or more operands is an arithmetic value, 
# it returns the second value
x = 100
y = True
res = x and y
print(res)

# Output
# True

  • Below is the truth table for the  and  operator
   operand-1     operand-2        operand-1 and operand-2
       True           True                               True 
       True           False                               False 
       False           False                               False 
       False           True                               False 


 

The or operator in python :

  •  or  operator takes two operands.
  • The operands of  or  operator can be a boolean expression, object or a combination of both.
  • Similar to the  and  operator, the  or  operator checks multiple conditions. 
  • The  or  operator is the operator for disjunction
  • Return values of  or  operator :
    • it returns True when at-least one conditions is True
    • it returns False if both the conditions are False.
    • If one or more operands of  or  operator is an arithmetic value, it returns the first value as a result
  • Code Sample : 

# When at-least one conditions is True
# it returns True
x = True
y = True
res = x or y
print(res)

# Output
# True



# When at-least one conditions is True
# it returns True
x = True
y = False
res = x or y
print(res)

# Output
# True



# When both the conditions are False
# it returns False
x = False
y = False
res = x or y
print(res)

# Output
# False



# If one or more operands is an arithmetic value, 
# it returns the first value
x = True
y = 100
res = x or y
print(res)

# Output
# True


# If one or more operands is an arithmetic value, 
# it returns the first value
x = 100
y = True
res = x or y
print(res)

# Output
# 100


  • Below is the truth table for the  or  operator:
   operand-1     operand-2        operand-1 or operand-2
       True           True                               True 
       True           False                               True
       False           False                               False 
       False           True                               True

The not operator in python :

  • The  not  operator takes one operand. 
  • The operands of  not  operator can be a boolean expression, object or a combination of both.
  • The  not  operator is the operator for negation
  • Return values of  not  operator :
    • If the operand is True it returns False 
    • If the operand is False it returns True
    • If the operand of  not  operator is an arithmetic values it returns False for nonzero value
    • If the operand of  not  operator is zero it returns True
  • Code Sample : 

# If the operand is True it returns False 
x = True
res = not x
print(res)

# Output
# False



# If the operand is False it returns True
x = False
res = not x
print(res)

# Output
# True



# If the operand is a nonzero arithmetic value
# it returns False
x = 100
res = not x
print(res)

# Output
# False


# If the operand of not operator is zero it returns True
x = 0
res = not x
print(res)

# Output
# True


  • Below is the truth table for the  not  operator :
   operand                      not operand
       True                               False 
       False                               True

Precedence of Logical Operators

  • When we use more than one operators in a single expression, Python uses operator precedence to evaluate the expression.
  • Same is the case with logical operators.
  • The precedence of logical operators is : not > and > or
  • To evaluate an expression Python first groups the operands of the operator with the highest precedence, then it groups the operands of the operator with the lower precedence and so on.
  • If an expression has more than one logical operators with the same precedence Python evaluates them from the left to right
  • Evaluation of some logical expressions as per precedence :
    • a or b and c :  a or (b and c)
    • a and b or c and d : (a and b) or (c and d)
    • a and b and c or d : ((a and b) and c) or d
    • not a and b or c : ((not a) and b) or c

 Using any() and all() built-in functions in Python

Note # This is an intermediate level topic and should be studied only after studying the topic Functions in Python

  • Python has two built-in functions called 
    • any()
    • all().

any() built in function in python

  • any() takes one or more arguments
  • It returns True if any of the arguments are True. 
  • If all of the arguments are False, it returns False. 
  • Here are some examples:

any([True, False, False]) returns True 
any([False, False, False]) returns False 
any(["a", "", "b"]) returns True 
any(["", "", ""]) returns False

all() built in function in python

  • all() takes one or more arguments. 
  • It returns True if all of the arguments are True. 
  • If any of the arguments are False, it returns False. 
  • Here are some examples:

all([True, True, True]) returns True 
all([True, False, True]) returns False 
all(["a", "b", "c"]) returns True 
all(["a", "", "b"]) returns False

Prev. Tutorial : Relational operators  

Next Tutorial : Membership operators