|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Lecture 1: Jupyter Notebooks, lists, dictionaries, numpy arrays\n", |
| 8 | + "\n", |
| 9 | + "Also see the tutorials for lists and dictionaries. This lecture will focus on Fun Things with Numpy arrays.\n", |
| 10 | + "We're going to do stuff with lists, first, because it's easier to understand. After this lecture, you should always use numpy arrays, not lists, when storing/manipulating arrays of numbers." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "# Some typical list-based ways to store a list of numbers\n", |
| 20 | + "a_list = [0.0, 1.0, 2.0, 3.0]\n", |
| 21 | + "b_list = [-2.0, -1.0, 0.0, 0.2]\n", |
| 22 | + "\n", |
| 23 | + "print(a_list)\n", |
| 24 | + "print(b_list)" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "# print one element of the list (notice index, 1)\n", |
| 34 | + "print(a_list[1])" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "# Calculate the sum of a list\n", |
| 44 | + "my_sum = 0\n", |
| 45 | + "for e in a_list:\n", |
| 46 | + " # e is set to a_list[i] where i is 0, 1, 2 etc\n", |
| 47 | + " my_sum += e\n", |
| 48 | + "print(f\"Sum: {my_sum}\")" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": null, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "# Perform a mathematical operation on each element in the list \n", |
| 58 | + "# In this case, 2 * x + 1\n", |
| 59 | + "#. Notice how we create a list to put the result in, and use append (rather than creating the list first)\n", |
| 60 | + "operate_on_a_list = []\n", |
| 61 | + "for e in a_list:\n", |
| 62 | + " operate_on_a_list.append( 2 * e + 1)\n", |
| 63 | + "print(operate_on_a_list)" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "# Iterate through two lists together\n", |
| 73 | + "add_lists = []\n", |
| 74 | + "for a, b in zip(a_list, b_list):\n", |
| 75 | + " add_lists.append(a + b)\n", |
| 76 | + "print(f\"Adding lists: {add_lists}\")" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "# See if 2.0 is in the list\n", |
| 86 | + "b_found_two = False\n", |
| 87 | + "for a in a_list:\n", |
| 88 | + " if abs(a - 2.0) < 1e-12:\n", |
| 89 | + " b_found_two = True\n", |
| 90 | + "print(f\"Found 2: {b_found_two}\")" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "## Just say NO to lists for mathematical operations on arrays of numbers\n", |
| 98 | + "Use numpy.\n", |
| 99 | + "Do numpy tutorial before lab." |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "# Import the library numpy and declare that you want to access numpy as np.[blah]\n", |
| 109 | + "import numpy as np" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "a_np_array = np.array(a_list) # Shortcut way to create a numpy array from a list\n", |
| 119 | + "b_np_array = np.array(b_list)\n", |
| 120 | + "print(a_np_array) # Notice different formatting from a list\n", |
| 121 | + "print(b_np_array)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "# Calculate the sum of a list\n", |
| 131 | + "print(f\"Sum: {np.sum(a_np_array)}\")" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "# Perform a mathematical operation on each element in the list \n", |
| 141 | + "# In this case, 2 * x + 1\n", |
| 142 | + "#. Look - no for loop!!!\n", |
| 143 | + "operate_on_an_np_array = 2 * a_np_array + 1\n", |
| 144 | + "print(operate_on_an_np_array)" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
| 153 | + "# Add two lists together\n", |
| 154 | + "print(f\"Adding lists: {a_np_array + b_np_array}\")" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": null, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "# See if 2.0 is in the list\n", |
| 164 | + "#. isclose and any are a bit complicated to use, but very powerful\n", |
| 165 | + "all_twos = np.isclose(a_np_array, 2.0)\n", |
| 166 | + "if all_twos.any():\n", |
| 167 | + " print(\"Found a 2\")\n", |
| 168 | + "print(f\"Found 2: {all_twos}\")" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "markdown", |
| 173 | + "metadata": {}, |
| 174 | + "source": [ |
| 175 | + "## Dictionaries\n", |
| 176 | + "See tutorial on dictionaries.\n", |
| 177 | + "\n", |
| 178 | + "Dictionaries are the heart of Python - pretty much everything in Python is a dictionary.\n", |
| 179 | + "\n", |
| 180 | + "Basic idea: Store data by a key (name) and value (what's in that key)\n", |
| 181 | + "\n", |
| 182 | + "Difference to lists: You can think of a list as a dictionary where all the keys are the numbers 0..n-1" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "# Notice curly brackets\n", |
| 192 | + "my_dict = {\"key 1\": [-0.2, 0.2], \"key 2\": \"value\"}\n", |
| 193 | + "print(my_dict)" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": null, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "# Getting one element out of the dictionary\n", |
| 203 | + "print(my_dict[\"key 1\"])" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": null, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "for k, v in my_dict.items():\n", |
| 213 | + " print(f\"Key {k}, value {v}\")" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [], |
| 221 | + "source": [ |
| 222 | + "# Adding one element to the dictionary\n", |
| 223 | + "my_dict[3] = \"Value for key 3\"\n", |
| 224 | + "print(my_dict)" |
| 225 | + ] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "code", |
| 229 | + "execution_count": null, |
| 230 | + "metadata": {}, |
| 231 | + "outputs": [], |
| 232 | + "source": [] |
| 233 | + } |
| 234 | + ], |
| 235 | + "metadata": { |
| 236 | + "kernelspec": { |
| 237 | + "display_name": "Python 3", |
| 238 | + "language": "python", |
| 239 | + "name": "python3" |
| 240 | + }, |
| 241 | + "language_info": { |
| 242 | + "codemirror_mode": { |
| 243 | + "name": "ipython", |
| 244 | + "version": 3 |
| 245 | + }, |
| 246 | + "file_extension": ".py", |
| 247 | + "mimetype": "text/x-python", |
| 248 | + "name": "python", |
| 249 | + "nbconvert_exporter": "python", |
| 250 | + "pygments_lexer": "ipython3", |
| 251 | + "version": "3.7.4" |
| 252 | + } |
| 253 | + }, |
| 254 | + "nbformat": 4, |
| 255 | + "nbformat_minor": 2 |
| 256 | +} |
0 commit comments