|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Dictionaries" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "### Start with some practice at getting the value from key:value pairs in a dictionary." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "##### A dictionary has aleady been defined below. Can you print out the French odd numbers:" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "engtofr = {'one':'un', 'two':'deux', 'three':'trois', 'four':'quatre', 'five':'quinze'}" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "##### Now we'll add the next number" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "engtofr['six'] = 'six'\n", |
| 47 | + "print('The new dictionary is', engtofr)" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "##### Can you add the numbers up to ten in a new cell - assuming you know French!" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "##### Maybe we want to show both feminine and masculine versions of 'one', so we can modify one of the key:value pairs as follows:" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "engtofr['one'] = 'un or unne'\n", |
| 71 | + "print('The new dictionary is', engtofr)" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "##### Oops - I wrote 'unne' instead of 'une'! Can you correct it in a new cell below please." |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "##### Removing key:value pairs can be done using 'del' statement:" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "del engtofr['two']\n", |
| 95 | + "print('My shortened list is', engtofr)" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "##### What happens if you run the previous cell again? Can you see what the problem is?" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "### Further methods for dictionaries" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "##### Create a dictionary in a new cell below - whatever you like. Let's assume you call it test_dict" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "##### Now print out test_dict.keys()\n", |
| 124 | + "##### Similarly, print out test_dict.values() and test_dict.items().\n", |
| 125 | + "##### These can all be useful, eg extracting the values to plot on a graph" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [] |
| 134 | + } |
| 135 | + ], |
| 136 | + "metadata": { |
| 137 | + "kernelspec": { |
| 138 | + "display_name": "Python 3", |
| 139 | + "language": "python", |
| 140 | + "name": "python3" |
| 141 | + }, |
| 142 | + "language_info": { |
| 143 | + "codemirror_mode": { |
| 144 | + "name": "ipython", |
| 145 | + "version": 3 |
| 146 | + }, |
| 147 | + "file_extension": ".py", |
| 148 | + "mimetype": "text/x-python", |
| 149 | + "name": "python", |
| 150 | + "nbconvert_exporter": "python", |
| 151 | + "pygments_lexer": "ipython3", |
| 152 | + "version": "3.7.4" |
| 153 | + } |
| 154 | + }, |
| 155 | + "nbformat": 4, |
| 156 | + "nbformat_minor": 2 |
| 157 | +} |
0 commit comments