Tutorialspoint
Problem
Solution
Submissions

Chain Comparison Operators

Certification: Basic Level Accuracy: 33.19% Submissions: 238 Points: 5

Write a Python program to chain multiple comparison operators.

Example 1
  • Input: a = 5, b = 10, c = 15
  • Output:
    • a < b < c → True
    • a > b < c → False
  • Explanation:
    • Step 1: For a < b < c, check if a < b (5 < 10) and b < c (10 < 15).
    • Step 2: Since both conditions are True, the result is True.
    • Step 3: For a > b < c, check if a > b (5 > 10) and b < c (10 < 15).
    • Step 4: Since a > b is False, the entire expression evaluates to False.
Example 2
  • Input: x = 100, y = 200, z = 300
  • Output:
    • x < y < z → True
    • x > y > z → False
  • Explanation:
    • Step 1: For x < y < z, check if x < y (100 < 200) and y < z (200 < 300).
    • Step 2: Since both conditions are True, the result is True.
    • Step 3: For x > y > z, check if x > y (100 > 200) and y > z (200 > 300).
    • Step 4: Since both x > y and y > z are False, the entire expression evaluates to False.
Constraints
  • -10^6 ≤ value ≤ 10^6 for each variable
  • All input values are integers
  • Maximum chain length: 5 comparisons
  • Time Complexity: O(1)
  • Space Complexity: O(1)
ListWiproTech Mahindra
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Chain comparisons automatically use logical AND
  • Python evaluates chains from left to right

The following are the steps to Chain Comparison Operators:

  • Define a function chain_comparison(a, b, c).
  • Use chained comparison operators (<, >, <=, >=, ==, !=) to compare values in a single expression.
  • Return the boolean result of the comparison.
  • Call chain_comparison(5, 10, 15) and print the result.

Submitted Code :