Python built-in type (2) -- Boolean operation, python Boolean
In python, the bool operators are sorted by priority.or
,and
,not
, Whereor
,and
Short-circuit Operator
not
Test the true value of the expression before obtaining the inverse value.
not
The operator has only one expression,not
Test the true value of the expression and then obtain the reverse value. The returned result is notTrue
YesFalse
>>> expression1 = ''>>> expression2 = '1'>>> not expression1True>>> not expression2False
or
,
and
The result returned by the operator is the result of one of the expressions that meet the logical conditions in the expressions on both sides of the operator.
In other languages, for example, C # And bool operations, the result must be bool values. But in python, this is not the case. It returns the value of one of the expressions that meet the bool operation conditions.
Ifx
IsTrue
, The result isx
Ifx
IsFalse
, The result isy
.
>>> expression1 = '1'>>> expression2 = '2'>>> expression1 or expression2'1'>>> expression2 or expression1'2'
Ifx
IsFalse
, The result isx
Ifx
IsTrue
, The result isy
.
>>> expression1 = ''>>> expression2 = {}>>> expression1 and expression2''>>> expression2 and expression1{}
or
,
and
Short-circuit Operator
Short-circuit operators mean that the expressions left and right of the operators are evaluated only when a value is required. For examplex or y
, Python evaluate from left to right, first expressionx
True Value test, if the expressionx
Is the true value, accordingor
Operator features, regardlessy
What is the bool result of an expression? The result of an operator is an expression.x
, So the expressiony
Does not evaluate. This behavior is calledShort Circuit.
# The function checks whether a number is an even number def is_even (num): print ('input num is: ', num) return num % 2 = 0 # is_even (1) is short-circuited, not executed >>> is_even (2) or is_even (1) input num is: 2 True >>> is_even (1) or is_even (2) input num is: 1 input num is: 2 True
or
,
and
You can use multiple operators in combination. When using this operator, perform a short circuit from left to right and finally enter the result.
Expressionx or y and z
, Will firstx or y
Evaluate the value, and then sum the resultz
While still following the short circuit principle.
# Is_even (2) and is_even (4) are all short-circuited >>> is_even (1) and is_even (2) and is_even (4) this num is: 1 False # is_even (1) is False, is_even (3) is short-circuited # is_even (1) and is_even (3) is False, is_even (5) required value # is_even (1) and is_even (3) or is_even (5) is False, is_even (7) is short-circuited >>> is_even (1) and is_even (3) or is_even (5) and is_even (7) this num is: 1 this num is: 5 False
not
Operator Priority Ratio
or
,
and
When used together
not
, And then calculate
or
,
and
Value
>>> is_even(1) or is_even(3)this num is : 1this num is : 3False>>> not is_even(1) or is_even(3)this num is : 1True>>> is_even(1) or not is_even(3)this num is : 1this num is : 3True>>>
not
Operator Priority Ratio
==
,
!=
Low,
not a == b
Will be interpreted
not (a == b)
,
a == not b
A syntax error is prompted.
>>> not 1 == 1False>>> 1 == not 1SyntaxError: invalid syntax>>> not 1 != 1True>>> 1 != not 1SyntaxError: invalid syntax