diff --git a/notebooks/01_python_basics.ipynb b/notebooks/01_python_basics.ipynb index c4ee966..e39926c 100644 --- a/notebooks/01_python_basics.ipynb +++ b/notebooks/01_python_basics.ipynb @@ -523,7 +523,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "# Boolean\n", @@ -849,7 +851,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "gene_seq = \"ATGCGACTGATCGATCGATCGATCGATGATCGATCGATCGATGCTAGCTAC\"\n", @@ -942,13 +946,15 @@ "outputs": [], "source": [ "print('Hello World')\n", - "print('Hello World\\n', sep=\"\")" + "print('Hello World\\n', end=\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "
\n", + "\n", "* **Combining** single and double quotes." ] }, @@ -1281,7 +1287,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Creating lists and tuples from iterables (e.g. sequences such as lists/tuples, generators, range)\n", + "#### Creating lists and tuples from iterables (e.g. sequences such as lists/tuples, range)\n", "\n", "* Objects that are *iterables* can be converted to lists or tuples." ] @@ -1297,7 +1303,7 @@ "print(list_1)\n", "print(tuple_1)\n", "\n", - "# Creating a list/tuple from a \"generator\" type object.\n", + "# Creating a list/tuple from a \"range\" type object.\n", "list_1 = list(range(21))\n", "tuple_1 = tuple(range(21))\n", "print(list_1)\n", @@ -1308,10 +1314,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> What are **generators** ? \n", - "> Generators are a type of objects that produce (generate) a sequence of items based.\n", - "> Unlike lists or tuples, the items in a sequence produced by a generator are not stored in memory, but\n", - "> generated *lazily* (i.e. \"on the fly\") at the time when they are needed." + "> What are **`range`** objects? \n", + "> `range` objects are sequences of **integer numbers**, e.g. `0, 1, 2, 3, 4, ...`.\n", + ">\n", + "> By default, a call to `range(x)` creates a sequence of integers from `0` to `x`, `x` excluded.\n", + ">\n", + "> **Examples**:\n", + "> * `range(10` -> `0, 1, 2, 3, 4, 5, 6, 7, 8, 9`\n", + "> * `range(3, 7)` -> `3, 4, 5, 6`" ] }, { @@ -1454,7 +1464,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "What can be done however, is to assign a new tuple to the same variable - this will *look* like we have modified a tuple, but in fact we have created a new tuple object and assigned it to our variable." + "
\n", + "\n", + "What can be done however, is to **assign a new tuple to the same variable** - this will *look* like we have modified a tuple, but in fact we have created a new tuple object and assigned it to our variable." ] }, { @@ -1463,8 +1475,11 @@ "metadata": {}, "outputs": [], "source": [ + "my_tuple = (1 , 2 , 3 , 5)\n", + "print(my_tuple)\n", + "\n", "my_tuple = (1 , 2 , 3 , \"spam\") # We do not modify an existing tuple: we create a new one.\n", - "print(my_tuple)" + "print(my_tuple)\n" ] }, { @@ -1473,7 +1488,7 @@ "source": [ "
\n", "\n", - "**Additional info**\n", + "### Additional info: tuples referencing mutable values\n", "\n", "* We just saw that **tuples are immutable**... but let's consider the following:" ] @@ -1524,7 +1539,7 @@ "\n", "### Copy of list content vs. copy of pointer (copying mutable values)\n", "\n", - "When assigning a **mutable** variable to another variable (as done below when assigning `l1` to `l2`), we are not duplicating the content of the existing variable: instead, we only create **a new pointer** to the content of the mutable variable." + "When assigning a variable to another variable (as done below when assigning `l1` to `l2`), we are not duplicating the content of the existing variable: instead, we only create **a new pointer** to the content of the original variable." ] }, { @@ -1886,7 +1901,7 @@ "outputs": [], "source": [ "quote = \"Drop your panties Sir William, I cannot wait till lunchtime.\"\n", - "individual_chars = list(my_string)\n", + "individual_chars = list(quote)\n", "\n", "print(individual_chars)" ] @@ -1910,7 +1925,7 @@ "outputs": [], "source": [ "quote = \"Drop your panties Sir William, I cannot wait till lunchtime.\"\n", - "words = my_string.split()\n", + "words = quote.split()\n", "\n", "print(words)" ] @@ -1933,8 +1948,15 @@ "source": [ "# Here, the separator calls the join method which accepts the list \"my_list\" as argument.\n", "quote = \" \".join(words) \n", - "print(quote)\n", - "\n", + "print(quote)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "# One can use a more exotic separator - in fact, any string can be used as separator.\n", "quote = \"_SEP_\".join(words) \n", "print(quote)\n", @@ -2016,18 +2038,21 @@ "## Dictionaries \n", "Dictionaries, or `dict`, are containers that associate a **key** to a **value**, just like a real world dictionary associates a word to its definition.\n", "* Dictionaries are instantiated with the `{key:value}` or `dict(key=value)` syntax.\n", + "\n", + " ```python\n", + " color_code = {'blue': 23, 'green': 45, 'red': 8}\n", + " ```\n", + " \n", "* **Keys** must be unique in the dictionary, and must be an immutable object (typically a `str`).\n", "* **Values** can appear as many time as desired in the dictionary.\n", "* The `[]` operator is used to **select objects from the dictionary**, but **using their key** instead\n", - " of their index.\n", - " ```python\n", - " color_code = {'blue': 23, 'green': 45, 'red': 8}\n", - " color_code['blue'] # returns 23\n", - " color_code['red'] # returns 8\n", - " ```\n", - "* Unlike lists or tuples, **dictionaries** are **unordered collections**: they do not record element position \n", - " or order of insertion. Therefore values cannot be retrieved by index position. E.g. `color_code[0]` is not \n", - " a valid syntax (and will raise a **`keyError`**), unless there is a key value of `0` in the dict.\n", + " of their index. E.g. `color_code[0]` is not a valid syntax (and will raise a **`KeyError`**), unless\n", + " there is a key value of `0` in the dict (which is not the case in our example).\n", + " \n", + " ```python\n", + " color_code['blue'] # returns 23\n", + " color_code['red'] # returns 8\n", + " ``` \n", "* Dictionaries are **mutable** objects: `key:value` pairs can be added and removed, values can be modified." ] }, @@ -2067,7 +2092,7 @@ "print(d[\"fox\"])\n", "\n", "key_value = \"penguin\"\n", - "print(f\"A {key_value} is a {d[key_value]}.\")" + "print(\"A\", key_value, \"is a\", d[key_value])" ] }, {