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:
- and
- or
- 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!