From da74cc06e983b26e01296c49cc6a025b7055e31b Mon Sep 17 00:00:00 2001 From: Dave Whipp Date: Tue, 13 Aug 2024 19:08:18 +0300 Subject: [PATCH] Minor formatting edits and new glossary entries --- source/back-matter/md/glossary.md | 9 +++- source/back-matter/nb/glossary.ipynb | 12 ++++- .../part1/chapter-02/md/00-python-basics.md | 46 +++++++++---------- .../chapter-02/nb/00-python-basics.ipynb | 44 +++++++++--------- 4 files changed, 64 insertions(+), 47 deletions(-) diff --git a/source/back-matter/md/glossary.md b/source/back-matter/md/glossary.md index 875d6ee1..3059d618 100644 --- a/source/back-matter/md/glossary.md +++ b/source/back-matter/md/glossary.md @@ -5,7 +5,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.3' - jupytext_version: 1.15.2 + jupytext_version: 1.16.4 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -192,6 +192,9 @@ Topological spatial relations Tuple A [tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) is a Python data structure that consists of a number of values separated by commas. Coordinate pairs are often represented as a tuple, such as: `(60.192059, 24.945831)`. Tuples belong to [sequence data types](https://docs.python.org/3/library/stdtypes.html#typesseq) in Python. Other sequence data types are lists and ranges. Tuples have many similarities with lists and ranges, but they are often used for different purposes. The main difference between tuples and lists is that tuples are [immutable](https://docs.python.org/3/glossary.html#term-immutable), which means that the contents of a tuple cannot be altered (while lists are mutable; you can, for example, add and remove values from lists). +Type conversion + Changing the data type of a Python object. For example, `float(5)`. + Unary union The unary union operation takes multiple geometries and merges them into a single geometry. This is useful when you have a collection of shapes and you want to treat them as a single entity for analysis or visualization. @@ -207,3 +210,7 @@ Well-known binary Well-known text Well-known text (WKT) is a text markup language for representing vector geometry objects. WKT can represent various geometric objects: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, Triangle, PolyhedralSurface, TIN (Triangulated irregular network) and GeometryCollection. Coordinates for the geometries can be represented in 2D, 3D or 4D (x,y,z,m). The binary equivalent for WKT is `Well-known binary` format. ``` + +```python + +``` diff --git a/source/back-matter/nb/glossary.ipynb b/source/back-matter/nb/glossary.ipynb index 7c6632da..c2b4069b 100644 --- a/source/back-matter/nb/glossary.ipynb +++ b/source/back-matter/nb/glossary.ipynb @@ -184,6 +184,9 @@ "Tuple\n", " A [tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) is a Python data structure that consists of a number of values separated by commas. Coordinate pairs are often represented as a tuple, such as: `(60.192059, 24.945831)`. Tuples belong to [sequence data types](https://docs.python.org/3/library/stdtypes.html#typesseq) in Python. Other sequence data types are lists and ranges. Tuples have many similarities with lists and ranges, but they are often used for different purposes. The main difference between tuples and lists is that tuples are [immutable](https://docs.python.org/3/glossary.html#term-immutable), which means that the contents of a tuple cannot be altered (while lists are mutable; you can, for example, add and remove values from lists).\n", "\n", + "Type conversion\n", + " Changing the data type of a Python object. For example, `float(5)`.\n", + "\n", "Unary union\n", " The unary union operation takes multiple geometries and merges them into a single geometry. This is useful when you have a collection of shapes and you want to treat them as a single entity for analysis or visualization.\n", "\n", @@ -200,6 +203,13 @@ " Well-known text (WKT) is a text markup language for representing vector geometry objects. WKT can represent various geometric objects: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, Triangle, PolyhedralSurface, TIN (Triangulated irregular network) and GeometryCollection. Coordinates for the geometries can be represented in 2D, 3D or 4D (x,y,z,m). The binary equivalent for WKT is `Well-known binary` format.\n", "```" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -218,7 +228,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.12.5" } }, "nbformat": 4, diff --git a/source/part1/chapter-02/md/00-python-basics.md b/source/part1/chapter-02/md/00-python-basics.md index 33d84473..86aea19c 100644 --- a/source/part1/chapter-02/md/00-python-basics.md +++ b/source/part1/chapter-02/md/00-python-basics.md @@ -5,7 +5,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.3' - jupytext_version: 1.15.2 + jupytext_version: 1.16.4 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -498,7 +498,7 @@ Note that although it is possible to have different types of data in a Python li ### Adding and removing values from lists -Finally, we can add and remove values from lists to change their lengths. Let’s consider that we no longer want to include the first value in the `station_names` list. Since we haven't see that list in a bit, let's first print it out. +Finally, we can add and remove values from lists to change their lengths. Let’s consider that we no longer want to include the first value in the `station_names` list. Since we have not seen that list in a bit, let's first print it out. ```python station_names @@ -520,7 +520,7 @@ In addition to the `del` statement, there are two other common approaches for re - `demo_list.pop(index)`: Will remove the item at index `index` from the list `demo_list` -If we would instead like to add a few samples to the `station_names` list, we can type `station_names.append('List item to add')`, where `'List item to add'` would be the text that would be added to the list in this example. Let's add two values to our list: `'Helsinki lighthouse'` and `'Helsinki Malmi airfield'` and check the list contents after this. +If we would instead like to add a few more stations to the `station_names` list, we can type `station_names.append('List item to add')`, where `'List item to add'` would be the text that would be added to the list in this example. Let's add two values to our list: `'Helsinki lighthouse'` and `'Helsinki Malmi airfield'` and check the list contents after this. ```python station_names.append("Helsinki lighthouse") @@ -536,7 +536,7 @@ As you can see, we add values one at a time using `station_names.append()`. `lis ### Appending to an integer? Not so fast... -Let’s consider our list `station_names`. As we know, we already have data in the list `station_names`, and we can modify that data using built-in methods such as `station_names.append()`. In this case, the method `append()` is something that exists for lists, but not for other data types. It is intuitive that you might like to add (or append) things to a list, but perhaps it does not make sense to append to other data types. Let's create a variable `station_name_length` that we can use to store the length of the list `station_names`. We can then print the value of `station_name_length` to confirm the length is correct. +Let’s consider our list `station_names`. As we know, we already have data in the list `station_names`, and we can modify that data using built-in methods such as `station_names.append()`. In this case, the method `.append()` is something that exists for lists, but not for other data types. It is intuitive that you might like to add (or append) things to a list, but perhaps it does not make sense to append to other data types. Let's create a variable `station_name_length` that we can use to store the length of the list `station_names`. We can then print the value of `station_name_length` to confirm the length is correct. ```python station_name_length = len(station_names) @@ -546,7 +546,7 @@ station_name_length = len(station_names) station_name_length ``` -If we check the data type of `station_name_length`, we can see it is an integer value, as expected. +If we check the data type of `station_name_length`, we can see it is an integer value as expected. ```python type(station_name_length) @@ -558,18 +558,18 @@ Let's see what happens if we try to append the value `1` to `station_name_length station_name_length.append(1) ``` -Here we get an `AttributeError` because there is no method built in to the `int` data type to append to `int` data. While `append()` makes sense for `list` data, it is not sensible for `int` data, which is the reason no such method exists for `int` data. +Here we get an `AttributeError` because there is no method built in to the `int` data type to append to `int` data. While `.append()` makes sense for `list` data, it is not sensible for `int` data, which is the reason no such method exists for `int` data. ### Some other useful list methods -With lists we can do a number of useful things, such as count the number of times a value occurs in a list or where it occurs. The `list.count()` method can be used to find the number of instances of an item in a list. For instance, we can check to see how many times `'Helsinki Kumpula'` occurs in our list `station_names` by typing `station_names.count('Helsinki Kumpula')`. +With lists we can do a number of useful things, such as count the number of times a value occurs in a list or where it occurs. The `.count()` method can be used to find the number of instances of an item in a list. For instance, we can check to see how many times `'Helsinki Kumpula'` occurs in our list `station_names` by typing `station_names.count('Helsinki Kumpula')`. ```python station_names.count("Helsinki Kumpula") ``` -Similarly, we can use the `list.index()` method to find the index value of a given item in a list. Let's find the index of `'Helsinki Kumpula'` in the `station_names` list. +Similarly, we can use the `.index()` method to find the index value of a given item in a list. Let's find the index of `'Helsinki Kumpula'` in the `station_names` list. ```python station_names.index("Helsinki Kumpula") @@ -582,7 +582,7 @@ There are two other common methods for lists that we need to see. ### Reversing a list -The `list.reverse()` method can be used to reverse the order of items in a list. Let's reverse our `station_names` list and then print the results. +The `.reverse()` method can be used to reverse the order of items in a list. Let's reverse our `station_names` list and then print the results. ```python station_names.reverse() @@ -597,7 +597,7 @@ Yay, it works! A common mistake when reversing lists is to do something like `st ### Sorting a list -The `list.sort()` method works the same way as reversing a list. Let's sort our `station_names` list and print its contents. +The `.sort()` method works the same way as reversing a list. Let's sort our `station_names` list and print its contents. ```python station_names.sort() # Notice no output here... @@ -607,7 +607,7 @@ station_names.sort() # Notice no output here... station_names ``` -As you can see, the list has been sorted alphabetically using the `list.sort()` method, but there is no screen output when this occurs. Again, if you were to assign that output to `station_names` the list would get sorted, but the contents would then be assigned `None`. And as you may have noticed, `Helsinki Malmi airfield` comes before `Helsinki lighthouse` in the sorted list. This is because alphabetical sorting in Python places capital letters before lowercase letters. +As you can see, the list has been sorted alphabetically using the `.sort()` method, but there is no screen output when this occurs. Again, if you were to assign that output to `station_names` the list would get sorted, but the contents would then be assigned `None`. And as you may have noticed, `Helsinki Malmi airfield` comes before `Helsinki lighthouse` in the sorted list. This is because alphabetical sorting in Python places capital letters before lowercase letters. ## Making different data types work together @@ -652,13 +652,13 @@ Here we get a `TypeError` because Python does not know to combine a string of ch ### Converting data from one type to another -It is not the case that things like the `station_name` and `station_id` cannot be combined at all, but in order to combine a character string with a number we need to perform a *data type conversion* to make them compatible. Let's convert `station_id` to a character string using the `str()` function. We can store the converted variable as `station_id_str`. +It is not the case that things like the `station_name` and `station_id` cannot be combined at all, but in order to combine a character string with a number we need to perform a data {term}`type conversion` to make them compatible. Let's convert `station_id` to a character string using the `str()` function. We can store the converted variable as `station_id_str`. ```python station_id_str = str(station_id) ``` -We can confirm the type has changed by checking the type of `station_id_str`, or by checking the output of a code cell with the variable. +We can confirm the type has changed by checking the type of `station_id_str` or by checking the output of a code cell with the variable. ```python type(station_id_str) @@ -719,15 +719,15 @@ Note that here we are converting `station_id` to a character string using the `s ## Formatting text (and numbers) -The previous example showed a simple example how it is possible to combine character strings and numbers together using the `+` operator between the different text components. Although this approach works, it can become quite laborous and error-prone if having more complicated set of textual and/or numerical components that you should work with. Hence, next we show a few useful techniques that make manipulating strings easier and more efficient. +The previous case showed a simple example of how it is possible to combine character strings and numbers together using the `+` operator between the different text components. Although this approach works, it can become quite laborious and error-prone when you have a more complicated set of textual and/or numerical components that you work with. Hence, next we show a few useful techniques that make manipulating strings easier and more efficient. -There are three approaches that can be used to manipulate strings in Python, namely 1) f-strings, 2) using the`.format()` function and 3) using the % operator. We recommed using the f-string approach, but we also give examples of the two other approaches because there are plenty of examples and code snippets on the web, where these string formatting approaches are still used. Hence, it is good to be aware of them all. In addition, we show a few useful methods that make working with text in different ways possible. +There are three approaches that can be used to manipulate strings in Python: (1) f-strings, (2) using the`.format()` method, and (3) using the % operator. We recommend using the f-string approach, but we also provide examples of the two other approaches because there are plenty of examples and code snippets on the web where these string formatting approaches are still used. Hence, it is good to be aware of them all. In addition, we show a few useful methods that make working with text in different ways possible. ### F-String formatting -In the following, we show how we can combine the `station_name` text, `station_id` integer number and `temp` floating point number together using Python's f-string formatting approach. In addition, we will round a decimal number (temperature) into two decimal points on-the-fly: +In the following, we show how we can combine the `station_name` text, the `station_id` integer number, and the `temp` floating point number together using Python's f-string formatting approach. In addition, we will round a decimal number (`temp`) to two decimal points on the fly. ```python editable=true slideshow={"slide_type": ""} @@ -747,16 +747,16 @@ print(info_text) _**Figure 2.2**. F-string formatting explained._ -As you can see, using string formatting it is possible to easily modify a body of text "interactively" based on values stored in given variables. Figure 2.2 breaks down the different parts of the string. The text that you want to create and/or modify is enclosed within the quotes preceded with letter `f`. You can pass any existing variable inside the text template by placing the name of the variable within the curly braces `{}`. Using string formatting, it is also possible to insert numbers (such as `station_id` and `temp` here) within the body of text without needing first to convert the data type to string. This is because the f-string functionality kindly does the data type conversion for us in the background without us needing to worry about it (handy!). +As you can see, using string formatting it is possible to easily modify a body of text "interactively" based on values stored in given variables. Figure 2.2 breaks down the different parts of the string. The text that you want to create and/or modify is enclosed within the quotes preceded with letter `f`. You can pass any existing variable inside the text template by placing the name of the variable within the curly braces `{}`. Using string formatting, it is also possible to insert numbers (such as `station_id` and `temp`) within the body of text without needing first to convert the data types to strings. This is because the f-string functionality kindly does the data type conversion for us in the background without us needing to worry about it (handy!). -It is also possible to round these numbers on-the-fly to specific precision, such as two decimal points as in our example by adding format specifier (`:.2f`) after the variable that we want to round. The format specifier works by first adding a colon (`:`) after the variable name, and then specifying with dot (`.`) that we want to round our value to 2 decimal places (can be any digits). The final character `f` in the format specifier defines the type of the conversion that will be conducted: character `f` will convert the value to decimal number, whereas character `e` would make the number to appear as exponential number while character `%` would convert the value to percentage representation. +It is also possible to round numbers on the fly to specific precision, such as the two decimal points in our example, by adding the format specifier (`:.2f`) after the variable that we want to round. The format specifier works by first adding a colon (`:`) after the variable name, and then specifying with dot (`.`) that we want to round our value to 2 decimal places (can be any number of digits). The final character `f` in the format specifier defines the type of the formatting that will be done: the character `f` will display the value as a decimal number, the character `e` would make the number appear in scientific notation, while the character `%` would convert the value to percentage representation. -As we have hopefully demonstrated, f-string formatting is easy to use, yet powerful with its capability to e.g. do data conversions on the fly. Hence, it is the recommended approach for doing string manipulation nowadays in Python. Just remember to add the letter `f` before your string template! +As we have hopefully demonstrated, f-string formatting is easy to use, yet powerful with its capability to do data conversions on the fly, for example. Hence, it is the recommended approach for doing string manipulation presently in Python. Just remember to add the letter `f` before your string template! ### Older approaches for string formatting -As mentioned previously, there are also a couple of other approaches that can be used to achieve the same result as above. These older approaches preceded the f-string which was introduced in Python version 3.6. The first one is `.format()` method that is placed after the quotes, like this: +As mentioned previously, there are also a couple of other approaches that can be used to achieve the same result as above. These older approaches preceded the f-string, which was introduced in Python version 3.6. The first one is the `.format()` method, which is placed after the string in quotes, like this: ```python editable=true slideshow={"slide_type": ""} @@ -769,9 +769,9 @@ text2 = ( print(text2) ``` -As you can see, here we got the same result as with f-strings, but used the `.format()` which was placed after the quotes. The variables were inserted within the text template by using curly braces and giving them a name (placeholder) which should have a matching counterpart within the parentheses that link to the actual variable which will be inserted to the body of text. As you see, the placeholder does not necessarily need to have the same name as the actual variable that contains the inserted value, but it can be anything, like the name `my_text_variable` as in the example above. +As you can see, here we get the same result as we did with an f-string, but we used the `.format()` placed after the quotes. The variables were inserted within the text template using curly braces and giving them a name (placeholder) which is expected to have a matching counterpart within the `.format()` parentheses that link to the variable value that will be inserted in the body of text. As you see, the placeholder does not necessarily need to have the same name as the actual variable that contains the inserted value, but it can be anything, like the name `my_text_variable` as in the example above. -The last (historical) string formatting approach is to use `%s` -operator. In this approach, the placeholder `%s` is added within the quotes, and the variables that are inserted into the body of text are placed inside parentheses after the `%` operator, like this: +The last (historical) string formatting approach is to use the `%s` operator. In this approach, the placeholder `%s` is added within the quotes, and the variables that are inserted into the body of text are placed inside parentheses after the `%` operator, like this: ```python editable=true slideshow={"slide_type": ""} # 3. The % operator approach (no longer recommended) @@ -787,7 +787,7 @@ To conclude, using the f-string approach is the easiest and most intuitive way t ## Manipulating character strings Here we demonstrate some of the most useful string manipulation techniques, such as splitting strings based on a given character, replacing characters with new ones, slicing strings, etc. -The aim is to produce a list of weather station locations in Helsinki that are represented in uppercase text (i.e., KUMPULA, KAISANIEMI, HARMAJA). The text that we will begin working with is following: +The aim is to produce a list of weather station locations in Helsinki that are represented in uppercase text (i.e., `KUMPULA, KAISANIEMI, HARMAJA`). The text that we will begin working with is below: ```python editable=true slideshow={"slide_type": ""} diff --git a/source/part1/chapter-02/nb/00-python-basics.ipynb b/source/part1/chapter-02/nb/00-python-basics.ipynb index e4803d31..c4dd6466 100644 --- a/source/part1/chapter-02/nb/00-python-basics.ipynb +++ b/source/part1/chapter-02/nb/00-python-basics.ipynb @@ -1665,7 +1665,7 @@ "source": [ "### Adding and removing values from lists\n", "\n", - "Finally, we can add and remove values from lists to change their lengths. Let’s consider that we no longer want to include the first value in the `station_names` list. Since we haven't see that list in a bit, let's first print it out." + "Finally, we can add and remove values from lists to change their lengths. Let’s consider that we no longer want to include the first value in the `station_names` list. Since we have not seen that list in a bit, let's first print it out." ] }, { @@ -1741,7 +1741,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If we would instead like to add a few samples to the `station_names` list, we can type `station_names.append('List item to add')`, where `'List item to add'` would be the text that would be added to the list in this example. Let's add two values to our list: `'Helsinki lighthouse'` and `'Helsinki Malmi airfield'` and check the list contents after this." + "If we would instead like to add a few more stations to the `station_names` list, we can type `station_names.append('List item to add')`, where `'List item to add'` would be the text that would be added to the list in this example. Let's add two values to our list: `'Helsinki lighthouse'` and `'Helsinki Malmi airfield'` and check the list contents after this." ] }, { @@ -1791,7 +1791,7 @@ "source": [ "### Appending to an integer? Not so fast...\n", "\n", - "Let’s consider our list `station_names`. As we know, we already have data in the list `station_names`, and we can modify that data using built-in methods such as `station_names.append()`. In this case, the method `append()` is something that exists for lists, but not for other data types. It is intuitive that you might like to add (or append) things to a list, but perhaps it does not make sense to append to other data types. Let's create a variable `station_name_length` that we can use to store the length of the list `station_names`. We can then print the value of `station_name_length` to confirm the length is correct." + "Let’s consider our list `station_names`. As we know, we already have data in the list `station_names`, and we can modify that data using built-in methods such as `station_names.append()`. In this case, the method `.append()` is something that exists for lists, but not for other data types. It is intuitive that you might like to add (or append) things to a list, but perhaps it does not make sense to append to other data types. Let's create a variable `station_name_length` that we can use to store the length of the list `station_names`. We can then print the value of `station_name_length` to confirm the length is correct." ] }, { @@ -1827,7 +1827,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If we check the data type of `station_name_length`, we can see it is an integer value, as expected." + "If we check the data type of `station_name_length`, we can see it is an integer value as expected." ] }, { @@ -1886,7 +1886,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Here we get an `AttributeError` because there is no method built in to the `int` data type to append to `int` data. While `append()` makes sense for `list` data, it is not sensible for `int` data, which is the reason no such method exists for `int` data." + "Here we get an `AttributeError` because there is no method built in to the `int` data type to append to `int` data. While `.append()` makes sense for `list` data, it is not sensible for `int` data, which is the reason no such method exists for `int` data." ] }, { @@ -1895,7 +1895,7 @@ "source": [ "### Some other useful list methods\n", "\n", - "With lists we can do a number of useful things, such as count the number of times a value occurs in a list or where it occurs. The `list.count()` method can be used to find the number of instances of an item in a list. For instance, we can check to see how many times `'Helsinki Kumpula'` occurs in our list `station_names` by typing `station_names.count('Helsinki Kumpula')`. " + "With lists we can do a number of useful things, such as count the number of times a value occurs in a list or where it occurs. The `.count()` method can be used to find the number of instances of an item in a list. For instance, we can check to see how many times `'Helsinki Kumpula'` occurs in our list `station_names` by typing `station_names.count('Helsinki Kumpula')`. " ] }, { @@ -1922,7 +1922,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly, we can use the `list.index()` method to find the index value of a given item in a list. Let's find the index of `'Helsinki Kumpula'` in the `station_names` list." + "Similarly, we can use the `.index()` method to find the index value of a given item in a list. Let's find the index of `'Helsinki Kumpula'` in the `station_names` list." ] }, { @@ -1960,7 +1960,7 @@ "source": [ "### Reversing a list\n", "\n", - "The `list.reverse()` method can be used to reverse the order of items in a list. Let's reverse our `station_names` list and then print the results." + "The `.reverse()` method can be used to reverse the order of items in a list. Let's reverse our `station_names` list and then print the results." ] }, { @@ -2009,7 +2009,7 @@ "source": [ "### Sorting a list\n", "\n", - "The `list.sort()` method works the same way as reversing a list. Let's sort our `station_names` list and print its contents." + "The `.sort()` method works the same way as reversing a list. Let's sort our `station_names` list and print its contents." ] }, { @@ -2049,7 +2049,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As you can see, the list has been sorted alphabetically using the `list.sort()` method, but there is no screen output when this occurs. Again, if you were to assign that output to `station_names` the list would get sorted, but the contents would then be assigned `None`. And as you may have noticed, `Helsinki Malmi airfield` comes before `Helsinki lighthouse` in the sorted list. This is because alphabetical sorting in Python places capital letters before lowercase letters." + "As you can see, the list has been sorted alphabetically using the `.sort()` method, but there is no screen output when this occurs. Again, if you were to assign that output to `station_names` the list would get sorted, but the contents would then be assigned `None`. And as you may have noticed, `Helsinki Malmi airfield` comes before `Helsinki lighthouse` in the sorted list. This is because alphabetical sorting in Python places capital letters before lowercase letters." ] }, { @@ -2189,7 +2189,7 @@ "source": [ "### Converting data from one type to another\n", "\n", - "It is not the case that things like the `station_name` and `station_id` cannot be combined at all, but in order to combine a character string with a number we need to perform a *data type conversion* to make them compatible. Let's convert `station_id` to a character string using the `str()` function. We can store the converted variable as `station_id_str`." + "It is not the case that things like the `station_name` and `station_id` cannot be combined at all, but in order to combine a character string with a number we need to perform a data {term}`type conversion` to make them compatible. Let's convert `station_id` to a character string using the `str()` function. We can store the converted variable as `station_id_str`." ] }, { @@ -2205,7 +2205,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can confirm the type has changed by checking the type of `station_id_str`, or by checking the output of a code cell with the variable." + "We can confirm the type has changed by checking the type of `station_id_str` or by checking the output of a code cell with the variable." ] }, { @@ -2436,9 +2436,9 @@ "source": [ "## Formatting text (and numbers)\n", "\n", - "The previous example showed a simple example how it is possible to combine character strings and numbers together using the `+` operator between the different text components. Although this approach works, it can become quite laborous and error-prone if having more complicated set of textual and/or numerical components that you should work with. Hence, next we show a few useful techniques that make manipulating strings easier and more efficient.\n", + "The previous case showed a simple example of how it is possible to combine character strings and numbers together using the `+` operator between the different text components. Although this approach works, it can become quite laborious and error-prone when you have a more complicated set of textual and/or numerical components that you work with. Hence, next we show a few useful techniques that make manipulating strings easier and more efficient.\n", "\n", - "There are three approaches that can be used to manipulate strings in Python, namely 1) f-strings, 2) using the`.format()` function and 3) using the % operator. We recommed using the f-string approach, but we also give examples of the two other approaches because there are plenty of examples and code snippets on the web, where these string formatting approaches are still used. Hence, it is good to be aware of them all. In addition, we show a few useful methods that make working with text in different ways possible." + "There are three approaches that can be used to manipulate strings in Python: (1) f-strings, (2) using the`.format()` method, and (3) using the % operator. We recommend using the f-string approach, but we also provide examples of the two other approaches because there are plenty of examples and code snippets on the web where these string formatting approaches are still used. Hence, it is good to be aware of them all. In addition, we show a few useful methods that make working with text in different ways possible." ] }, { @@ -2453,7 +2453,7 @@ "source": [ "### F-String formatting\n", "\n", - "In the following, we show how we can combine the `station_name` text, `station_id` integer number and `temp` floating point number together using Python's f-string formatting approach. In addition, we will round a decimal number (temperature) into two decimal points on-the-fly:" + "In the following, we show how we can combine the `station_name` text, the `station_id` integer number, and the `temp` floating point number together using Python's f-string formatting approach. In addition, we will round a decimal number (`temp`) to two decimal points on the fly." ] }, { @@ -2500,11 +2500,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As you can see, using string formatting it is possible to easily modify a body of text \"interactively\" based on values stored in given variables. Figure 2.2 breaks down the different parts of the string. The text that you want to create and/or modify is enclosed within the quotes preceded with letter `f`. You can pass any existing variable inside the text template by placing the name of the variable within the curly braces `{}`. Using string formatting, it is also possible to insert numbers (such as `station_id` and `temp` here) within the body of text without needing first to convert the data type to string. This is because the f-string functionality kindly does the data type conversion for us in the background without us needing to worry about it (handy!). \n", + "As you can see, using string formatting it is possible to easily modify a body of text \"interactively\" based on values stored in given variables. Figure 2.2 breaks down the different parts of the string. The text that you want to create and/or modify is enclosed within the quotes preceded with letter `f`. You can pass any existing variable inside the text template by placing the name of the variable within the curly braces `{}`. Using string formatting, it is also possible to insert numbers (such as `station_id` and `temp`) within the body of text without needing first to convert the data types to strings. This is because the f-string functionality kindly does the data type conversion for us in the background without us needing to worry about it (handy!). \n", "\n", - "It is also possible to round these numbers on-the-fly to specific precision, such as two decimal points as in our example by adding format specifier (`:.2f`) after the variable that we want to round. The format specifier works by first adding a colon (`:`) after the variable name, and then specifying with dot (`.`) that we want to round our value to 2 decimal places (can be any digits). The final character `f` in the format specifier defines the type of the conversion that will be conducted: character `f` will convert the value to decimal number, whereas character `e` would make the number to appear as exponential number while character `%` would convert the value to percentage representation. \n", + "It is also possible to round numbers on the fly to specific precision, such as the two decimal points in our example, by adding the format specifier (`:.2f`) after the variable that we want to round. The format specifier works by first adding a colon (`:`) after the variable name, and then specifying with dot (`.`) that we want to round our value to 2 decimal places (can be any number of digits). The final character `f` in the format specifier defines the type of the formatting that will be done: the character `f` will display the value as a decimal number, the character `e` would make the number appear in scientific notation, while the character `%` would convert the value to percentage representation. \n", "\n", - "As we have hopefully demonstrated, f-string formatting is easy to use, yet powerful with its capability to e.g. do data conversions on the fly. Hence, it is the recommended approach for doing string manipulation nowadays in Python. Just remember to add the letter `f` before your string template! " + "As we have hopefully demonstrated, f-string formatting is easy to use, yet powerful with its capability to do data conversions on the fly, for example. Hence, it is the recommended approach for doing string manipulation presently in Python. Just remember to add the letter `f` before your string template! " ] }, { @@ -2519,7 +2519,7 @@ "source": [ "### Older approaches for string formatting\n", "\n", - "As mentioned previously, there are also a couple of other approaches that can be used to achieve the same result as above. These older approaches preceded the f-string which was introduced in Python version 3.6. The first one is `.format()` method that is placed after the quotes, like this:" + "As mentioned previously, there are also a couple of other approaches that can be used to achieve the same result as above. These older approaches preceded the f-string, which was introduced in Python version 3.6. The first one is the `.format()` method, which is placed after the string in quotes, like this:" ] }, { @@ -2555,9 +2555,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As you can see, here we got the same result as with f-strings, but used the `.format()` which was placed after the quotes. The variables were inserted within the text template by using curly braces and giving them a name (placeholder) which should have a matching counterpart within the parentheses that link to the actual variable which will be inserted to the body of text. As you see, the placeholder does not necessarily need to have the same name as the actual variable that contains the inserted value, but it can be anything, like the name `my_text_variable` as in the example above. \n", + "As you can see, here we get the same result as we did with an f-string, but we used the `.format()` placed after the quotes. The variables were inserted within the text template using curly braces and giving them a name (placeholder) which is expected to have a matching counterpart within the `.format()` parentheses that link to the variable value that will be inserted in the body of text. As you see, the placeholder does not necessarily need to have the same name as the actual variable that contains the inserted value, but it can be anything, like the name `my_text_variable` as in the example above. \n", "\n", - "The last (historical) string formatting approach is to use `%s` -operator. In this approach, the placeholder `%s` is added within the quotes, and the variables that are inserted into the body of text are placed inside parentheses after the `%` operator, like this:" + "The last (historical) string formatting approach is to use the `%s` operator. In this approach, the placeholder `%s` is added within the quotes, and the variables that are inserted into the body of text are placed inside parentheses after the `%` operator, like this:" ] }, { @@ -2607,7 +2607,7 @@ "## Manipulating character strings\n", "\n", "Here we demonstrate some of the most useful string manipulation techniques, such as splitting strings based on a given character, replacing characters with new ones, slicing strings, etc. \n", - "The aim is to produce a list of weather station locations in Helsinki that are represented in uppercase text (i.e., KUMPULA, KAISANIEMI, HARMAJA). The text that we will begin working with is following:" + "The aim is to produce a list of weather station locations in Helsinki that are represented in uppercase text (i.e., `KUMPULA, KAISANIEMI, HARMAJA`). The text that we will begin working with is below:" ] }, {