Logical Operators



A business problem generally gets complex, and basic operators are unable to solve them, to enable a programmer to handle complex decision making logical operator are present. There three types of Logical Operator in Python

  • OR logical operator
  • AND logical operator
  • NOT logical operator
  • OR Logical Operator

In this logical operation, if either of the operands is true, the result is true.

XORYResult
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

Note: if the left operand is True then the right operand is not evaluated, and the result of the operation is True

AND Logical Operator

In this logical operation if both of the operands must be true, then only the result is true.

XANDYResult
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

Note: if the left operand is False then the right operand is not evaluated, and the result is False

NOT Logical Operator

This operator negates the value of the operand; one more point about this operator is that it requires only one operand.

XNOTResult
FalseTrue
TrueFalse

Now let us consider a small scenario, John goes to play soccer if

  1. Shoes are clean
  2. Not raining outside
  3. The ground has proper lighting

If all these conditions are True, then only John will go to play soccer otherwise he will not go. In this case Condition1 AND Condition2 AND Condition3

Assume that John is not fond of clean and tidy shoes so the 1st condition might be optional. So, in this case (Condition2 AND Condition3)

Identity Operator: compares the memory location of two variables, "is" identity operator, if they point to the same memory location the result is True otherwise False. "not is" if they point to the same memory location the result is False otherwise True. In this id() methods serves the job of fetching memory location of variables.

Membership Operator: We are yet to learn complex data types which are list, tuple, and dictionary. But if we need to search something from the objects of these data types we need to membership operator. These are "in" and "not in". As the name suggests "in" operator will return true if the keyword is present in the object and "not in" will return true if it is not present in the object.

This comes to the end of Operators, Practice as many examples as you can, this will help you in implementing the complex business logic with ease.