And and or in Python are quite different from other languages
And and or in other languages are the result of returning a bool type, not python. It returns one of the values that do the and and or operations.
The value that determines the value of the expression returns that value.
Copy the Code code as follows:
>> 5 and ' '
The result here is ', an empty string, because it causes the expression to be false.
That is, in and, only the previous value is false, it is possible to become a result. Otherwise, the result is the value that follows.
Knowing the process of and and or running, you can implement the expression:
Copy the Code code as follows:
if (expression):
Return TrueValue
Else
Return Falsevalue
And this equivalent notation is (similar to the expression):
Copy the Code code as follows:
expression and TrueValue or Falsevalue
#或者
(Expression and (TrueValue,) or (Falsevalue,)) [0] #感谢snake117提供的建议
#这种是避免truevalue有可能是false的情况, such as an empty string