ZhangZhihui's Blog  

Unlike some other languages, in Python, the logical operators "and" and "or" do not return Boolean values True or False; they actually return the last evaluated operand. We generally use these operators in if and while conditions, so we do not get to know what they return exactly because, in those cases, only their truth value is used. Let us see what they actually return.

>>> 0 and 4

0

>>> 4 and 8

8

>>> 0 or 4

4

>>> 4 or 8

4

For and operator, the second operand is not evaluated if the first one is False.

In the expression 0 and 4, the interpreter evaluates the first operand; it is False, so there is no need to evaluate the second operand. 0 is the last evaluated operand and so it is returned.

In the expression 4 and 8, the first operand is True, so the second operand has to be evaluated, and so here, 8 is the last evaluated operand, and it is returned.

For or operator, the second operand is not evaluated if the first one is True.

In the expression 0 or 4, the first operand is False, so the second operand has to be evaluated. 4 is the last evaluated operand, so it is returned.

In the expression 4 or 8, the first operand is True; there is no need to evaluate the second one. Thus, 4 is the last evaluated operand, so it is returned.

The expression operand1 and operand2 first evaluates operand1; if it is False, its value is returned; otherwise, operand2 is evaluated and its value is returned.

The expression operand1 or operand2 first evaluates operand1; if it is True, its value is returned; otherwise, operand2 is evaluated, and its value is returned.

These operators actually return operands, but most of the time, they are used in a Boolean context, so only their truth value is used. The fact that they return the last evaluated argument can be used by programmers in certain situations.

Suppose we have a string s, and if it is empty, it has to be replaced by a default value, 'NA'. We can write this:

s = s or 'NA'

If the string s is empty, the first operand s will be False, so the second operand will be evaluated, and it becomes the value of the expression. If the string s is not empty, the second operand will not be evaluated, and the value of the expression s or 'NA' will be s only. Here is another example:

average = count!=0 and total/count

Here, we are finding the average and guarding our division by using the and operator. If count is not equal to zero, the first operand will be True, so the second operand will be evaluated, and its value will be returned and assigned to average.

If count is equal to 0, the first operand will be False, so the second operand will not be evaluated, thus avoiding divide by zero error. The value of the first operand will be assigned to average. So, average will be assigned False. In Python, False is numeric value 0, and True is numeric value 1. So, when we use this average in mathematical context, value 0 is used.

The operator "not" always returns Boolean value True or False; True if its argument is falsy, False if its argument is Truthy.

 

posted on 2024-07-29 21:06  ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报