Description
This issue describes how to implement the comparisons
concept exercise for the Python track.
Getting started
Please please please read the docs before starting. Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism's maintainers' time. So, before diving into the implementation, please read up on the following documents:
Please also watch the following video:
Goal
This concept exercise should teach how basic (non-customized) comparisons work in python and how to use them effectively.
Learning objectives
- 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
valuesTrue
andFalse
- know that identity comparisons
is
andis 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) andNaN
(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
Out of scope
- 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
Concepts
- Comparison priority in Python
- Comparison operators
==
,>
,<
,!=
- Identity methods
is
andis not
- Equality applied to
built-in
types - Equivalence vs equality
- Inequality
Prerequisites
basics
bools
dicts
lists
sets
strings
tuples
numbers
iteration
Resources to refer to
- 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
Hints
- Referring to one or more of the resources linked above, or analogous resources from a trusted source.
Concept Description
(a variant of this can be used for the v3/languages/python/concepts/<concept>/about.md
doc and this exercises introduction.md
doc.)
The concept description needs to be filled in here.
Representer
No changes required.
Analyzer
No changes required.
Implementing
Tests should be written using unittest.TestCase and the test file named comparisons_test.py.
Code in the .meta/example.py
file should only use syntax & concepts introduced in this exercise or one of its prerequisites.
Please do not use comprehensions, generator expressions, or other syntax not previously covered. Please also follow PEP8 guidelines.
Help
If you have any questions while implementing the exercise, please post the questions as comments in this issue.