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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Short-Circuiting the Evaluation

00:00 In this lesson, you’ll see how Python makes use of something called short-circuit evaluation to sometimes speed up program execution. Short-circuit evaluation, sometimes called lazy evaluation, takes advantage of something you learned in a previous lesson.

00:18 Remember that sometimes you never even have to evaluate the second operand of an and operation. And Python knows that too. If the first expression is false, the result of the entire operation is False.

00:31 There’s no need to evaluate the second expression. And this is what Python does. Since often that second expression isn’t evaluated, this can speed up your program execution.

00:45 To demonstrate short-circuit evaluation, we’ll use these two functions. Each returns a True or a False value but prints a statement before that, so you’ll see when each function is executed. And I have those two functions in this Python file called short_circuit.py.

01:04 I’ll import them into my REPL, and you’ll see when each is executed, when used with the and operation. First, import those functions.

01:19 Now, do tests using them with the and operation. Let’s try True and False first.

01:34 As you can see, both functions were executed before the result was displayed. Now try False first, then True.

01:50 You can see only the False function was evaluated. There was no need to evaluate the True function, since the result of the and operation was already determined. Now, try where both are false.

02:12 Again, only the left operand was evaluated. Since it was known to be false, there was no need to evaluate the right. And finally, when both are true,

02:29 both functions had to be evaluated because Python couldn’t tell from just the first expression what the overall result was going to be. Again, as the first expression evaluates to True, the second expression must be evaluated to determine the result of the and operation. Notice also the result in those cases is actually what the righthand operand evaluated to.

02:51 And if the first expression evaluated to False, that was the result of the entire operation, and there was no need to evaluate the second. And that’s how short-circuit evaluation works.

03:05 How might you as a programmer take advantage of short-circuit evaluation? Here are a couple of suggestions. If one of your two expressions takes much longer to evaluate than the other, take that expression and put it last.

03:18 There’s a possibility it won’t be executed at all. Sometimes another idea is to put an expression that you know will most likely be false first. So when it does evaluate to False, there’s no need to evaluate the second.

03:35 Next, you’ll explore how and can be used with things other than Boolean expressions.

Become a Member to join the conversation.