Logic operations
Python also has standard logical connectives (and, or, not), although the resulting
expressions are not necessarily Boolean (True or False). For example, consider the logical
AND operation, also known as conjunction:
x and y
You might think that this should give the value True, one of Python’s special Boolean
logic objects, if x and y both evaluate to True in a conditional context (imagine each tested
in an if statement), and the value False otherwise; the other logic object. However, the
situation is more subtle than that, because the operation doesn’t necessarily give back True
or False logic objects. Rather, it gives back the x or y value, one of the values you put in,
which only evaluate as being equivalent to True or False in a conditional test.
For example, given that we know that zero is considered to be False in a conditional
test and other numbers are considered to be True:
z = 0 and 3 # z equals 0 rather than False
z = 1 and 2 # z equals 2 rather than True
z = 5 and 0 # z equals 0 rather than False
z = x and y # z might equal x or y, depending on value
Considering the last line, if x evaluates to True in a conditional context then z evaluates
to y; the truth is determined by the second item. And conversely, if x evaluates to False in
a conditional context then z evaluates to x; which is known to be false. Naturally, this also
holds when the values we are using in the logical operation are not so obviously True or
False in comparisons:
[] and 5 # [] (False in conditional context)
3 and 5 # 5 (True in conditional context)
3 and () # () (False in conditional context)
Moving on from the AND operation there is also the OR operation, also known as
disjunction:
x or y
Here, if x evaluates to True in a conditional context then the statement x or y
immediately gives back x, and y is not even evaluated (given it doesn’t matter whether it
is true or false). If x evaluates to False in a conditional context then x or y evaluates to y.
There is also negation, the NOT operation:
not x
This does evaluate to a proper Boolean, so it evaluates to True if x evaluates to False in
a conditional context, and vice versa.
If a special Boolean object is definitely required from an operation like AND or OR,
rather than one of the values in the comparison, you can explicitly convert the result using
bool():
x = 3 and 5 # x is 5
y = bool(x) # y is True
Although it may seem a little odd, you can do the above in one step and use the logic
operation as if it were a single argument:
y = bool(3 and 5) # y is True
In many circumstances explicitly converting the result of a logic operation to Boolean
True or False is not required. Going further, it is sometimes positively useful to get back
one of the actual values that was put into the operation. For example, suppose you have a
variable x, which is sometimes true and sometimes not, and you wanted a text string that
said ‘Yes’ if x was True and ‘No’ if x was False. You could do:
if x:
text = "Yes"
else:
text = "No"
But an alternative would be to write:
text = x and "Yes" or "No"
Here, if x is True then the AND operation evaluates to ‘Yes’ and the OR operation
preserves this. If x is False then the AND operation evaluates to False and the OR
operation evaluates to ‘No’. This is perhaps a bit of trickery, based on the strings being
true, but it can be convenient.
There is one more subtle point with these Boolean True operations. Considering the
statement x or y, if x turns out to be true in a conditional context then the expression y
would not be evaluated at all, given that the OR statement will be true no matter what y is.
The same idea applies to x and y: this time y is not evaluated when x is false. This turns
out to come in quite handy. For example, if x is a tuple or list, we could do:
x = [256, 128]
if x and x[0] > 10:
# etc.
Here, if x is an empty list (so evaluates to False in a conditional context) then the
second expression is not even evaluated, which is just as well since it would fail and
generate an exception (error), because x[0] does not exist.
As an example of a conditional statement, we could have:
x = [123, 54, 92, 87, 33]
if len(x) > 10:
x.append(999)
elif len(x) > 3:
x.append(888)
else:
x.append(777)
# x now [123, 54, 92, 87, 33, 888]
In this case, since len(x)=5, the if expression evaluates to False whereas the elif
expression evaluates to True and so 888 is appended to x, and the else block is skipped.
There is another conditional statement allowed from Python 2.5 onwards:
expression1 if conditionalExpression else expression2
This reads a bit oddly because the condition is in the middle, but what this means is that
if the conditionalExpression evaluates to True it evaluates to expression1 and otherwise it
evaluates to expression2. Thus instead of writing slightly tricky code like:
text = x and "Yes" or "No"
you can instead write:
text = "Yes" if x else "No"
Loops
When you have a collection (tuple, list, set, dictionary …) it is quite natural to want to
consider every element in the collection in turn, processing each in some way. This
process is often called iteration. This is the job of the ‘for’ loop.
Do'stlaringiz bilan baham: |