Exc. #6 And


I didn't understand what was expected of me during that exercise? I spent a good 10 minutes reading through the instructions, going back to exc. 5, re-reading the notes I wrote down in my notebook and then just decided to copy paste the stuff in. But I can't fathom what was the point of exc. 6.


Judging from the title, the point is the logical operator, and. These are used in what I call compound expressions which are multi-expression logical expressions.

A == B

is a simple expression

A == B and B == C

is a compound expression consisting of two simple expressions, both of which must be True in order for the expression to yield True.

Logical AND expressions are said to short-circuit on False. That means if the first expression (the operand on the left) is False, then the whole expression is False, without even evalutating the expression on the right. If the first is True, and the second is not, the yield is False. Both must be True.

Bear in mind that we used independent expressions as our two operands. A == B, is one, and B == C is the other. They each yield a boolean. Now consider this,

A == B and C

It is very common to see this mistake in people's code. It doesn't raise any exceptions because it is perfectly valid. Only problem is, if C is the empty string, 0 or None it will always be falsy and if it is any other object or value it will always be truthy. Python will convert it to a boolean and evaluate the entire expression on that. Careful to never fall into this trap.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.