Search⌘ K
AI Features

Be Careful With Chained Operations

Explore how Python handles chained operations by evaluating expressions only once. Understand comparison chaining and its effects to write clearer and more efficient Python code.

We'll cover the following...

Chained operations can be tricky sometimes. Let’s check them out!

C++
print((False == False) in [False]) # makes sense
print(False == (False in [False])) # makes sense
print(False == False in [False]) # now what?

Now, take a look at this.

C++
print(True is False == False)
print(False is False is False)

How about this?

C++
print(1 > 0 < 1)
print((1 > 0) < 1)
print(1 > (0 < 1))

Explanation

As per the ...