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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

How Pandas Uses Boolean Operators

Give Feedback

As filtering can be a bit tricky in Pandas, you’ll learn in this lesson how Pandas uses boolean operators.

rworreby on May 27, 2020

I actually want to point out a section that is wrong in this video, starting from 1:00 to 1:45:

As Python uses short-circuit evaluation for algebraic expressions, the expression in the video (4 < 3 and 5 > 4) would be treated the following way: 4 < 3 is False and therefore the expression would return False immediately (as “False and ” is always false). The statement in the video that 4 < 3 is evaluated, then 5 > 3 is evaluated and then both compared (???) is therefore wrong.

In the next section, short-circuit evaluation is mentioned, however, in the example that is given, there is no short-circuit evaluation, as its a nested comparison. It is also wrong that (3 and 5) “just” evaluates to 5. In a logical test, the result is the last evaluated statement. Its not the last argument.

As a reference, check the following things:

(3 and 5) # Evaluates to 5, no short-circuit eval
(5 and 3) # Evaluates to 3, no short-circuit eval
(0 and 5) # Evaluates to 0, short-circuit eval
(5 and 0) # Evaluates to 0, no short-circuit eval

I think in the video there is a wrong understanding of what short-circuit evaluation is. Truth tables is a thing to check out, but applied to boolean logic in python these are the base cases of short-circuit evaluation:

False and X # X is never evaluated
True or X # X is never evaluated

# No short-circuit
False or X # Could still evaluate to True or False overall, so X has to be evaluated
True and X # Could still evaluate to True or False overall, so X has to be evaluated

Matt Williams on Aug. 18, 2020

I don’t know that the statement above is correct, because if you perform the evaluation 5 < 3 or 3 < 4, the output is True. Obviously the interpreter looked at both numerical comparisons in the operation, otherwise it would have stopped at 5 < 3 and returned false, as rworreby is suggesting above.

Dan Bader RP Team on Aug. 18, 2020

Thanks for flagging this @rworreby. What’s referred to as “short-circuit evaluation” in the video around the 1:30 mark isn’t actually short-circuit evaluation but the evaluation of a nested boolean expression.

We’ll get the terminology cleared up with the next update of this course. Sorry about any confusion this may have caused.

In the meantime, please check out the following resources for a deeper look at short-circuit evaluation of boolean expressions in Python:

Become a Member to join the conversation.