You can learn about Operators in Python Programs with Outputs helped you to understand the language better.

Python Programming – Logical/Boolean Operators

Logical operators are also called Boolean operators. It combines the results of one or more expressions, and these are called logical expressions. After test¬ing the conditions, they return logical status True or False. Python tests an expression with and or operators from left to right and returns, the last value tested. There are three logical operators: and, or, and not.


The and-operator evaluates to True if both the expressions are true; False if either or both operands evaluate to False. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.


The or operator evaluates to True if either of operands evaluates to true; False if both operands evaluate to False. For example, n%2 == 0 or n%3 == 0 is true if either of the expression is true, that is, if the number is divisible by 2 or 3.


The not operator is used to reverse the logical state of its expression. Logical not return True if the expression is false and False if the expression is true. For example, the not operator negates a Boolean expression, so not (x > y) is true if x > y is false, that is, if x is less than or equal to y.
Comparisons may be combined using the Boolean operators and or, and the outcome of the comparison may be negated with not. These have lower priorities than comparison operators.
# Logical operator examples
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not b) # True

  • A Boolean expression is an expression that is either True or False.

Let’s Try

Assign the given values to variable and write the output of the following logical operations:

>» (2 = = 2) or (3 = = 5)

>>> 2>5 or 2<1

>>> 5>2 or 1<2

>>> (2==2) and (3==5)

>>> 2>5 and 2<1

>>> 5>2 and 1<2

>>> not(5>2)

>>> not(2>5)

Hierarchy of Logical Operators

The logical not operator has higher precedence than the others. The and-operator has higher precedence than the or operator. Both the logical and & or operators have lower precedence than the relational and arithmetic operators.
Precedence of Logical Operators
Python Programming - Logical/Boolean Operators chapter 4 img 4

Example 13.
Consider the following declarations:
j = 0
m = 1
n = -1
Evaluate the following expressions:
(a) j and m
(b) j < m and n <m
(c) m + n or not j
(a) The expression j and m is not true because the value of j = 0 and the value of m = 1. Hence, the result is 0 (zero),i.e, false.
(b) The expression j < m and n < m is equivalent to (j < m) and (n < m) because the precedence of relational operators is higher than that of logical operators. Substituting the values of the variables, you get the result as true.
(c) The expression m + n or not j is equivalent to (m + n) or (not j). Substituting the values of the variables, you get the result as true.

Leave a Reply

Your email address will not be published. Required fields are marked *