Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Concept Documents] : comparisons concept #2456 #2733

Merged
merged 47 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
9d913b8
Add blurb for Comparison concept
gurupratap-matharu Nov 8, 2021
a20072e
Add introduction for Comparisons concept
gurupratap-matharu Nov 8, 2021
aadeb28
Add documentation for about.md for Comparisons concept on python track
gurupratap-matharu Nov 8, 2021
30bd1f0
Add links to resources for comparisons concept in python track
gurupratap-matharu Nov 8, 2021
2ec216a
Update concepts/comparisons/.meta/config.json
gurupratap-matharu Nov 12, 2021
d9c24aa
Update concepts/comparisons/.meta/config.json
gurupratap-matharu Nov 12, 2021
afe1bb0
Update concepts/comparisons/.meta/config.json
gurupratap-matharu Nov 12, 2021
f7d4566
Update concepts/comparisons/about.md
gurupratap-matharu Nov 12, 2021
759d03c
Update concepts/comparisons/links.json
gurupratap-matharu Nov 12, 2021
f4a5de2
Update concepts/comparisons/links.json
gurupratap-matharu Nov 12, 2021
c9b47b9
Update concepts/comparisons/introduction.md
gurupratap-matharu Nov 12, 2021
4a43927
Update description for official python operators
gurupratap-matharu Nov 12, 2021
0c5a5a9
Add note about short circuiting in comparison chaining
gurupratap-matharu Nov 12, 2021
e5883da
changed unpacking declaration to simple declaration
gurupratap-matharu Nov 12, 2021
3955479
Update concepts/comparisons/about.md
gurupratap-matharu Nov 12, 2021
9257582
Update example of membership test with better variable names
gurupratap-matharu Nov 12, 2021
3c983ff
Changed double quotes to single ones for consistency
gurupratap-matharu Nov 12, 2021
706a158
Update concepts/comparisons/about.md
gurupratap-matharu Nov 12, 2021
a302c3e
Update concepts/comparisons/links.json
gurupratap-matharu Nov 12, 2021
76d63ad
Add comparisons link at the bottom of the document
gurupratap-matharu Nov 14, 2021
10a104e
Add note for comparison priority
gurupratap-matharu Nov 14, 2021
ce782eb
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
a41cac4
Add link for arithmetic conversions
gurupratap-matharu Nov 14, 2021
5e540fd
Add example for complex number comparison
gurupratap-matharu Nov 14, 2021
ccd4f5e
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
c1ababc
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
1476cbf
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
ca3d0a3
Add period and convert statement to h3
gurupratap-matharu Nov 14, 2021
e812e82
Replace single letter variable with more verbose ones
gurupratap-matharu Nov 14, 2021
8ba2822
Update example of membership test
gurupratap-matharu Nov 14, 2021
11530ea
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
4985beb
Minor apostrophe update
gurupratap-matharu Nov 14, 2021
61cb7fd
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
dd6cc57
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
1cfe9b0
Update concepts/comparisons/introduction.md
gurupratap-matharu Nov 14, 2021
17dd5bc
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
f936928
Add chinese and korean strings for comparison
gurupratap-matharu Nov 14, 2021
92d3511
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
eed9c6a
Update concepts/comparisons/about.md
gurupratap-matharu Nov 14, 2021
2321489
Fix broken link due to mistyped word
gurupratap-matharu Nov 14, 2021
a5306de
Remove code samples and change prose as per about.md
gurupratap-matharu Nov 14, 2021
a2a5ae0
Update concepts/comparisons/.meta/config.json
BethanyG Nov 14, 2021
11acd58
Apply suggestions from code review
BethanyG Nov 14, 2021
aa18a17
Change examples of chinese and korean words
gurupratap-matharu Nov 15, 2021
a22047e
Mention Chinese and Korean in title case
gurupratap-matharu Nov 15, 2021
402295d
Update concepts/comparisons/about.md
J08K Nov 15, 2021
a41b825
Update concepts/comparisons/about.md
J08K Nov 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions concepts/comparisons/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"blurb": "TODO: add blurb for this concept",
"authors": ["bethanyg", "cmccandless"],
"contributors": []
"blurb": "Comparison operators evaluate two operand values, returning True or False based on whether the comparison condition is met.",
"authors": ["gurupratap-matharu"],
"contributors": ["bethanyg"]
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
}
154 changes: 153 additions & 1 deletion concepts/comparisons/about.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,154 @@
#TODO: Add about for this concept.
# About

A comparison operator in python, also called python relational operator, compares the values of two operands and returns `True` or `False` based on whether the condition is met. The most common comparison operators are `"<"`, `">"`, `"=="`, `">="`, `"<="`, `"!="`, `"is"`
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
BethanyG marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> 7 > 5
True
>>> 99 < 100
True
>>> 4 < 4
False
>>> 4 <= 4
True
>>> 1 >= 1
True
>>> 5 == 5
True
>>> 6 != 6 # not equal to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line-end comments should have two spaces before the #

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing the change. Did you forget to push it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed it. Check now

False
>>> 5 != 6
True
```

## 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`).
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

Also unlike `C`, expressions like `a < b < c` have the interpretation that is conventional in mathematics.
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
BethanyG marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> x, y, z = 2, 5, 10
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
>>> x < y < z
True
>>> x < y > z
False
>>> x > y < z
False
```

## Comparison of different data types
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

Since everything in `python` represents an `object` things start getting interesting when we compare objects of different types. For example, the `string` value of a number is considered a completely different value from the `integer` or `floating-point` version, an `integer` can be equal to a `floating point`.
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> 17 == '17'
False
>>> 17 == 17.0
True
>>> 17.0 == 0017.000
True
```

