Logical Operators in Python

In the previous article we have explored the various Arithmetic operations that can be utilized in Python, in this article we will further elaborate on logical operations that can be performed on files within Python. Logical operators in python are symbols or word used to combine expressions in such a way that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.

Working with Logical Operators in Python

Logical operators are used to combine multiple comparison operators. There are three logical operators: or (OR), and (AND), not (NOT). The not operation is performed first , followed by and , the or operation is performed last . Logical Operators are mainly utilized in conditional-type statement which often result in a Boolean result. Logical expressions are used in complex conditional instructions and can act as checkpoints to screen unwanted conditions. When you have a series of logical expressions, for clarification, use one or more sets of parentheses to enclose each expression.

 Logical operators combine relations according to the following rules:

  • The ampersand (&) symbol is a valid substitute for the logical operator AND The vertical bar ( | ) is a valid substitute for the logical operator OR. The (!) symbol acts as a substitute for NOT operations.
  • Only one logical operator can be used to combine two relations. However, multiple relations can be combined into a complex logical expression.
  • Regardless of the number of relations and logical operators used to build a logical expression, the result is either true, false, or indeterminate because of missing values.

AND:  Both relations must be true for the complex expression to be true.

OR: . If either relation is true, the complex expression is true.

NOT: The value of relations doesn’t match the condition/expression

Logical operator “OR”

Returns True if at least one of the comparisons is true. If both comparisons are false, then the result of the operation will be False .

a = 5 > 7 or 2 < 3
print(a)
Output:
True
# Python program to demonstrate
# OR logical operator
  
a = 10
b = -10
c = 0
  
if a > 0 or b > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")
  
if b > 0 or c > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")
Output:
Either of the number is greater than 0
No number is greater than 0

Logical operator “AND”

Returns True if both comparisons are true. If at least one of the comparisons is false, then the result of the operation will be False .

a = 5 > 7 and 2 < 3
print(a)
Output:
False

Logical operator “NOT”

Inverts the comparison result. If the comparison is true, then the result of the operation will be False .If the comparison is false, then the result of the operation will be True .

a = not 5 > 7
print(a)
Output:
True
# Python program to demonstrate
# logical not operator
a = 10
  
if not a:
    print("Boolean value of a is True")
  
if not (a%3 == 0 or a%5 == 0):
    print("10 is not divisible by either 3 or 5")
else:
    print("10 is divisible by either 3 or 5")
Output:
10 is divisible by either 3 or 5

So we have learned in detail the usage of the AND, NOT, OR logical operators in python. In the next article we will go through the identity operators and their usage in Python.

Leave a Comment