Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:02:46+00:00 2026-06-06T03:02:46+00:00

This morning, I find myself writing something like: if (a == b == c):

  • 0

This morning, I find myself writing something like:

if (a == b == c):
  # do something

And was surprised that it gave me the expected result.

I thought it would behave as:

if ((a == b) == c):
  # do something

But it obviously didn’t. It seems Python is treating the first statement differently from the second, which is nice but I couldn’t find any documentation or explanation regarding this.

I tested and got this:

In [1]: 2 == 2 == 2
Out[1]: True

In [2]: (2 == 2) == 2
Out[2]: False

Would someone care to explain me what are the rules regarding such “chaining” of == (or !=) ?

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:02:49+00:00Added an answer on June 6, 2026 at 3:02 am

    This works with all comparison operators – eg, you can also do:

    >>> 4 < 5 < 6
    True
    >>> 4 < 5 !=2
    True
    

    In general, according to the documentation, a op1 b op2 c where op1 and op2 are any of: <, >, !=, ==, <=, >=, is , is not, in or not in will give the same result as:

    a op1 b and b op2 c
    

    The docs also say that this can work with arbitrarily many comparisons, so:

    >>> 5 != '5' != 'five' != (3+2)
    True
    

    Which can be a slightly confusing result sometimes since it seems to say 5 != (3+2) – each operand is only compared with the ones immediately adjacent to it, rather than doing all possible combinations (which mightn’t be clear from examples using only ==, since it won’t affect the answer if everything defines __eq__ sanely).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched google this morning and could not find an answer, so I would
This morning we found an old chunk of code that was causing a library
I found out this morning that proc.new works in a class initialize method, but
So, my friend sent me a puzzle this morning: Find the number of distinct
I've been looking on SO this morning to find a way of creating a
I have looked through this a number of times this morning and cannot find
I continuously find myself having to write timed windows services that poll outside queues
I was tasked this morning to find a replacement spell check ActiveX component to
Simple one this morning, but causing me issues and can't seem to find any
I started using Blueprint CSS at about 8AM EDT this morning and I find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.