From d80ad9a6c19fc45c7dc6873acdffaa22653f26f9 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 23 Jan 2022 16:02:53 -0400 Subject: [PATCH 1/3] Updating the the concept introduction for numbers Attempting to clarify the difference between an int and a float by defining precision. Adding a section outlining the use of underscores in numeric literals. --- concepts/numbers/introduction.md | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index e9fa438f714..c602796b99f 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -1,7 +1,5 @@ # Introduction -TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction - ## Numbers There are three different kinds of built-in numbers in Python : `ints`, `floats`, and `complex`. However, in this exercise you'll be dealing only with `ints` and `floats`. @@ -14,7 +12,7 @@ Integers in Python have [arbitrary precision][arbitrary-precision] -- the amount ### floats -`floats` are numbers containing a decimal point. e.g. `0.0`,`3.14`,`-9.01`. +`floats` or `floating point numbers` contain a decimal point. e.g. `0.0`,`3.14`,`-9.01`. Floating point numbers are usually implemented in Python using a `double` in C (_15 decimal places of precision_), but will vary in representation based on the host system and other implementation details. This can create some surprises when working with floats, but is "good enough" for most situations. @@ -26,9 +24,13 @@ You can see more details and discussions in the following resources: - [Documentation for `float()` built in][`float()` built in] - [0.30000000000000004.com][0.30000000000000004.com] +### Precision + +Before diving into arithmetic, it is worth thinking about what precision means. Precision is the level of exactness that a number can be represented. An `int` is less precise than a `float` the same way that `1` is less precise that`1.125`. + ## Arithmetic -Python fully supports arithmetic between `ints` and `floats`. It will convert narrower numbers to match their less narrow counterparts when used with the binary arithmetic operators (`+`, `-`, `*`, `/`, `//`, and `%`). When division with `/`, `//` returns the quotient and `%` returns the remainder. +Python fully supports arithmetic between `ints` and `floats`. It will convert narrower numbers to match their wider (or more precise) counterparts when used with the binary arithmetic operators (`+`, `-`, `*`, `/`, `//`, and `%`). When division with `/`, `//` returns the quotient and `%` returns the remainder. Python considers `ints` narrower than `floats`. So, using a float in an expression ensures the result will be a float too. However, when doing division, the result will always be a float, even if only integers are used. @@ -72,9 +74,25 @@ To convert a float to an integer, you can use `int()`. Also, to convert an integ 3.0 ``` +## Underscores in Numeric Literals + +As of Python 3.6, python supports the use of underscores in numerical literals to improve readability: +```python +# A float with underscores +>>> dollars = 35_000_000.0 +>>> print(dollars) +35000000.0 +``` + +Rules for using underscores as outlined in [pep 515][pep 515] are as follows: +* Only one consecutive underscore allowed, and only between digits. +* Multiple consecutive underscores allowed, but only between digits. +* Multiple consecutive underscores allowed, in most positions except for the start of the literal, or special positions like after a decimal point. + [arbitrary-precision]: https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#:~:text=In%20computer%20science%2C%20arbitrary%2Dprecision,memory%20of%20the%20host%20system. [numeric-type-docs]: https://docs.python.org/3/library/stdtypes.html#typesnumeric [`int()` built in]: https://docs.python.org/3/library/functions.html#int [`float()` built in]: https://docs.python.org/3/library/functions.html#float [0.30000000000000004.com]: https://0.30000000000000004.com/ [floating point math]: https://docs.python.org/3.9/tutorial/floatingpoint.html +[pep 515]: https://www.python.org/dev/peps/pep-0515/ \ No newline at end of file From d6c356a8f46002bb3cfe9b49010f95282edf1305 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 25 Jan 2022 08:41:21 -0400 Subject: [PATCH 2/3] Update concepts/numbers/introduction.md applying grammar suggestion Co-authored-by: BethanyG --- concepts/numbers/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index c602796b99f..1faaac42b26 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -26,7 +26,7 @@ You can see more details and discussions in the following resources: ### Precision -Before diving into arithmetic, it is worth thinking about what precision means. Precision is the level of exactness that a number can be represented. An `int` is less precise than a `float` the same way that `1` is less precise that`1.125`. +Before diving into arithmetic, it is worth thinking about what precision means. Precision is the level of exactness at which a number can be represented. An `int` is less precise than a `float` the same way that `1` is less precise that`1.125`. ## Arithmetic From f286d47ec7cb699bfecc9d04373622999a184ba8 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 25 Jan 2022 08:41:42 -0400 Subject: [PATCH 3/3] Update concepts/numbers/introduction.md applying grammar suggestion Co-authored-by: BethanyG --- concepts/numbers/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index 1faaac42b26..b35150dbf3f 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -76,7 +76,7 @@ To convert a float to an integer, you can use `int()`. Also, to convert an integ ## Underscores in Numeric Literals -As of Python 3.6, python supports the use of underscores in numerical literals to improve readability: +As of version 3.6, Python supports the use of underscores in numerical literals to improve readability: ```python # A float with underscores >>> dollars = 35_000_000.0