This concept exercise should teach how basic non-customized comparisons work in python and how to use them effectively.
- understand all comparison operations in Python have the same priority and are evaluated after arithmetic, shifting, or bitwise operations.
- understand all comparisons yield the boolean values True and False
- know that identity comparisons is and is not are for checking an objects identity only
- understand that
==
and!=
compare both the value & type of an object. - know where Python has altered the behavior of
==
and!=
for certainbuilt-in
types (such as numbers, or for standard library types like decimals, and fractions to allow comparison across and within type. - know that unlike numeric types, strings (
str
) and binary sequences (bytes
&byte array
) cannot be directly compared. - understand how comparisons work within
built-in
sequence types(list
,tuple
,range
) andbuilt-in
collection types
(set
,[dict]
) - know about the "special" comparisons
None
,NotImplemented
(comparing either should use identity operators and not equality operators because they are singleton objects) and NaN (NaN
is never==
to itself) - use the value comparison operators
==
,>
,<
,!=
with numeric types - use the value comparison operators
==
,>
,<
,!=
with non-numeric types - use
is
andis not
to check/verify identity
- rich comparison with
__lt__
,__le__
,__ne__
,__ge__
,__gt__
- understanding (and using the concept) that the
==
operator calls the dunder method__eq__()
on a specific object, and uses that object's implementation for comparison. Where no implementation is present, the default__eq__()
from genericobject
is used. - overloading the default implementation of the
__eq__()
dunder method on a specific object to customize comparison behavior. set operations
- performance considerations
- Comparison priority in Python
- Comparison operators
==
,>
,<
,!=
- Identity methods
is
andis not
- Equality applied to
built-in
types - Equivalence vs equality
- Inequality
basics
booleans
dicts
lists
sets
strings
tuples
numbers
iteration
- Comparisons in Python (Python language reference)
- Value comparisons in Python (Python language reference)
- Identity comparisons in Python (Python language reference)
- Python operators official doc
- Python Object Model (Python docs)
- Basic Customization
- Python basic operators on tutorialspoint
- Python comparison operators on data-flair
- PEP 207 to allow Operator Overloading for Comparison