From 7a597041fbddf57a7b7ff5aa1c83b52a8cb7e48c Mon Sep 17 00:00:00 2001 From: Michiel Cottaar <MichielCottaar@gmail.com> Date: Sat, 10 Feb 2018 11:42:10 +0000 Subject: [PATCH] Updated notebook --- getting_started/02_text_io.ipynb | 55 ++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/getting_started/02_text_io.ipynb b/getting_started/02_text_io.ipynb index c28071c..c3b6aa2 100644 --- a/getting_started/02_text_io.ipynb +++ b/getting_started/02_text_io.ipynb @@ -103,14 +103,14 @@ "metadata": {}, "outputs": [], "source": [ - "!cat 02_test_io/my_file" + "!cat 02_text_io/my_file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> Any lines starting with `!` will be interpreted as shell commands by ipython. Note that this is an ipython feature and can not be used to call shell commands in your python scripts. How to call shell commands from python will be discusses in the `scripts` practical.\n", + "> Any lines starting with `!` will be interpreted as shell commands by ipython. It is great when playing around in the ipython notebook or in the ipython terminal, however it is an ipython-only feature and hence is not available when writing python scripts. How to call shell commands from python will be discusses in the `scripts` practical.\n", "\n", "If we want to add to the existing file we can open it in the append mode:" ] @@ -123,7 +123,7 @@ "source": [ "with open('02_text_io/my_file', 'a') as my_file:\n", " my_file.write('More lines is always better\\n')\n", - "!cat 02_test_io/my_file" + "!cat 02_text_io/my_file" ] }, { @@ -155,7 +155,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The main rationale for choosing between single or double quotes, is whether the string itself will contain any quotes. You can include a single quote in a string surrounded by single quotes by escaping it with the `\\` character:" + "The main rationale for choosing between single or double quotes, is whether the string itself will contain any quotes. You can include a single quote in a string surrounded by single quotes by escaping it with the `\\` character, however in such a case it would be more convenient to use double quotes:" ] }, { @@ -190,7 +190,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "However, the easiest way to create multi-line strings is to use a triple quote (again single or double quotes can be used:" + "However, the easiest way to create multi-line strings is to use a triple quote (again single or double quotes can be used). Triple quotes allow your string to span multiple lines:" ] }, { @@ -222,6 +222,23 @@ "print(single_line_string)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "One pitfall when creating a list of strings is that python automatically concatenates string literals, which are only separated by white space:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_list_of_strings = ['a', 'b', 'c' 'd', 'e']\n", + "print(\"The 'c' and 'd' got concatenated, because we forgot the comma:\", my_list_of_strings)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -247,7 +264,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In python 2 each element in a string was a single byte rather than a potentially multi-byte character. You can create such a byte array from your unicode string in python 3 using the `encode()` method and converted back to a `decode()` method." + "In python 2 each element in a string was a single byte rather than a potentially multi-byte character. You can convert back to interpreting your sequence as a unicode string or a byte array using:\n", + "* `encode()` called on a string converts it into a bytes array (`bytes` object)\n", + "* `decode()` called on a `bytes` array converts it into a unicode string." ] }, { @@ -257,7 +276,7 @@ "outputs": [], "source": [ "delta = \"Δ\"\n", - "print(delta, ' in python 2 would be represented as ', delta.encode())" + "print('The character', delta, 'consists of the following 2 bytes', delta.encode())" ] }, { @@ -293,7 +312,7 @@ "outputs": [], "source": [ "with open('/usr/local/fsl/data/standard/MNI152_T1_1mm.nii.gz', 'rb') as gzipped_nifti:\n", - " print('First few bytes of gzipped NIFTI file:', gzipped_nifti[:10])" + " print('First few bytes of gzipped NIFTI file:', gzipped_nifti.read(10))" ] }, { @@ -305,7 +324,7 @@ "There are two functions to convert python objects into strings, `repr()` and `str()`.\n", "All other functions that rely on string-representations of python objects will use one of these two (for example the `print()` function will call `str()` on the object).\n", "\n", - "The goal of the `str()` function is to be readable, while the goal of `repr()` is to be unambiguous. For example" + "The goal of the `str()` function is to be readable, while the goal of `repr()` is to be unambiguous. Compare" ] }, { @@ -322,7 +341,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "While the output of both `str()` functions are very clear, we can not know whether the input was a string or an actual integer." + "with" ] }, { @@ -339,7 +358,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note that the output of the `repr()` function can be directly be passed back to the python interpreter to recreate our string/integer.\n", + "In both cases you get the value of the object (3), but only the `repr` returns enough information to actually know the type of the object.\n", "\n", "<a class=\"anchor\" id=\"combining-strings\"></a>\n", "### Combining strings\n", @@ -448,6 +467,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "Note that the variable `:` delimeter separates the variable identifies on the left from the formatting rules on the right.\n", + "\n", "Finally the new, fancy formatted string literals (only available in python 3.6+). This new format is very similar to the recommended style, except that all placeholders are automatically evaluated in the local environment at the time the template is defined. This means that we do not have to explicitly provide the parameters (and we can evaluate the sum inside the string!), although it does mean we also can not re-use the template." ] }, @@ -460,7 +481,7 @@ "a = 3\n", "b = 1/3\n", "\n", - "print(f'{a + b:.3f} = {a} + {b:.3f} = {a + b}')" + "print(f'{a + b:.3f} = {a} + {b:.3f}')" ] }, { @@ -525,7 +546,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To separate a comma separated list we will need to supply the delimiter to the `split()` method. We can then use the `strip()` method to remove any trailing whitespace:" + "To separate a comma separated list we will need to supply the delimiter to the `split()` method. We can then use the `strip()` method to remove any whitespace at the beginning or end of the string:" ] }, { @@ -537,7 +558,7 @@ "scientific_packages_string = \"numpy, scipy, pandas, matplotlib, nibabel\"\n", "list_with_whitespace = scientific_packages_string.split()\n", "print(list_with_whitespace)\n", - "list_without_whitespace = [individiual_string.strip() for indivisual_string in list_with_shite_space]\n", + "list_without_whitespace = [individual_string.strip() for individual_string in list_with_whitespace]\n", "print(list_without_whitespace)" ] }, @@ -561,8 +582,8 @@ "print(int(\"3\"))\n", "print(float(\"3\"))\n", "print(float(\"3.213\"))\n", - "print(float(\"3.213E5\"))\n", - "print(float(\"3.213d-25\"))" + "print(float(\"3.213e5\"))\n", + "print(float(\"3.213E-25\"))" ] }, { @@ -573,7 +594,7 @@ "### Regular expressions\n", "Regular expressions are used for looking for specific patterns in a longer string. This can be used to extract specific information from a well-formatted string or to modify a string. In python regular expressions are available in the [re](https://docs.python.org/3/library/re.html#re-syntax) module.\n", "\n", - "A full discussion of regular expression goes far beyond this tutorial. If you are interested, have a look at [https://docs.python.org/3/howto/regex.html]\n", + "A full discussion of regular expression goes far beyond this practical. If you are interested, have a look [here](https://docs.python.org/3/howto/regex.html).\n", "\n", "<a class=\"anchor\" id=\"exercises\"></a>\n", "## Exercises\n", -- GitLab