Prahlad Inala
Prahlad Inala

Follow

Prahlad Inala

Follow
Logical Operators in Python

Logical Operators in Python

Prahlad Inala's photo
Prahlad Inala
ยทJul 12, 2021ยท

2 min read

Play this article

What are Logical Operators in Python?

Logical operators are used to combining 2 booleans into another boolean.

Following are the logical operators in python:

  1. and
  2. or
  3. not

And Operator

And operator evaluates to true if both left and right evaluate to true., else it evaluates to false.

x = 8
print(x > 5 and x < 10)
#True
#returns True because 8 is greater than 5 (True) AND 8 is less than 10 (True)

x = 5
print(x > 3 and x > 10)
#False
#returns False because 5 is greater than 3 (True) But 5 is not greater than 10 (False)

x = 5
print(x < 3 and x > 10)
#False
#returns False because 5 is not less than 3 (False) AND 5 is not greater than 10 (False)

OR Operator

Or operator evaluates to True if either left or right side evaluates to True., else it evaluates to false.

x = 5
print(x > 3 or x > 10)
#True
#returns True because 5 is greater than 3 (True), even 5 is not greater than 10(False)

x = 5
print(x < 3 or x < 10)
#True
#returns True because 5 is less than 3 (False), even 5 is less than 10 (True)

x = 5
print(x < 3 or x > 10)
#False 
#returns False because 5 is less than 3 (False), even 5 is not greater than 10 (False)

Not Operator

Not operator evaluates to the opposite boolean given a boolean.

x = 5
print(not(x > 3 and x < 10))
#False
# returns False because not is used to reverse the result

x = 5
print(not(x < 3 and x < 10))
#True
# returns True because not is used to reverse the result

Did you find this article valuable?

Support Prahlad Inala by becoming a sponsor. Any amount is appreciated!

See recent sponsors |ย Learn more about Hashnode Sponsors
ย 
Share this