Python
In this tutorial we will learn about Logical operators in Python.
Following are the list of Logical operators in Python
Click here if you want to read more about Boolean Algebra.
and
The logical and
operator will give True value if both the operands are True. Otherwise, it will give False.
Truth table of logical and
operator.
A | B | A and B |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
In the following Python program we will get True
only if both expressions evaluates to True.
# variables
a = 10
b = 20
x = 40
y = 50
m = a < b # this will give True
n = y > x # this will give True
print("Result: ", (m and n))
Output of the above code.
Result: True
or
The logical or
operator will give True value if any one of the operand is True. If both are False then it will return False.
Truth table of logical or
operator.
A | B | A or B |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
In the following Python program we will get True
if any one of the expression evaluates to True.
# variables
a = 10
b = 20
x = 40
y = 50
m = a <= b # this will give True
n = y < x # this will give False
print("Result: ", (m or n))
Output of the above code.
Result: True
not
The logical not
operator will give True value if the operand is False. And it will return False value if the operand is True.
Truth table of logical not
operator.
A | not A |
---|---|
False | True |
True | False |
In the following example we will get True
only if the expression evaluates to False.
# variables
a = 10
b = 20
m = a > b # this will give False
print("Result: ", (not m))
Output of the above code.
Result: True
ADVERTISEMENT