Table of Contents
Logical Operators
- Logical operators are used to combining two or more relational expressions.
- Python provides three different logical operators.
Operator Name | Meaning |
---|---|
&& | Logical and |
|| | Logical or |
! | Logical not |
Logical and
- The result of Logical AND will be true only if both operands are true.
Result_exp1 | Result_exp2 | Final Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example
Logical and operator
print( 10 > 15 and 5 < 10)
print( 5 < 10 and 10 > 6 )
Output
False
True
Logical or
- The result of Logical OR will be true if anyone operand is true.
Result_exp1 | Result_exp2 | Final Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example
Logical or operator
print( 10 > 15 or 5 < 10)
print( 5 < 10 or 10 > 6 )
Output
True
True
Logical not
- Logical NOT ( ! ) is used to reverse the value of the expression.
Result_exp1 | Final Result |
---|---|
0 | 1 |
1 | 0 |
Example
Logical not operator
print(not(5>10))
print(not(10>15) and (5<10))
print(not(5>10) or (9<6))
Output
True
True
True
True
True