From 9a7cde3a8d44e94d4fe1dae4a48bc284333d7ac8 Mon Sep 17 00:00:00 2001 From: Gurupratap Matharu Date: Mon, 15 Nov 2021 21:28:20 +0530 Subject: [PATCH] [New Concept Documents] : comparisons concept #2456 (#2733) * Add blurb for Comparison concept * Add introduction for Comparisons concept * Add documentation for about.md for Comparisons concept on python track * Add links to resources for comparisons concept in python track * Update concepts/comparisons/.meta/config.json Co-authored-by: BethanyG * Update concepts/comparisons/.meta/config.json Co-authored-by: BethanyG * Update concepts/comparisons/.meta/config.json Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/links.json Co-authored-by: Isaac Good * Update concepts/comparisons/links.json Co-authored-by: Isaac Good * Update concepts/comparisons/introduction.md Co-authored-by: BethanyG * Update description for official python operators * Add note about short circuiting in comparison chaining * changed unpacking declaration to simple declaration * Update concepts/comparisons/about.md Co-authored-by: Isaac Good * Update example of membership test with better variable names * Changed double quotes to single ones for consistency * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/links.json Co-authored-by: Isaac Good * Add comparisons link at the bottom of the document * Add note for comparison priority * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Add link for arithmetic conversions * Add example for complex number comparison * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: Isaac Good * Add period and convert statement to h3 * Replace single letter variable with more verbose ones * Update example of membership test * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Minor apostrophe update * Update concepts/comparisons/about.md Co-authored-by: Isaac Good * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/introduction.md Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Add chinese and korean strings for comparison * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Fix broken link due to mistyped word * Remove code samples and change prose as per about.md * Update concepts/comparisons/.meta/config.json * Apply suggestions from code review Markdown style standards: one-sentence-per-line changes. Co-authored-by: Victor Goff * Change examples of chinese and korean words * Mention Chinese and Korean in title case * Update concepts/comparisons/about.md Co-authored-by: BethanyG * Update concepts/comparisons/about.md Co-authored-by: BethanyG Co-authored-by: BethanyG Co-authored-by: Isaac Good Co-authored-by: Victor Goff Co-authored-by: Job van der Wal <48634934+J08K@users.noreply.github.com> --- concepts/comparisons/.meta/config.json | 4 +- concepts/comparisons/about.md | 199 ++++++++++++++++++++++++- concepts/comparisons/introduction.md | 23 ++- concepts/comparisons/links.json | 52 ++++++- 4 files changed, 266 insertions(+), 12 deletions(-) diff --git a/concepts/comparisons/.meta/config.json b/concepts/comparisons/.meta/config.json index 9b9e8da5a9..67db625c51 100644 --- a/concepts/comparisons/.meta/config.json +++ b/concepts/comparisons/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "TODO: add blurb for this concept", - "authors": ["bethanyg", "cmccandless"], + "blurb": "Comparison operators evaluate two operand values, returning True or False based on whether the comparison condition is met.", + "authors": ["gurupratap-matharu", "bethanyg"], "contributors": [] } diff --git a/concepts/comparisons/about.md b/concepts/comparisons/about.md index c628150d56..3c43a38ec1 100644 --- a/concepts/comparisons/about.md +++ b/concepts/comparisons/about.md @@ -1,2 +1,199 @@ -#TODO: Add about for this concept. +# About +A [comparison operator][comparisons] in Python (_also called a Python relational operator_), looks at the values of two operands and returns `True` or `False` based on whether the `comparison` condition is met. +The most common comparison operators are `"<"`, `">"`, `"=="`, `">="`, `"<="`, and `"!="`. +They all have the same priority (which is higher than that of the Boolean operations). + +```python +>>> 7 > 5 +True +>>> 99 < 100 +True +>>> 4 < 4 +False +>>> 4 <= 4 +True +>>> 1 >= 1 +True +>>> 5 == 5 +True +>>> 6 != 6 # not equal to +False +>>> 5 != 6 +True +>>> not 3 == 3 # interpreted as not(3 == 3) +False +``` + +## Comparison Chaining + +Comparisons can be chained arbitrarily, e.g., `x < y <= z` is equivalent to `x < y` `and` `y <= z`, except that `y` is evaluated only once (but in both cases `z` is _not_ evaluated at all when `x < y` is found to be `False`). +This is also called `short-circuit` evaluation which means the execution is stopped if the truth value of the expression has already been determined. +Note that the evaluation of expression takes place from left to right. +In python, short circuiting is supported by various boolean operators, functions and, in this case, comparison chaining. + +Also unlike `C`, expressions like `a < b < c` have the interpretation that is conventional in mathematics. + +```python +>>> x = 2 +>>> y = 5 +>>> z = 10 +>>> x < y < z +True +>>> x < y > z +False +>>> x > y < z +False +``` + +## Comparison between different data types + +Since everything in Python is an `object`, things can get interesting when objects of different types are compared. +For example, the `str` value of a number is considered completely different from the `integer` or `floating-point` value. +Python makes this distinction because strings are text, while `integers` and `floats` are numeric types. + +However, an `integer` **can** be considered equal to a `float`, as they are both numeric types that Python can implicitly convert to compare. + +For other numeric types, comparison operators are defined where they "make sense", but throw a `TypeError` if the underlying objects cannot be converted for comparison. +For more information on the rules that python uses for numeric conversion, see [arithmetic conversions][arithmetic conversions] in the Python documentation. + +```python +>>> 17 == '17' +False +>>> 17 == 17.0 +True +>>> 17.0 == 0017.000 +True +>>> complex(1, 2) == complex(1.0, 2.00) +True +``` + + +## Value Comparison + +The operators `<`, `>`, `==`, `>=`, `<=`, and `!=` compare the _values_ of two different objects. +The objects do not need to have the same type. +Remember that in Python every object has a `value` in addition to `type` and `identity`. + +### Following are comparison behaviour of most `built-ins` types. + +Numbers of built-in _numeric_ types such as `int`, `hex`, `ocal`, `binary`, `float`, `complex` and of the standard library types `fractions.Fraction` and `decimal.Decimal` can be compared within and across their types. + +Any ordered comparison of a number to a `NaN` (_not a number_) value is `False`. +A counter-intuitive implication is that `NaN` never compares equal to `NaN`. + +```python +>>> x = float('NaN') +>>> x +nan +>>> 3 < x +False +>>> x < 3 +False +>>> x == x +False +``` + +Strings (`str`) are compared _lexicographically_ using their individual numerical Unicode code points (_the result of passing each code point in the `str` to the built-in function `ord()`_). +`str` and `binary` sequences cannot be directly compared. + +```python +>>> 'santa' < 'claus' +False +>>> 'Santa' < 'claus' +True +>>> ord('s') +115 +>>> ord('S') +83 +>>> ord('c') +99 +>>> # let's try Chinese words +>>> '你好' < '再见' # hello < goodbye +True +>>> # check ord() of first letters +>>> ord('你'), ord('再') +(20320, 20877) +>>> +>>> +>>> # let's try Korean words +>>> '예쁜' < '아름다운' # pretty < beautiful +False +>>> ord('예'), ord('아') +(50696, 50500) +``` + +Collections like `list`, `set`, `tuple` and `dict` can also be compared -- provided they are of the same `type`, have the same length, and each _**pair**_ of corresponding elements within the collection are comparable. + +```python +>>> [1, 2] == [1, 2] +True +>>> (1, 2) == [1, 2] +False +>>> [1, 2] < [1, 2, 3] +True +>>> # comparison of dicts +>>> {'name': 'John', 'age': 19} == {'name': 'John', 'age': 18} +False +>>> {'name': 'John', 'age': 19} == {'name': 'John', 'age': 19} +True +``` + +## Identity comparisons + +The operators `is` and `is not` test for an object's _identity_. +An object's identity is determined using the `id()` function. + +`apple is orange` is `True` if and only if `apple` and `orange` are the same object. +`apple is not orange` yields the inverse truth value. + +```python +>>> my_fav_numbers = [1, 2, 3] +>>> your_fav_numbers = my_fav_numbers +>>> my_fav_numbers == your_fav_numbers +True +>>> id(my_fav_numbers) +4462635008 +>>> id(your_fav_numbers) +4462635008 +>>> my_fav_numbers is not your_fav_numbers +False +``` + +## Membership test operations + +The operators `in` and `not in` test for _membership_. +`fish in soup` evaluates to `True` if `fish` is a member of `soup`, and evaluates `False` otherwise. +`fish not in soup` returns the negation, or _opposite of_ `fish in soup`. + +For the string and bytes types, `name` in `fullname` is `True` if and only if `name` is a substring of `fullname`. + +```python +lucky_numbers = {11, 22, 33} +>>> 22 in lucky_numbers +True +>>> 44 in lucky_numbers +False +>>> +>>> employee = { + 'name': 'John Doe', + 'id': 67826, + 'age': 33, + 'title': 'ceo' + } +>>> 'age' in employee +True +>>> 33 in employee +False +>>> 'lastname' not in employee +True +>>> +>>> name = 'Super Batman' +>>> 'Bat' in name +True +>>> 'Batwoman' in name +False +``` + +[comparisons]: https://docs.python.org/3/library/stdtypes.html?highlight=comparisons#comparisons +[arithmetic conversions]: https://docs.python.org/3/reference/expressions.html?highlight=number%20conversion#arithmetic-conversions diff --git a/concepts/comparisons/introduction.md b/concepts/comparisons/introduction.md index fcde74642c..27a105c12b 100644 --- a/concepts/comparisons/introduction.md +++ b/concepts/comparisons/introduction.md @@ -1,2 +1,23 @@ -#TODO: Add introduction for this concept. +# Introduction +A [comparison operator][comparisons] in Python (_also called a Python relational operator_), looks at the values of two operands and returns `True` or `False` based on whether the `comparison` condition is met. +The most common comparison operators are `"<"`, `">"`, `"=="`, `">="`, `"<="`, and `"!="`. +They all have the same priority (which is higher than that of the Boolean operations) + +## Comparison Chaining + +Comparisons can be chained arbitrarily, e.g., `x < y <= z` is equivalent to `x < y` `and` `y <= z`, except that `y` is evaluated only once (but in both cases `z` is _not_ evaluated at all when `x < y` is found to be `False`). +This is also called `short-circuit` evaluation which means the execution is stopped if the truth value of the expression has already been determined. +Note that the evaluation of expression takes place from left to right. +In python, short circuiting is supported by various boolean operators, functions and, in this case, comparison chaining. + +## Comparison of different data types + +Since everything in Python is an `object`, things can get interesting when objects of different types are compared. +For example, the `str` value of a number is considered completely different from the `integer` or `floating-point` value. +However, an `integer` **can** be considered equal to a `float`, as they are both numeric types that Python can implicitly convert to compare. +For other numeric types, comparison operators are defined where they "make sense", but throw a `TypeError` if the underlying objects cannot be converted for comparison. +For more information on the rules that python uses for numeric conversion, see [arithmetic conversions][arithmetic conversions] in the Python documentation. + +[comparisons]: https://docs.python.org/3/library/stdtypes.html? +[arithmetic conversions]: https://docs.python.org/3/reference/expressions.html?highlight=number%20conversion#arithmetic-conversions diff --git a/concepts/comparisons/links.json b/concepts/comparisons/links.json index eb5fb7c38a..ed61054b72 100644 --- a/concepts/comparisons/links.json +++ b/concepts/comparisons/links.json @@ -1,18 +1,54 @@ [ { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/reference/expressions.html#comparisons", + "description": "Comparisons in Python (Python language reference)" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://www.tutorialspoint.com/python/python_basic_operators.htm", + "description": "Python basic operators on Tutorials Point" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://data-flair.training/blogs/python-comparison-operators/", + "description": "Python comparison operators on Data Flair" }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://www.python.org/dev/peps/pep-0207/", + "description": "PEP 207 to allow Operator Overloading for Comparison" + }, + { + "url": "https://docs.python.org/3/reference/expressions.html#is-not", + "description": "Identity comparisons in Python (Python language reference)" + }, + { + "url": "https://docs.python.org/3/library/operator.html", + "description": "Operators (Python Docs)" + }, + { + "url": "https://docs.python.org/3/library/stdtypes.html#typesnumeric", + "description": "Numeric types (Python Docs)" + }, + { + "url": "https://docs.python.org/3/library/decimal.html#decimal.Decimal", + "description": "Decimal types (Python Docs)" + }, + { + "url": "https://docs.python.org/3/library/fractions.html#fractions.Fraction", + "description": "Fractions (Python Docs)" + }, + { + "url": "https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range", + "description": "Sequence types (Python Docs)" + }, + { + "url": "https://docs.python.org/3/reference/datamodel.html#objects", + "description": "Python Object Model (Python docs)" + }, + { + "url": "https://docs.python.org/3/reference/datamodel.html#customization", + "description": "Basic Customization (Python language reference)" + }, + { + "url": "https://docs.python.org/3/reference/expressions.html#value-comparisons", + "description": "Value comparisons in Python (Python language reference)" } ]