## Value Comparisons

The operators  `<` ,  `>` ,  `==` ,  `>=` ,  `<=` , and  `!=`  compare the values of two objects. The objects do not need to be of the same type.

The chapter  Objects, Values, and Types  states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical method to access the value of an object. Furthermore, there is no requirement that the value of an object must be constructed in a particular way, e.g., composed of all its data attributes. Comparison operators implement a particular notion of what an object's value is.

The default behavior for equality comparison ( `==`  and  `!=` ) is based on object identity. Therefore, comparison of instances with the same identity results in equality, and comparison of equality of instances with different identities results in inequality.

No default comparison order ( `<` ,  `>` ,  `<=` ,  `>=` ) is provided; an attempt generates a  `TypeError` .

The following list describes the comparison behavior of the most important built-in types:

* Numbers : Built-in numeric types ( `int` , `float` , `complex` ) and types from the standard library ( `fractions.Fraction` and `decimal.Decimal` ) can be compared with themselves and among their types, with the restriction that complex numbers do not support order comparisons. Within the limits of the involved types, they are compared mathematically correctly without loss of precision.
* None and NotImplemented : They are singletons. PEP 8 advises that comparisons for singletons should be done with `is` or `is not` , never with equality operators.
* Binary Sequences : Instances of `bytes` or `bytearray` compare lexicographically using the numeric values of their elements.
* Character Strings : Instances of `str` compare lexicographically using Unicode code points (the result of the built-in `ord()` function) or their characters.
* Sequences : Instances of `tuple` , `list` , or `range` can only be compared within their types, with the restriction that ranges do not support order comparisons. Equality comparisons between these types result in inequality, and order comparisons between these types generate `TypeError` . They compare lexicographically using comparison of their corresponding elements.
* Mappings : Instances of `dict` compare equal if and only if they have the same `(key, value)` pairs.
* Sets : Instances of `set` or `frozenset` can be compared with each other and among their types. They define order comparison operators with the intention of checking subsets and supersets.
* Other Built-in Types : Most other built-in types do not have comparison methods implemented, so they inherit the default comparison behavior.

User-defined classes that customize their comparison behavior should follow some consistency rules, if possible:

* Equality comparison should be reflexive.
* Comparison should be symmetric.
* Comparison should be transitive.

If any of these conditions are not met, the resulting behavior is undefined.
