Logical Operators in Python

Operators are fundamental in programming. There are times when we tell computers to perform certain tasks when some conditions are met. Logical operators are key elements in that flow.

Logical operators are used to combining conditional statements. For eg., if we have to process some job applications and the primary criterion is age should be greater than 18 and it shouldn’t exceed 25 years. This is how we tell the computer to put the filter:

Our thinking:
If the age is between 18 and 25 then candidate is eligible for the interview

Computer understands:

if age > 18 and age < 25 then "Call for Interview round!"

In such logic where we have to use multiple conditions to perform some tasks, we use logical operators. There are three logical operators:

  • AND
  • OR
  • NOT

let’s try to understand how these are used to create complex programs.

The logical operators and, or and not are also referred to as binary operators. While and as well as or operator needs two operands, which may evaluate to true or false, not operator needs one operand evaluating to true or false.

AND Logical Operator

Let’s think about AND in our way! Suppose, you are in a new city and you are looking for a place to eat. You like Italian so you start checking the list of all restaurants serving Italian. And Boom! there are 500!

The next step is you check the review and filter with say more 5-star ratings and the list has now 3.

The next step is you see which one of these three is the nearest and you go to that and have a good dining experience.

Simple, right? Now if we instruct the computer to do the same task for us, this is how it tentatively looks like:

If cuisine == italian and review == 5 and distance is lowest, then "GO!"

If we want some task to be executed only when ALL conditions are true, AND operator is used. In the above example, you are not okay with nearest or you are also not okay with review == 5, instead, you want all the conditions to be true.

Let’s understand the same thing with another traditional and best example which is widely used:

AND Operator

In the above picture, both switch A and switch B has to be on for the bulb to light up. if any button is off, the bulb wouldn’t light.

In the same way, when we use AND operator, we want both conditions to be met, to result in TRUE (or in this case, light up bulb).

x =2

x < 5 and  x < 10

"""
output:

True

"""

 

x =2

x > 5 and  x < 10

#Here only one condition is True hence result is false

"""
output:

False

"""

 

a =  10
b =  10
c = -10
  
if a > 0 and b > 0:
    print("The numbers are greater than 0")
  
if a > 0 and b > 0 and c > 0:
    print("The numbers are greater than 0")
else:
    print("Atleast one number is not greater than 0")


"""
output:

The numbers are greater than 0
Atleast one number is not greater than 0

"""

 

True and False

"""
output:

False

"""

 

OR Logical Operator

Let’s get ahead in what happens when you reach that well-filtered restaurant. You call the waiter and ask him for available options and you have Caprese Salad and finally, you ask for the bill.

The waiter asks “how would you like to pay? Cash or Card!”

Now it should be clear where OR is used. You would obviously not pay with Cash and Card both, right?

OR is used when we want some task to be executed only when any (not all) condition is true. OR operator, unlike AND, doesn’t need all conditions to be true to execute the task.

Let’s understand with this example:

OR Operator

In the above picture, we see that if any switch is on (whether it is A or B), the bulb lights up.

Same way, if we want some code to be executed when any condition is true (not necessarily both like AND operator), we use OR operator.

Let’s try with examples!

x = 7

x < 5 or x < 10

# See here, only one condition is met and it returns True.

"""
output:

True

"""

 

x = 11

x < 5 or x < 10

#Both conditions are failed.

"""
output:

False

"""

 

True or False

#returns true because one of the condition is true

"""
output:

True

"""

 

a = 10
b = -10
c = 0
  
if a > 0 or b > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")
  
if b > 0 or c > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")

"""
output:

Either of the number is greater than 0
No number is greater than 0

"""

 

NOT Logical Operator

NOT logical operator simply negates the results. So if the result is True; NOT True will be False. (It is used to negate the results.)

If the condition is False, the NOT False will be True!

Let’s see some examples!

not(True)

"""
output:

False

"""

 

not(False)

"""
output:

True

"""

 

x = 7

x < 5 or x < 10

# With OR operator the condition is True.


"""
output:

True

"""

 

x = 7

not(x < 5 or x < 10)

# With OR operator the condition is True but NOT negates the results, hence False!

"""
output:

False

"""

 

x = True
y = False

print('x and y is',x and y)

print('x or y is',x or y)

print('not x is',not x)

"""
output:

x and y is False
x or y is True
not x is False

"""

 

For computers, in programming languages, 1 is denoted as True and 0 is denoted as False.

Let’s see what this means.

if (1): 
  print ("Print True for 1")
else:
  print ("Print False for 0")


"""
output:

Prints True for 1

"""

 

if (0): 
  print ("Print True for 1")
else:
  print ("Print False for 0")


"""
output:

Print False for 0

"""

 

Precedence in Logical Operators

If we have all these Logical operators in one statement, there is precedence followed in execution. not operator has the highest precedence followed by and and or.

The order of execution will be:

  1. not
  2. and
  3. or