A logical operator is a symbol or word that connects two or more expressions so that the value of the produced expression created is solely determined by the value of the original expressions and the operator’s meaning.
The following are the logical operators available in Python:
and
or
not
The table below explains each of the logical operators in Python.
Operator Symbol | Operator Name | Example | Description |
and | Logical And | Operand-A and Operand-B | It returns True, when both the operand-A and operand-B is True, otherwise it returns False. |
or | Logical Or | Operand-A or Operand-B | It returns True, when either the operand-A or operand-B is True, otherwise it returns False. |
not | Logical Not | not Operand-A | It returns the operand's logical state in reverse. |
The following figure is the visual representation of the logical operators in Python.
The code below illustrates how to use these logical operators in Python.
A=10B=20C=30#And Operator#Both Trueprint "The value of (A<B and B<C): ",(A<B and B<C);#First operand: True Second Operand: Falseprint "The value of (A<B and B>C): ",(A<B and B>C);#Both Falseprint "The value of (A>B and B>C): ",(A>B and B<C);#Or Operator#Both Trueprint "The value of (A<B or B<C): ",(A<B or B<C);#First operand: True Second Operand: Falseprint "The value of (A<B or B>C): ",(A<B or B>C);#Both Falseprint "The value of (A>B or B>C): ",(A>B or B>C);#Not operatorD=True#reverse of true logical stateprint "The value of not(D): ",not(D);