diff --git a/getting_started/00_overview.ipynb b/getting_started/00_overview.ipynb
index af3bb246c5a767ffc74bb993c088ffdbc41fdc7d..726c6baa7fda2867deec3fefa9bb0f375e5674bc 100644
--- a/getting_started/00_overview.ipynb
+++ b/getting_started/00_overview.ipynb
@@ -2,6 +2,7 @@
"cells": [
{
"cell_type": "markdown",
+ "id": "d9a95412",
"metadata": {},
"source": [
"# WIN Pytreat - Python overview\n",
@@ -67,6 +68,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "bfe0abb7",
"metadata": {},
"outputs": [],
"source": [
@@ -76,6 +78,7 @@
},
{
"cell_type": "markdown",
+ "id": "6b7b0a1d",
"metadata": {},
"source": [
"And you can explore the available methods on a Python object by using the\n",
@@ -85,6 +88,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "4aaf1645",
"metadata": {},
"outputs": [],
"source": [
@@ -94,6 +98,7 @@
},
{
"cell_type": "markdown",
+ "id": "5e17377c",
"metadata": {},
"source": [
"\n",
@@ -118,6 +123,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "7a90987e",
"metadata": {},
"outputs": [],
"source": [
@@ -129,6 +135,7 @@
},
{
"cell_type": "markdown",
+ "id": "6c5482ab",
"metadata": {},
"source": [
"**Cons**\n",
@@ -248,21 +255,23 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "92f979f2",
"metadata": {},
"outputs": [],
"source": [
"a = 7\n",
"b = 1 / 3\n",
"c = a + b\n",
- "print('a: ', a)\n",
- "print('b: ', b)\n",
- "print('c: ', c)\n",
- "print('b: {:0.4f}'.format(b))\n",
- "print('a + b:', a + b)"
+ "print('a: ', a)\n",
+ "print('b: ', b)\n",
+ "print('c: ', c)\n",
+ "print('b: {:0.4f}'.format(b))\n",
+ "print('a + b: ', a + b)"
]
},
{
"cell_type": "markdown",
+ "id": "1bfc6ffb",
"metadata": {},
"source": [
"\n",
@@ -272,25 +281,27 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "276cb884",
"metadata": {},
"outputs": [],
"source": [
"a = 'Hello'\n",
"b = \"Kitty\"\n",
- "c = '''\n",
+ "c = \"\"\"\n",
"Magic\n",
"multi-line\n",
"strings!\n",
- "'''\n",
+ "\"\"\"\n",
"\n",
"print(a, b)\n",
"print(a + b)\n",
- "print('{}, {}!'.format(a, b))\n",
+ "print(f'{a}, {b}!')\n",
"print(c)"
]
},
{
"cell_type": "markdown",
+ "id": "f233a287",
"metadata": {},
"source": [
"String objects have a number of useful methods:"
@@ -299,6 +310,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "479ad215",
"metadata": {},
"outputs": [],
"source": [
@@ -309,6 +321,7 @@
},
{
"cell_type": "markdown",
+ "id": "89132c40",
"metadata": {},
"source": [
"Another useful method is:"
@@ -317,6 +330,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "74773e8f",
"metadata": {},
"outputs": [],
"source": [
@@ -327,6 +341,7 @@
},
{
"cell_type": "markdown",
+ "id": "3d070148",
"metadata": {},
"source": [
"Two common and convenient string methods are `strip()` and `split()`. The\n",
@@ -336,6 +351,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "442e0a9b",
"metadata": {},
"outputs": [],
"source": [
@@ -346,6 +362,7 @@
},
{
"cell_type": "markdown",
+ "id": "9ff59d32",
"metadata": {},
"source": [
"With `split()` we can tokenize a string (to turn it into a list of strings)\n",
@@ -355,6 +372,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "7c7d9f09",
"metadata": {},
"outputs": [],
"source": [
@@ -364,6 +382,7 @@
},
{
"cell_type": "markdown",
+ "id": "d5485806",
"metadata": {},
"source": [
"We can also use the `join` method to re-construct a new string. Imagine that\n",
@@ -374,6 +393,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "e546a282",
"metadata": {},
"outputs": [],
"source": [
@@ -382,6 +402,7 @@
},
{
"cell_type": "markdown",
+ "id": "a3f3c49e",
"metadata": {},
"source": [
"`strip`, `split` and `join` makes this job trivial:"
@@ -390,15 +411,17 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "6a6e0823",
"metadata": {},
"outputs": [],
"source": [
- "print('Original: {}'.format(data))\n",
- "print('Strip, split, and join: {}'.format(' '.join(data.strip().split(','))))"
+ "print(f'Original: {data}')\n",
+ "print('Strip, split, and join:', ' '.join(data.strip().split(',')))"
]
},
{
"cell_type": "markdown",
+ "id": "c5009202",
"metadata": {},
"source": [
"\n",
@@ -417,6 +440,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "8114c7c6",
"metadata": {},
"outputs": [],
"source": [
@@ -435,6 +459,7 @@
},
{
"cell_type": "markdown",
+ "id": "fefe91af",
"metadata": {},
"source": [
"The key difference between lists and tuples is that tuples are *immutable*\n",
@@ -444,6 +469,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "3277edf9",
"metadata": {},
"outputs": [],
"source": [
@@ -454,6 +480,7 @@
},
{
"cell_type": "markdown",
+ "id": "c52a32cb",
"metadata": {},
"source": [
"Square brackets are used to index tuples, lists, strings, dictionaries, etc.\n",
@@ -463,6 +490,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "710c4f99",
"metadata": {},
"outputs": [],
"source": [
@@ -472,6 +500,7 @@
},
{
"cell_type": "markdown",
+ "id": "e57f4f8a",
"metadata": {},
"source": [
"> **MATLAB pitfall:** Python uses zero-based indexing, unlike MATLAB, where\n",
@@ -481,6 +510,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "3f0cf402",
"metadata": {},
"outputs": [],
"source": [
@@ -491,6 +521,7 @@
},
{
"cell_type": "markdown",
+ "id": "44726855",
"metadata": {},
"source": [
"A range of values for the indices can be specified to extract values from a\n",
@@ -500,6 +531,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "59c8d4a9",
"metadata": {},
"outputs": [],
"source": [
@@ -508,6 +540,7 @@
},
{
"cell_type": "markdown",
+ "id": "0beb8aed",
"metadata": {},
"source": [
"> **MATLAB pitfall:** Note that Python's slicing syntax is different from\n",
@@ -523,6 +556,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "44c5a48c",
"metadata": {},
"outputs": [],
"source": [
@@ -534,6 +568,7 @@
},
{
"cell_type": "markdown",
+ "id": "56b622fa",
"metadata": {},
"source": [
"You can also change the step size, which is specified by the third value (not\n",
@@ -543,6 +578,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "2c9b61e4",
"metadata": {},
"outputs": [],
"source": [
@@ -553,6 +589,7 @@
},
{
"cell_type": "markdown",
+ "id": "3949cc29",
"metadata": {},
"source": [
"Some methods are available on `list` objects for adding and removing items:"
@@ -561,6 +598,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "f96a3e31",
"metadata": {},
"outputs": [],
"source": [
@@ -579,6 +617,7 @@
},
{
"cell_type": "markdown",
+ "id": "4cf71af8",
"metadata": {},
"source": [
"What will `d.append([50,60])` do, and how is it different from\n",
@@ -588,6 +627,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "ac1fce51",
"metadata": {},
"outputs": [],
"source": [
@@ -597,6 +637,7 @@
},
{
"cell_type": "markdown",
+ "id": "15a01466",
"metadata": {},
"source": [
"\n",
@@ -611,6 +652,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "051312df",
"metadata": {},
"outputs": [],
"source": [
@@ -623,6 +665,7 @@
},
{
"cell_type": "markdown",
+ "id": "e39b639f",
"metadata": {},
"source": [
"Like lists (and unlike tuples), dicts are mutable, and have a number of\n",
@@ -632,6 +675,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "db998eb1",
"metadata": {},
"outputs": [],
"source": [
@@ -645,6 +689,7 @@
},
{
"cell_type": "markdown",
+ "id": "11002f02",
"metadata": {},
"source": [
"\n",
@@ -665,6 +710,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "14b8bfc8",
"metadata": {},
"outputs": [],
"source": [
@@ -677,6 +723,7 @@
},
{
"cell_type": "markdown",
+ "id": "ad4a61a6",
"metadata": {},
"source": [
"In contrast, when you pass a mutable value around, you are passing a\n",
@@ -688,6 +735,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "2852dd29",
"metadata": {},
"outputs": [],
"source": [
@@ -703,6 +751,7 @@
},
{
"cell_type": "markdown",
+ "id": "3405fef2",
"metadata": {},
"source": [
"\n",
@@ -724,6 +773,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "fa3a015c",
"metadata": {},
"outputs": [],
"source": [
@@ -740,6 +790,7 @@
},
{
"cell_type": "markdown",
+ "id": "66809170",
"metadata": {},
"source": [
"There is also the `in` test for strings, lists, etc:"
@@ -748,6 +799,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "69c83d4a",
"metadata": {},
"outputs": [],
"source": [
@@ -758,6 +810,7 @@
},
{
"cell_type": "markdown",
+ "id": "5c2a73d3",
"metadata": {},
"source": [
"We can use boolean values in `if`-`else` conditional expressions:"
@@ -766,19 +819,21 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "ae10c994",
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 3, 4]\n",
"val = 3\n",
"if val in a:\n",
- " print('Found {}!'.format(val))\n",
+ " print(f'Found {val}!')\n",
"else:\n",
- " print('{} not found :('.format(val))"
+ " print(f'{val} not found :(')"
]
},
{
"cell_type": "markdown",
+ "id": "bbaad787",
"metadata": {},
"source": [
"Note that the indentation in the `if`-`else` statement is **crucial**.\n",
@@ -793,6 +848,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "feb11366",
"metadata": {},
"outputs": [],
"source": [
@@ -803,6 +859,7 @@
},
{
"cell_type": "markdown",
+ "id": "2d0b9802",
"metadata": {},
"source": [
"You can also loop over the key-value pairs in a dict:"
@@ -811,23 +868,25 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "016a0b9a",
"metadata": {},
"outputs": [],
"source": [
"a = {'a' : 10, 'b' : 20, 'c' : 30}\n",
- "print('a.items()')\n",
- "for key, val in a.items():\n",
- " print(key, val)\n",
"print('a.keys()')\n",
"for key in a.keys():\n",
" print(key, a[key])\n",
"print('a.values()')\n",
"for val in a.values():\n",
- " print(val)"
+ " print(val)\n",
+ "print('a.items()')\n",
+ "for key, val in a.items():\n",
+ " print(key, val)"
]
},
{
"cell_type": "markdown",
+ "id": "5ecfcb80",
"metadata": {},
"source": [
"> In older versions of Python 3, there was no guarantee of ordering when using dictionaries.\n",
@@ -847,21 +906,23 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "bf4d0dbb",
"metadata": {},
"outputs": [],
"source": [
"d = [10, 20, 30]\n",
"print('Using the range function')\n",
"for i in range(len(d)):\n",
- " print('element at position {}: {}'.format(i, d[i]))\n",
+ " print(f'element at position {i}: {d[i]}')\n",
"\n",
"print('Using the enumerate function')\n",
"for i, elem in enumerate(d):\n",
- " print('element at position {}: {}'.format(i, elem))"
+ " print(f'element at position {i}: {elem}')"
]
},
{
"cell_type": "markdown",
+ "id": "2537a8f3",
"metadata": {},
"source": [
"\n",
@@ -876,17 +937,19 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "7f2bd1ce",
"metadata": {},
"outputs": [],
"source": [
"strings = ['hello', 'howdy', 'hi', 'hey']\n",
"nchars = [len(s) for s in strings]\n",
"for s, c in zip(strings, nchars):\n",
- " print('{}: {}'.format(s, c))"
+ " print(f'{s}: {c}')"
]
},
{
"cell_type": "markdown",
+ "id": "c7bd81f1",
"metadata": {},
"source": [
"> The `zip` function \"zips\" two or more sequences, so you can loop over them\n",
@@ -899,17 +962,19 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "e3542daa",
"metadata": {},
"outputs": [],
"source": [
"nchars = { s : len(s) for s in strings }\n",
"\n",
"for s, c in nchars.items():\n",
- " print('{}: {}'.format(s, c))"
+ " print(f'{s}: {c}')"
]
},
{
"cell_type": "markdown",
+ "id": "b09e20ec",
"metadata": {},
"source": [
"\n",
@@ -938,6 +1003,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "386b53ba",
"metadata": {},
"outputs": [],
"source": [
@@ -947,6 +1013,7 @@
},
{
"cell_type": "markdown",
+ "id": "888c2a4c",
"metadata": {},
"source": [
"A very similar syntax is used to write files:"
@@ -955,6 +1022,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "5bdb2fcc",
"metadata": {},
"outputs": [],
"source": [
@@ -965,6 +1033,7 @@
},
{
"cell_type": "markdown",
+ "id": "21cca2ce",
"metadata": {},
"source": [
"\n",
@@ -979,6 +1048,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "3be76086",
"metadata": {},
"outputs": [],
"source": [
@@ -988,6 +1058,7 @@
},
{
"cell_type": "markdown",
+ "id": "0c6e4b1f",
"metadata": {},
"source": [
"We may wish to process this data in another application (e.g. Excel or SPSS).\n",
@@ -997,17 +1068,19 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "4ee09755",
"metadata": {},
"outputs": [],
"source": [
"with open('lesion_counts.csv', 'w') as f:\n",
" f.write('Subject ID, Lesion count\\n')\n",
" for subj_id, count in zip(subject_ids, lesion_counts):\n",
- " f.write('{}, {}\\n'.format(subj_id, count))"
+ " f.write(f'{subj_id}, {count}\\n')"
]
},
{
"cell_type": "markdown",
+ "id": "01538f4b",
"metadata": {},
"source": [
"We can now load the `lesion_counts.csv` file into our analysis software of\n",
@@ -1018,6 +1091,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "9df720ba",
"metadata": {},
"outputs": [],
"source": [
@@ -1032,11 +1106,12 @@
"\n",
"print('Loaded lesion counts:')\n",
"for subj, count in lesion_counts.items():\n",
- " print('{}: {}'.format(subj, count))"
+ " print(f'{subj}: {count}')"
]
},
{
"cell_type": "markdown",
+ "id": "25ecd7db",
"metadata": {},
"source": [
"\n",
@@ -1061,6 +1136,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "70ea8549",
"metadata": {},
"outputs": [],
"source": [
@@ -1079,6 +1155,7 @@
},
{
"cell_type": "markdown",
+ "id": "60d3d252",
"metadata": {},
"source": [
"> Note that the `_` is used as shorthand here for a dummy variable\n",
@@ -1095,6 +1172,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "4b3b5f32",
"metadata": {},
"outputs": [],
"source": [
@@ -1113,6 +1191,7 @@
},
{
"cell_type": "markdown",
+ "id": "a5983a86",
"metadata": {},
"source": [
"You will often see python functions called with these named arguments. In\n",
@@ -1130,6 +1209,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "e0e53f1c",
"metadata": {},
"outputs": [],
"source": [
@@ -1145,6 +1225,7 @@
},
{
"cell_type": "markdown",
+ "id": "89b37ef2",
"metadata": {},
"source": [
"\n",
@@ -1173,6 +1254,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "ea408674",
"metadata": {},
"outputs": [],
"source": [
@@ -1181,6 +1263,7 @@
},
{
"cell_type": "markdown",
+ "id": "0fe6304b",
"metadata": {},
"source": [
"You could also emulate a 2D or ND matrix by using lists of lists, for example:"
@@ -1189,6 +1272,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "0f3e62a6",
"metadata": {},
"outputs": [],
"source": [
@@ -1199,6 +1283,7 @@
},
{
"cell_type": "markdown",
+ "id": "cd708cc7",
"metadata": {},
"source": [
"For simple tasks, you could stick with processing your data using python\n",
@@ -1234,6 +1319,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "dc215a0b",
"metadata": {},
"outputs": [],
"source": [
@@ -1252,6 +1338,7 @@
},
{
"cell_type": "markdown",
+ "id": "bfacc99a",
"metadata": {},
"source": [
"> Numpy is not a \"built-in\" library, so we have to import it. The statement\n",
@@ -1270,6 +1357,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "a1c2c0d1",
"metadata": {},
"outputs": [],
"source": [
@@ -1287,6 +1375,7 @@
},
{
"cell_type": "markdown",
+ "id": "3db10698",
"metadata": {},
"source": [
"The `zeros` and `ones` functions can also be used to generate N-dimensional\n",
@@ -1296,6 +1385,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "a24da68d",
"metadata": {},
"outputs": [],
"source": [
@@ -1307,6 +1397,7 @@
},
{
"cell_type": "markdown",
+ "id": "fdbb9c24",
"metadata": {},
"source": [
"> Note that, in a 2D Numpy array, the first axis corresponds to rows, and the\n",
@@ -1332,6 +1423,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "7072de1e",
"metadata": {},
"outputs": [],
"source": [
@@ -1342,6 +1434,7 @@
},
{
"cell_type": "markdown",
+ "id": "bbe6f261",
"metadata": {},
"source": [
"But you can also specify the delimiter to expect1:"
@@ -1350,6 +1443,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "d7eca1cd",
"metadata": {},
"outputs": [],
"source": [
@@ -1360,6 +1454,7 @@
},
{
"cell_type": "markdown",
+ "id": "337383bc",
"metadata": {},
"source": [
"> 1 And many other things such as file headers, footers, comments,\n",
@@ -1375,6 +1470,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "6cb9d979",
"metadata": {},
"outputs": [],
"source": [
@@ -1384,6 +1480,7 @@
},
{
"cell_type": "markdown",
+ "id": "84461b92",
"metadata": {},
"source": [
"Jupyter notebooks have a special feature - if you start a line with a `!`\n",
@@ -1394,6 +1491,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "e4e1bee1",
"metadata": {},
"outputs": [],
"source": [
@@ -1402,6 +1500,7 @@
},
{
"cell_type": "markdown",
+ "id": "dc4cf4a2",
"metadata": {},
"source": [
"> The `!` feature won't work in regular Python scripts.\n",
@@ -1414,6 +1513,7 @@
{
"cell_type": "code",
"execution_count": null,
+ "id": "07142a65",
"metadata": {},
"outputs": [],
"source": [
@@ -1421,11 +1521,11 @@
"colmeans = data.mean(axis=0)\n",
"\n",
"print('Column means')\n",
- "print('\\n'.join(['{}: {:0.2f}'.format(i, m) for i, m in enumerate(colmeans)]))"
+ "print('\\n'.join([f'{i}: {m:0.2f}' for i, m in enumerate(colmeans)]))"
]
}
],
"metadata": {},
"nbformat": 4,
- "nbformat_minor": 4
+ "nbformat_minor": 5
}
diff --git a/getting_started/00_overview.md b/getting_started/00_overview.md
index 8cb418151d878dbf30919488151f71a928016305..3722ffa9239cea7a2f04d6f4944571f4d353c88c 100644
--- a/getting_started/00_overview.md
+++ b/getting_started/00_overview.md
@@ -218,11 +218,11 @@ types). This is just like how things work in MATLAB.
a = 7
b = 1 / 3
c = a + b
-print('a: ', a)
-print('b: ', b)
-print('c: ', c)
-print('b: {:0.4f}'.format(b))
-print('a + b:', a + b)
+print('a: ', a)
+print('b: ', b)
+print('c: ', c)
+print('b: {:0.4f}'.format(b))
+print('a + b: ', a + b)
```
@@ -233,15 +233,15 @@ print('a + b:', a + b)
```
a = 'Hello'
b = "Kitty"
-c = '''
+c = """
Magic
multi-line
strings!
-'''
+"""
print(a, b)
print(a + b)
-print('{}, {}!'.format(a, b))
+print(f'{a}, {b}!')
print(c)
```
@@ -301,8 +301,8 @@ data = ' 1,2,3,4,5,6,7 '
```
-print('Original: {}'.format(data))
-print('Strip, split, and join: {}'.format(' '.join(data.strip().split(','))))
+print(f'Original: {data}')
+print('Strip, split, and join:', ' '.join(data.strip().split(',')))
```
@@ -551,9 +551,9 @@ We can use boolean values in `if`-`else` conditional expressions:
a = [1, 2, 3, 4]
val = 3
if val in a:
- print('Found {}!'.format(val))
+ print(f'Found {val}!')
else:
- print('{} not found :('.format(val))
+ print(f'{val} not found :(')
```
@@ -578,15 +578,15 @@ You can also loop over the key-value pairs in a dict:
```
a = {'a' : 10, 'b' : 20, 'c' : 30}
-print('a.items()')
-for key, val in a.items():
- print(key, val)
print('a.keys()')
for key in a.keys():
print(key, a[key])
print('a.values()')
for val in a.values():
print(val)
+print('a.items()')
+for key, val in a.items():
+ print(key, val)
```
@@ -608,11 +608,11 @@ There are some handy built-in functions that you can use with `for` loops:
d = [10, 20, 30]
print('Using the range function')
for i in range(len(d)):
- print('element at position {}: {}'.format(i, d[i]))
+ print(f'element at position {i}: {d[i]}')
print('Using the enumerate function')
for i, elem in enumerate(d):
- print('element at position {}: {}'.format(i, elem))
+ print(f'element at position {i}: {elem}')
```
@@ -629,7 +629,7 @@ number of characters in each of them:
strings = ['hello', 'howdy', 'hi', 'hey']
nchars = [len(s) for s in strings]
for s, c in zip(strings, nchars):
- print('{}: {}'.format(s, c))
+ print(f'{s}: {c}')
```
@@ -644,7 +644,7 @@ Or we could store the character counts in a dict:
nchars = { s : len(s) for s in strings }
for s, c in nchars.items():
- print('{}: {}'.format(s, c))
+ print(f'{s}: {c}')
```
@@ -710,7 +710,7 @@ Let's save the results out to a CSV (comma-separated value) file:
with open('lesion_counts.csv', 'w') as f:
f.write('Subject ID, Lesion count\n')
for subj_id, count in zip(subject_ids, lesion_counts):
- f.write('{}, {}\n'.format(subj_id, count))
+ f.write(f'{subj_id}, {count}\n')
```
@@ -731,7 +731,7 @@ with open('lesion_counts.csv', 'r') as f:
print('Loaded lesion counts:')
for subj, count in lesion_counts.items():
- print('{}: {}'.format(subj, count))
+ print(f'{subj}: {count}')
```
@@ -1016,5 +1016,5 @@ data = np.loadtxt('00_overview/2d_array.txt', comments='%')
colmeans = data.mean(axis=0)
print('Column means')
-print('\n'.join(['{}: {:0.2f}'.format(i, m) for i, m in enumerate(colmeans)]))
+print('\n'.join([f'{i}: {m:0.2f}' for i, m in enumerate(colmeans)]))
```