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 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 certain built-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) and built-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 and is 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 generic object 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 and is 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
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.
This issue describes how to implement the
comparisonsconcept 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
booleanvaluesTrueandFalseisandis notare for checking an objects identity only==and!=compare both the value & type of an object.==and!=for certainbuilt-intypes (such as numbers), or for standard library types like decimals, and fractions to allow comparison across and within type.str) and binary sequences (bytes&byte array) cannot be directly compared.built-insequence types (list,tuple,range) andbuilt-incollection types (set,dict)None,NotImplemented(comparing either should use identity operators and not equality operators because they are singleton objects) andNaN(NaN is never==to itself)==,>,<,!=with numeric types==,>,<,!=with non-numeric typesisandis notto check/verify identityOut of scope
__lt__,__le__,__ne__,__ge__,__gt__==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 genericobjectis used.__eq__()dunder method on a specific object to customize comparison behavior.set operationsConcepts
==,>,<,!=isandis notbuilt-intypesPrerequisites
basicsboolsdictslistssetsstringstuplesnumbersiterationResources to refer to
Hints
Concept Description
(a variant of this can be used for the
v3/languages/python/concepts/<concept>/about.mddoc and this exercisesintroduction.mddoc.)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.pyfile 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.