and Operation : Only two Boolean values are true when the result is true.
True and True # ==> truetrue and False ==> Falsefalse and True ==> falsefalse and false ==> false
or Operation : evaluates to true as long as there is a Boolean value of true.
True or True # ==> truetrue or False ==> truefalse or True ==> TrueFalse or False ==> false
Non-operation : Turns true to false, or false to true:
Not True # ==> falsenot False ==> Tru
Boolean operations are used in a computer to make conditional judgments, and the computer can automatically execute different follow-up code, depending on the result of the calculation as true or false.
In Python, Boolean types can also do and, or, and not with other data types, see the following code:
A ='a=t'a=f'
The result of the calculation is not a Boolean type, but the string ' a=t ', which is why?
Because Python regards 0
, 空字符串‘‘
and None
is treated as False, other numeric values and non-empty strings are considered True, so:
True and ' a=t ' evaluates to ' a=t ' continue to calculate ' a=t ' or ' a=f ' results or ' a=t '
To explain the above results, it also involves an important law of the and and or operations: short-circuit calculation.
Beginner Python's Boolean type