Python makes this distinction because strings are text, while `integers` and `floats` are both numbers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integers is not a value type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean we should int there? Sorry I didn't get you here.

gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

## Value Comparison

The operators `<`, `>`, `==`, `>=`, `<=`, and `!=` compare the `values` of two objects. The objects do not need to have the same type. Remember that in python
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
every object has a `value` in addition to `type` and `identity`
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

Following are comparison behaviour of most `built-ins` types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing period. Should this be a header?

Copy link
Contributor Author

@gurupratap-matharu gurupratap-matharu Nov 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the period. I felt its a sub-heading so I gave it an h3 tag. What do you think?


Numbers of built-in numeric types like `int`, `float` and of the standard library types `fractions.Fraction` and `decimal.Decimal` can be compared within and across their types.
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

Any ordered comparison of a number to a not-a-number value is `False`. A counter-intuitive implication is that not-a-number values are not equal to themselves.
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> x = float('NaN')
>>> x
nan
>>> 3 < x
False
>>> x < 3
False
>>> x == x
False
```

`Strings` compare lexicographically using the numerical Unicode code points (the result of the built-in function ord()) of their characters. `Strings` and `binary` sequences cannot be directly compared.
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> 'santa' < 'claus'
False
>>> 'Santa' < 'claus'
True
>>> ord('s')
115
>>> ord('S')
83
>>> ord('c')
99
```

Collections like `list`, `set`, `tuple` and `dict` can be compared too if they are of the same type, have the same length, and each pair of corresponding elements must compare equal.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Collections like `list`, `set`, `tuple` and `dict` can be compared too if they are of the same type, have the same length, and each pair of corresponding elements must compare equal.
Collections like `list`, `set`, `tuple` and `dict` can be compared if they are of the same type, have the same length.
They are equal if each pair of corresponding elements is equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the suggested change by Bethany below for this note. Please check and let me know if it works

gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> [1,2] == [1, 2]
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The operators `is` and `is not` test for an objects identity. An Object’s identity is determined using the `id()` function.
The operators `is` and `is not` test for an object's identity. An object's identity is determined using the `id()` function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice pick. This has been updated. Please check and close

gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

`x is y` is `True` if and only if `x` and `y` are the same object. `x` is not y` yields the inverse truth value.
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`x is y` is `True` if and only if `x` and `y` are the same object. `x` is not y` yields the inverse truth value.
`spam is ham` evaluates `True` if **and only if** `spam` and `ham` are the **_same object_**. `spam is not ham` yields the inverse truth value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try not to use any single letter variable names -- not even in examples. Happy for you to choose something other than spam or ham -- it just needs to be something other than x, y, a, et. al.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I changed it to apple and orange ☺️ Does that work?


```python
>>> x = [1,2,3]
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
>>> y = x
>>> x == y
True
>>> id(x)
4462635008
>>> id(y)
4462635008
>>> x is not y
False
```
BethanyG marked this conversation as resolved.
Show resolved Hide resolved

## Membership test operations

The operators `in` and `not in` test for membership. `a` in `s` evaluates to `True` if `a` is a member of s, and `False` otherwise. `a not in s` returns the negation of `a in s`.
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
J08K marked this conversation as resolved.
Show resolved Hide resolved

For the string and bytes types, `a` in `s` is `True` if and only if `a` is a substring of `b`.
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we want to avoid single-letter variable names.


```python
s = {11, 22, 33}
>>> 22 in s
True
>>> 44 in s
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"
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
>>> "Bat" in name
True
>>> "Batwoman" in name
False
```
53 changes: 52 additions & 1 deletion concepts/comparisons/introduction.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
#TODO: Add introduction for this concept.
# Introduction

A comparison operator in python, also called python relational operator, compares the values of two operands and returns `True` or `False` based on whether the condition is met. The most common comparison operators are `"<"`, `">"`, `"=="`, `">="`, `"<="`, `"!="`, `"is"`
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved

```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
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
```

## 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`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides omitting the code examples, I think I would make the same edits to the prose here as I did in the about.md.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sounds good.

Now

  • code blocks from introduction.md have been removed
  • prose is matched as per about.md


Also unlike `C`, expressions like `a < b < c` have the interpretation that is conventional in mathematics.

```python
>>> x, y, z = 2, 5, 10
>>> x < y < z
True
>>> x < y > z
False
>>> x > y < z
False
```

## Comparison of different data types

Since everything in `python` represents an `object` things start getting interesting when we compare objects of different types. For example, the `string` value of a number is considered a completely different value from the `integer` or `floating-point` version, an `integer` can be equal to a `floating point`.

```python
>>> 17 == '17'
False
>>> 17 == 17.0
True
>>> 17.0 == 0017.000
True
```

Python makes this distinction because strings are text, while `integers` and `floats` are both numbers.
52 changes: 44 additions & 8 deletions concepts/comparisons/links.json
Original file line number Diff line number Diff line change
@@ -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 tutorialspoint"
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
},
{
"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"
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
},
{
"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": "Python operators official doc"
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
},
{
"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"
gurupratap-matharu marked this conversation as resolved.
Show resolved Hide resolved
},
{
"url": "https://docs.python.org/3/reference/expressions.html#value-comparisons",
"description": "Value comparisons in Python (Python language reference)"
}
]