- Related Questions & Answers
- Chaining comparison operators in C#
- Python Comparison Operators
- How to overload Python comparison operators?
- What are Comparison Operators in JavaScript?
- Relational and comparison operators in C++
- Explain Python class method chaining
- What is the use of comparison operators with MySQL subquery?
- Exception chaining in Java
- Method Chaining in JavaScript
- Division Operators in Python?
- Basic Operators in Python
- Overloading Operators in Python
- Python Arithmetic Operators
- Python Assignment Operators
- Python Bitwise Operators

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Sometimes we need to use more than one condition checking in a single statement. There are some basic syntax for these kind of checking is x < y < z, or if x < y and x < z etc.

Like other languages, there are some basic comparison operators in Python. These comparison operators are <, <=, >, >=, ==, !=, is, is not, in, not in.

The precedence of these operators are same, and the precedence is lesser than arithmetic, bitwise and shifting operators.

These operators can be arranged arbitrarily. They will be used as a chain. So for an example, if the expression is x < y < z, then it is similar to the x < y and y < z. So from this example, we can see if the operands are p_{1}, p_{2},..., p_{n}, and the operators are OP_{1}, OP_{2},..., OP_{n-1}, then it will be same as p_{1} OP_{1} p_{2} and p_{2} OP_{2} p_{3}, , p_{n-1} OP_{n-1}p_{n}

So there are some examples on the chaining features of comparison operators.

a = 10 b = 20 c = 5 # c < a < b is same as c <a and a < b print(c < a) print(a < b) print(c < a < b) # b is not in between 40 and 60 print(40 <= b <= 60) # a is 10, which is greater than c print(a == 10 > c)

True True True False True

u = 5 v = 10 w = 15 x = 0 y = 7 z = 15 # The w is same as z but not same as v, v is greater than x, which is less than y print(z is w is not v > x < y) # Check whether w and z are same and x < z > y or not print(x < w == z > y)

True True

Advertisements