Tag: Character returned simple library color model parameter means []
Almost all objects can be compared, tested, and converted to a string (in fact, using the repr () function, or a slightly different str () function to convert)
1 whether the object is true
any object can test the true value, the condition for the if or while, or the operand of the following Boolean operation. The following values are considered false:
None
False
0 of any numeric type, for example,0, 0L,0.0, 0j.
Any empty sequence, for example, ", (), [].
Any empty mappings, for example,{}.
An instance of a user-defined class, if the class defines a __nonzero__ () or __len__ () method, when the method returns an integer of 0 or a boolean value of False . [1]
All other values are considered true-so many types of objects are always true.
Unless otherwise noted, the result is that the operation of the Boolean value and the built-in function always return 0 or false for false,1 or true for true (Important exception: Boolean operator or and and and always return an operand of them. )
2 Boolean operation OR and not
These are Boolean operations, which are prioritized in ascending order:
Operation |
Results |
Note |
x or y |
If x is false, then return y, otherwise return x |
(1) |
x and y |
If x is false, then return x, otherwise return y |
(2) |
Not x |
Returns Trueif X is false, otherwise false |
(3) |
Note:
- This is a short-circuit operator, so the second argument is evaluated only if the first argument is false.
- This is a short-circuit operator, so the second argument is evaluated only if the first argument is true.
- not is less than the priority of non-Boolean operators, so not a = = B is interpreted as not (a = = b) ,a = = not b is a syntax error.
Compare operations
All objects support comparison operations. They all have the same priority (higher than the boolean operation). The comparison can be arbitrarily linked; For example,x < y < = Z equals x < Y and y < = Z, only y is calculated once (but in both cases x < Y is not calculated when Z is false).
The following table summarizes the comparison operations:
Operation |
meaning |
Note |
< |
Strictly less than |
|
<= |
Less than or equal to |
|
> |
Strictly greater than |
|
>= |
Greater than or equal to |
|
== |
Equals |
|
!= |
Not equal to |
(1) |
Is |
The ID of the object |
|
is not |
Different object IDs |
|
Note:
- ! = can also be written as <>, but this is only used to keep backwards compatibility. The new code should always use! =.
different types of objects, except for different numeric and string types, are never equal; The results of this sort of objects are always consistent but the order is random (allowing the ordering of heterogeneous arrays to produce consistent results). In addition, some types (for example, file objects) support only degenerate comparison concepts, and any two objects of that type are not equal. Similarly, the order in which such objects are ordered is random but will always be consistent. when any one operand is a complex number, the<, =, > , and > = operators throw TypeError exception.
Non-identical instances of a class are typically not equal unless the class defines the __eq__ () or __cmp__ () method.
An instance of a class cannot typically be sorted with other instances of the same class or other types of objects unless the class defines a sufficiently rich comparison method (__ge__ (),__le__ (),__gt__ (),__lt __ ()) or the __cmp__ () method.
[Python Learning Article] [Book Learning] [Python Standrad Library] [Built-in type] Object Test True Value, Boolean operation, comparison operation