Today I saw such a way of writing when I was looking at other people's code. I felt that it was a pitfall that was easy to step on. After I figured it out, I wrote a memo.
When performing logical operations in Python, an operation rule called short-circuit logic is adopted by default. The name is very vivid. You can see that if you look directly at the code
print True and 1 # 1 print True or 1 # True print False and 1 # False print False or 1 # 1
below, although 1 will be regarded as Boolean value calculation, but the calculation result of the entire expression is not necessarily a Boolean value, but varies depending on the expression. The different results of the above expressions are short-circuit logic. Use vernacular In other words, once Python can determine whether the entire expression is True or False, subsequent calculations will not be performed. That is, the logic is short-circuited and subsequent expressions are ignored.
For exampleTrue or The result of 1
is True
because no matter what value follows or
, the result of the entire formula must be True, so Python sees True
, see or
, ignore the following 1, 1 is short-circuited, and return True
; the same is true for False and 1
, see False
Then and
No matter what follows, the result of the entire formula must be False
, so 1 is short-circuited, and False
is returned .
Correspondingly, why False or 1
returns 1 is because False
and or
cannot determine the result of the formula. The entire formula Whether the expression is True or False is determined by the value after or
, so Python needs to read the entire expression, so it returns 1.
After understanding what short-circuit logic is, Let’s take a look at Ternary operator
Ternary operator, also called ternary operator operator. Familiar with Java Students may know that its form is like this b?x:y
. For this expression, if the condition b
is True
, then the expression The result of the expression is x
. If b
is False
, then the result of the expression is y
. This is a very convenient way of writing, It is much simpler than the if statement.
However, in the early days of Python, there was no ternary operator written in a similar way, so there was a way to use short-circuit logic, using and
and or
to simulate the writing of the ternary operator. Let’s look at the code
A = X and 'table' or False
. This code uses short-circuit logic to simulate the ternary operator. When X
is # When ##True,
A will be assigned the value
'table', when
X is
Flase,
A will be assigned the value
False;
True and 0 or 1
True, and the expression should return
0 which is the result we expect, but in fact this expression will Return
1; Because the value of
True and 0 is
True, the value of the entire expression is determined by the value after
or , so Python will make a judgment on the following value, resulting in the return of
1, which is why
and...or is a pitfall in this writing method
if...else<a href="http://www.php.cn/wiki/111.html" target="_blank"></a>, please see the code for details
# <为真时的结果> if <判定条件> else <为假时的结果>0 if True else 1
will be the same as what we expect. When the condition is True
, the previous value will be returned. When the condition is False
, Return the following value. There will be no pitfalls. Just pay attention when writing. The order of conditions and return values is different from the ternary operator in Java. Just don’t make a mistake
The above is the detailed content of Introduction to the usage of Python ternary calculator and short-circuit logic method. For more information, please follow other related articles on the PHP Chinese website!