Logical operators in Python 3
Logical operators in Python are used to combine expressions containing comparison operators. The output of a logical expression is always True or False.
Various Logical Operators in Python are:
- Logical AND
- Logical OR
- Logical NOT
(i) Logical AND (and):
Logical AND operator is represented as word and. It is used to combine two expressions containing comparison operators.
If any input of this operator is False, output would be False. If all the inputs of this operator are True, output would be True.
Truth table for Logical AND operator is
Input1 | Input2 | Output |
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Examples:
(i) 10 >3 and 10>5 = True
because 10>3 returns True , 10>5 also returns True, so True and True returns True .
(ii)10 >3 and 10<5 = False
because 10>3 returns True , 10<5 also returns False so True and False returns False .
(iii) 10 <3 and 10<5 = False
Because 10<3 returns False , 10<5 also returns False, so False and False returns True .
(ii) Logical OR (or):
Logical OR operator is represented as word or. It is used to combine two expressions containing comparison operators.
If any input of this operator is True, output would be True. If all the inputs of this operator are False, output would be False.
Truth table for Logical OR operator is
Input1 | Input2 | Output |
False | False | False |
False | True | True |
True | False | True |
True | True | True |
Examples:
(i) 10 >3 or 10<5 = True
Because 10>3 returns True , 10<5 returns False, so True or False returns True.
(ii)10 >3 or 10>5 = True
As10>3 returns True , 10>5 also returns True, so True or True returns True .
(iii) 10 <3 or 10<5 = False
As10<3 returns False , 10<5 also returns False, so False or False returns False
(iii) Logical NOT(not)
Logical NOT operator is represented by word not in Python . It is used to reverse the output of a relational expression or logical expression.
If input of this operator is True, output would be False. If input of this operator is False, output would be True.
Truth table for Logical NOT operator is
Input | Output |
False | True |
True | False |
Examples:
(i) not (10 >3) = False
because 10>3 returns True , Not (True) returns False.
(ii) not (10 <3 and 10<5) = True
because 10 <3 and 10<5 returns False , not(False) returns True.