Chaining Comparison Operators in Python

by | Jan 8, 2023 | Python

Home » Python » Chaining Comparison Operators in Python

Introduction

This article illustrates how to use chaining comparison operators in Python. In all programming languages, we are often faced with situations where we want to check more than two conditions at a time. A simple example can be to find out the largest or smallest out of three numbers.

a < b < c?

Generally, the naïve implementation of this will be to write an if statement and join the two checking conditions with an and keyword like the following:

if a < b and b < c:
{
…#body of the if block
}

To facilitate checking for more than two conditions at once, Python has this special feature called chaining comparison operator. Using this, the above if block can be written as follows:

if a < b < c:
{
#body of the if block
}

Following the associativity and precedence rule of Python, all comparison operators hold the same priority in a hierarchical order. This priority is lower than that of arithmetic, shift, and bitwise operators. All comparison operators in Python follow the execution process as it would have if the operation was done in mathematics. This makes it easier for users to understand how the operators evaluate a certain result.

Comparison Operators in Python

“>” | “<” | “==” | “>=” | “<=” | “!=” | “is” [“not”] | [“not”] “in”

Chaining in Comparison Operators

  • The return type after comparison by the operator is boolean: true or false
  • You can chain the operands that you want to compare in any order as the following:
    x < y <= z is the same as x < y and y <= z. In the first case, y is evaluated only once but in the second case, y is evaluated twice. However, for both cases, if x < y is found to be false, then the operation does not go to z at all.
  • As a general case, is a, b, and c are expressions or operands and x, y and z are operators, then writing a x b y c is the same as writing a x b and b y c. You can separate more than two comparisons by and keyword. The only difference here is that in the first chained operator case, all the operands are evaluated only once. But in cases where you use and keyword, some operands are evaluated more than once.
  • If we are writing a > b > c, then keep in mind we are comparing a with b and b with c. We are not comparing a with c at all.

Sample Code

#Sample Code to show chaining comparison operators in Python
x = 5
print (1 < x < 10)
print (10 < x < 20)
print (x < 10 < x*10 < 100)
print (10 > x <= 9)
print (5 == x > 4)

OUTPUT

True
False
True
True
True

#Sample Code 2 to show chaining comparison operators in Python
a, b, c, d, e, f = 0, 5, 12, 0, 15, 15
exp1 = a <= b < c > d is not e is f
exp2 = a is d > f is not c
print (exp1)
print (exp2)

OUTPUT
True
False

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author