Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Chaining Comparison Operators

00:00 All right, so what’s the idea with chaining comparison operators? Consider the following if statement. The condition is if x is less than y and y is less than or equal to z, then we want to execute the function do_something(). Notice that y appears on the right in the first comparison, in the x < y, and then y appears on the left of the second comparison, in y <= z.

00:29 Then what you can do is combine both of those comparison operations into a single chained operation, like this. So, you can get rid of the and operator and then simply write x < y <= z.

00:47 Here’s another example. We’re going to run the function do_something() if x is not equal to n and n is in some_list.

00:56 Notice that the in is not a value comparison, but a membership comparison. This can be shortened to if x != n in some_list. However, doing something like this introduces some issues with readability, and readability starts to degrade quickly when you use three or more comparison operators in a chain of comparisons.

01:22 Let me show you another example. In this example, we combine different types of comparison operators—equality operators, or less than or equal to operators, or a membership operator. So, for example, 1 is less than 2, 2 is in the list [1, 2, 3], the list of [1, 2, 3] is greater than or equal to the list [1, 2], the [1, 2] list is certainly not equal to the integer 4, and 4 evaluates to 8/2.

01:53 So this chain of comparisons returns True. However, readability is important and you want to write code that you can read now and in the future and so that it’s easy for others to read as well.

02:08 So, you just saw that comparison chains are just chains of and calls. So, for example, this chain of comparisons that 1 is less than 2, 2 is in the list [1, 2, 3], and the list [1, 2, 3] is greater than the list [1, 2] can be replaced with these two and calls.

02:28 However, there is an advantage in using chained comparisons over the equivalent expression involving and calls. For example, in this if statement where we’re going to execute the make_purchase() function provided that purchase_price() is less than account_balance() and account_balance() is less than the sell_price()—in this chained comparison, the account_balance() function is evaluated only once.

02:54 Whereas if we replace this with the equivalent and call, the account_balance() function would be evaluated twice using an and call.

03:02 It would be evaluated once, first on the left-hand side of the and call, where we’re checking whether purchase_price() is less than account_balance(), and then a second time when we check whether account_balance() is less than sell_price().

03:17 So if it’s computationally expensive to run account_balance(), then you’re better off using a chained comparison. And in this case, readability is still pretty good.

03:28 We’ll see an example in the upcoming code on how if you use a chained comparison all expressions are evaluated only once.

03:38 Let’s see an example of how in a comparison chain the expressions involved in the comparison are only evaluated once. Go ahead and define a few functions that will contain print statements so that you can see when they’re executed. So for this u() function, write out something like "I am returning u" or "I am u".

04:02 And then this function will return, say, the integer 1,

04:08 and then let’s go ahead and define another function v(). This one is going to print "I am v" and it’s going to return the integer 2.

04:18 And then we’ll do one more. Let’s call this one w() and it’s going to print "I am w" and it’s going to return the integer 3.

04:30 So, u() returns the integer 1, and 1 is less than the value returned by v(). v() returns the value 2. And v(), which has a return value of 2, is less than the return value of w() since w() returns 3.

04:51 So, let’s go ahead and write u() < v() and v() < w(). This Boolean expression will evaluate to True because 1 is less than 2 and 2 is less than 3.

05:08 Notice that the function v() is evaluated twice. It needs to be evaluated once for the comparison on the left of the and operator and a second time when it appears on the right of the and operator.

05:23 However, now try chaining these comparisons. So go ahead and write u() < v(), and v() < w(). And you know that this is equivalent to the and operation that you just did, but notice now that the v() function is only evaluated once.

05:42 So in this situation, you’re better off choosing a chained comparison over an and operator because if that function in the middle takes a long time to run, then using a chained comparison only executes it once. All right! Well, that’s all there is to it to this lesson. In the next lesson, we’ll take a look at how Python decides how objects should be evaluated to True and False and how you can take advantage of this to write some pretty clean code.

06:09 I’ll see you then!

Become a Member to join the conversation.