Logical Operator Truth Table
p q p and q p or q not(p)
false false false false true
false true false true true
true false false true false
true true true true false

Python Script to demonstrate Working of Logical Operator.


# Python script that shows how logical operators works.
p = 5
q = 5
r = 10
res = (p == q) and (r > q)
print("(p== q) and (r > q) is =", res);
res = (p == q) and (r < q);
print("(p == q) and (r < q) is =", res);
res = (p == q) or (r < q);
print("(p == q) or (r < q) is =", res);
res = not(p == q) or (r < q);
print("not(p == q) or (r < q) is =", res);
res = not(p != q);
print("not(p != q) is =", res);
res = not(p == q);
print("not(p == q) is =", res);
return 0;
}
Output:
(p == q) and (r > q) is True
(p == q) and (r < q) is False
(p == q) or (r < q) is True
not(p == q) or (r < q) is False
not(p != q) is True
not(p == q) is False

Program Explanation:
In the above Python script, we assign p=5,q=5 and r=10 i.e integer values are assigned to the variable.
The expression (p == q) and (r > q) evaluates to True because both operands (p == q) and (r > q) are true.
similarly Python interpreter evaluates the following expression.
(p == q) and (r < q) evaluates to false because operand (r < q) is false.
(p == q) or (r < q) evaluates to True because (p == q) is true).
not(p == q) or (r < q) evaluates to false because both operand not(p != q) and (r < q) are false.
not(p != q) evaluates to True because operand not(p != q) is false. Hence, not(p != q) is True.
not(p == q) evaluates to false because (p == q) is true. Hence, not(p == q) is false.

Previous Topic:-->> Relational Operators || Next topic:-->>Bitwise perators