diff --git a/getting_started/01_basics.ipynb b/getting_started/01_basics.ipynb index 677b36d559ec4b943240a696cc8fc7b4f8c603a0..cc5aca80be43ae089b941824dc74893c8e3a47c1 100644 --- a/getting_started/01_basics.ipynb +++ b/getting_started/01_basics.ipynb @@ -69,7 +69,7 @@ "* lists\n", "* dictionaries\n", "\n", - "N-dimensional arrays and other types are supported through common modules (e.g., numpy, scipy, scikit-learn). These will be covered in a subsequent exercise." + "N-dimensional arrays and other types are supported through common modules (e.g., [numpy](https://numpy.org/), [scipy](https://docs.scipy.org/doc/scipy-1.4.1/reference/), [scikit-learn](https://scikit-learn.org/stable/)). These will be covered in a subsequent exercises." ] }, { @@ -163,9 +163,8 @@ "<a class=\"anchor\" id=\"Format\"></a>\n", "### Format\n", "\n", - "More interesting strings can be created using the\n", - "[`format`](https://docs.python.org/3/library/string.html#formatstrings)\n", - "statement, which is very useful in print statements:" + "More interesting strings can be created using an [f-string](https://realpython.com/python-f-strings/), \n", + "which is very useful in print statements:" ] }, { @@ -176,36 +175,21 @@ "source": [ "x = 1\n", "y = 'PyTreat'\n", - "s = 'The numerical value is {} and a name is {}'.format(x, y)\n", + "s = f'The numerical value is {x} and a name is {y}'\n", "print(s)\n", - "print('A name is {} and a number is {}'.format(y, x))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python also supports C-style [`%`\n", - "formatting](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting):" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "x = 1\n", - "y = 'PyTreat'\n", - "s = 'The numerical value is %i and a name is %s' % (x, y)\n", - "print(s)\n", - "print('A name is %s and a number is %i' % (y, x))" + "print(f'A name is {y} and a number is {x}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "Note the `f` before the initial quote. This lets python know to fill in the variables between the curly brackets.\n", + "\n", + "There are also other options along these lines, which will be discussed in the next practical. \n", + "This is the more modern version, although you will see plenty of the other alternatives in \"old\" code \n", + "(to python coders this means anything written before last week).\n", + "\n", "<a class=\"anchor\" id=\"String-manipulation\"></a>\n", "### String manipulation\n", "\n", @@ -460,7 +444,7 @@ "metadata": {}, "source": [ "> Similar things can be done for tuples, except for the last one: that is,\n", - "> `a += (80)` as a tuple is immutable so cannot be changed like this.\n", + "> `a += (80)` because a tuple is immutable so cannot be changed like this.\n", "\n", "<a class=\"anchor\" id=\"Indexing\"></a>\n", "### Indexing\n", @@ -626,7 +610,7 @@ "> _*Pitfall:*_\n", ">\n", "> Unlike in MATLAB, you cannot use a list as indices instead of an\n", - "> integer or a slice (although these can be done in `numpy`)." + "> integer or a slice (although this can be done in `numpy`)." ] }, { @@ -1066,7 +1050,7 @@ "not considered 'equal' in the sense that the operator `==` would consider them\n", "the same.\n", "\n", - "Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=`\n", + "Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=`.\n", "\n", "For example:" ] @@ -1107,7 +1091,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A useful keyword is `None`, which is a bit like \"null\". This can be a default value for a variable and should be tested with the `is` operator rather than `==` (for technical reasons that it isn't worth going into here). For example: `a is None` or `a is not None` are the preferred tests.\n", + "A useful keyword is `None`, which is a bit like \"null\". \n", + "This can be a default value for a variable and should be tested with the `is` operator rather than `==` (for technical reasons that it isn't worth going into here). For example: `a is None` or `a is not None` are the preferred tests.\n", + "Do not use the `is` instead of the `==` operator for any other comparisons (unless you know what you are doing).\n", "\n", "\n", "<a class=\"anchor\" id=\"If-statements\"></a>\n", diff --git a/getting_started/01_basics.md b/getting_started/01_basics.md index ac9372c7e12bd688b410200a76fe65eefdc69517..91bda70fbf45dba147f64e45990c777e6d10c5e4 100644 --- a/getting_started/01_basics.md +++ b/getting_started/01_basics.md @@ -63,7 +63,7 @@ Python has many different types and variables are dynamic and can change types ( * lists * dictionaries -N-dimensional arrays and other types are supported through common modules (e.g., numpy, scipy, scikit-learn). These will be covered in a subsequent exercise. +N-dimensional arrays and other types are supported through common modules (e.g., [numpy](https://numpy.org/), [scipy](https://docs.scipy.org/doc/scipy-1.4.1/reference/), [scikit-learn](https://scikit-learn.org/stable/)). These will be covered in a subsequent exercises. ``` a = 4 @@ -114,28 +114,20 @@ print(s3) <a class="anchor" id="Format"></a> ### Format -More interesting strings can be created using the -[`format`](https://docs.python.org/3/library/string.html#formatstrings) -statement, which is very useful in print statements: - +More interesting strings can be created using an [f-string](https://realpython.com/python-f-strings/), +which is very useful in print statements: ``` x = 1 y = 'PyTreat' -s = 'The numerical value is {} and a name is {}'.format(x, y) +s = f'The numerical value is {x} and a name is {y}' print(s) -print('A name is {} and a number is {}'.format(y, x)) +print(f'A name is {y} and a number is {x}') ``` +Note the `f` before the initial quote. This lets python know to fill in the variables between the curly brackets. -Python also supports C-style [`%` -formatting](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting): - -``` -x = 1 -y = 'PyTreat' -s = 'The numerical value is %i and a name is %s' % (x, y) -print(s) -print('A name is %s and a number is %i' % (y, x)) -``` +There are also other options along these lines, which will be discussed in the next practical. +This is the more modern version, although you will see plenty of the other alternatives in "old" code +(to python coders this means anything written before last week). <a class="anchor" id="String-manipulation"></a> ### String manipulation @@ -263,7 +255,7 @@ print(a) ``` > Similar things can be done for tuples, except for the last one: that is, -> `a += (80)` as a tuple is immutable so cannot be changed like this. +> `a += (80)` because a tuple is immutable so cannot be changed like this. <a class="anchor" id="Indexing"></a> ### Indexing @@ -337,7 +329,7 @@ print(a[1:3]) # same as a(2:3) in MATLAB > _*Pitfall:*_ > > Unlike in MATLAB, you cannot use a list as indices instead of an -> integer or a slice (although these can be done in `numpy`). +> integer or a slice (although this can be done in `numpy`). ``` b = [3, 4] @@ -574,7 +566,7 @@ capitals). Other values can also be used for True or False (e.g., `1` for not considered 'equal' in the sense that the operator `==` would consider them the same. -Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=` +Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=`. For example: ``` @@ -594,7 +586,9 @@ print(3 in [1, 2, 3, 4]) ``` -A useful keyword is `None`, which is a bit like "null". This can be a default value for a variable and should be tested with the `is` operator rather than `==` (for technical reasons that it isn't worth going into here). For example: `a is None` or `a is not None` are the preferred tests. +A useful keyword is `None`, which is a bit like "null". +This can be a default value for a variable and should be tested with the `is` operator rather than `==` (for technical reasons that it isn't worth going into here). For example: `a is None` or `a is not None` are the preferred tests. +Do not use the `is` instead of the `==` operator for any other comparisons (unless you know what you are doing). <a class="anchor" id="If-statements"></a>