nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
8 of 12
1What is an Interpreted language?
2Is Python a compiled language or an interpreted language?
3What is the difference between `/` and `//` in Python?
4Why can `a = 256; b = 256; a is b` return `True`, but `a = 257; b = 257; a is b` return `False` in some Python implementations?
5What is the difference between a shallow copy and a deep copy? When would each cause bugs?
6What causes a reference cycle, and how does Python's garbage collector handle it?
7What is the difference between `is` and `==`?
8How does Python evaluate chained comparisons like `1 < x < 10`?
9What are Python's built-in data types, and how are they categorized (mutable vs. immutable)?
10How does Python manage memory for objects internally (reference counting and the object model)?
11Why does Python not require explicit variable declarations, and how does dynamic typing affect variable assignment internally?
12Why can floating-point arithmetic in Python produce results like `0.1 + 0.2 != 0.3`? How would you correctly compare floats?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
08 / 12

How does Python evaluate chained comparisons like `1 < x < 10`?

Difficulty: 3/10
Operators & Expressions, Comparison Operators, Operator Chaining and Short-circuiting

Chained comparison semantics

Python treats a < b < c as (a < b) and (b < c), with two important differences from writing it out by hand: b is evaluated only once, and evaluation short-circuits, so if the first comparison is False the third operand is never evaluated. It is not parsed as (a < b) < c as in C-like languages, where you would compare a boolean to c. Any mix of comparison operators can be chained, including ==, !=, in, is and not in, and the chain can be arbitrarily long.

javascript

Why this matters: single evaluation is semantically important when the middle operand is a function call, property or expensive expression, so 0 < compute() < limit calls compute() once. It also makes range checks readable, which is why I always prefer 0 <= i < n over i >= 0 and i < n. Bytecode-wise, CPython duplicates the middle value on the stack and jumps out early on a false result; you cannot reproduce that with a normal function because function arguments are all evaluated eagerly.

Common mistakes: expecting a != b != c to mean 'all three distinct' (it only compares adjacent pairs, so a == c is allowed); writing exotic chains such as x < y > z that are legal but confusing; and using chained comparisons with numpy arrays or pandas Series, where the implicit 'and' calls bool() on an array and raises 'truth value of an array is ambiguous'. For arrays use (a > 0) & (a < 10) or np.logical_and, and for SQLAlchemy columns use .between() or & for the same reason. These libraries cannot override 'and', which is what the chain desugars to.

Trade-off: chained comparisons are a readability win for numeric ranges, but I avoid chains mixing different operator kinds (a == b in c) because reviewers must mentally expand them. This behaviour is stable across all Python 3 versions.

Scenario Questions

0-2 years experience

  1. 1Does 3 > 2 > 1 give the same result as (3 > 2) > 1? Evaluate both and explain.
  2. 2Rewrite if x >= 0 and x <= 100 using idiomatic Python.

2-5 years experience

  1. 1In 0 < f() < 10, how many times is f called, and what happens to the second comparison if the first is False?
  2. 2A teammate writes if a != b != c to mean 'all three are different'. Is that correct? What would you write instead?

5-8 years experience

  1. 1Chained comparisons on a numpy array raise a ValueError. Why, and how do you write the element-wise range check?
  2. 2You are designing an Interval or Date class that should work in chained comparisons. Which rich comparison methods do you implement, and when do you return NotImplemented?

8+ years experience

  1. 1Explain at the bytecode level how CPython compiles a < b < c so b is evaluated once and the chain short-circuits, and why a helper function or lambda cannot emulate it.
  2. 2Would you allow chained comparisons in a query-builder DSL? Why does 0 < col < 10 fail with SQLAlchemy or pandas, and how do those libraries work around it?

Follow-up Questions

  • Why can't you use chained comparisons on numpy arrays, and what do you use instead?
  • Does a != b != c check that all three values are distinct?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.