Skip to content

Logical operators in Python

  • by

Python Logical operators are used to combine conditional statements. Python has three Logical operators are the and, or, not operators.

andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

Logical operators in Python

Simple example code perform Logical AND, Logical OR and Logical NOT operations.

x = True
y = False

print('x and y is',x and y)

print('x or y is',x or y)

print('not x is',not x)


Logical operators in Python

Check multiple conditions at the same time.

price = 9.99

print(price > 9 and price < 10)

print(price > 10 or price < 20)

print(not price > 10)

Precedence of Logical Operators

When you mix the logical operators in an expression, Python will evaluate them in the order which is called the operator precedence.

The following shows the precedence of the not, and, and or operators:


Python will evaluate them from the left to right:

a or b and cmeansa or (b and c)
a and b or c and dmeans(a and b) or (c and d)
a and b and c or dmeans((a and b) and c) or d
not a and b or cmeans((not a) and b) or c

Do comment if you have any doubts or suggestions on this Python operator topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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