|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Lists\n", |
| 8 | + "\n", |
| 9 | + "Lists provide a simple way to hold a collection of values. You create a list using square brackets. For example, here we can create a list that contains the values \"cat\", \"dog\" and \"horse\"\n", |
| 10 | + "\n", |
| 11 | + "```python\n", |
| 12 | + "a =[ \"cat\", \"dog\", \"horse\" ]\n", |
| 13 | + "```" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": { |
| 20 | + "collapsed": true |
| 21 | + }, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "a = [\"cat\", \"dog\", \"horse\"]" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "print(a)" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "You can access the items in the list by placing the index of the item in square brackets. The first item is at index 0" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "a[0]" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "The second item is at index 1, and the third item at index 2." |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "a[2]" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "What do you think will happen if we access the item at index 3?" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "a[3]" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "You can also access the items in the list from the back. What do you think is at index -1?" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "a[-1]" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "What about index -2, -3 or -4?" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [ |
| 113 | + "a[-3]" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "You can add items onto a list by using the `.append` function. You can find this using tab completion and Python help..." |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "help(a.append)" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "metadata": { |
| 136 | + "collapsed": true |
| 137 | + }, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "a.append(\"fish\")" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "a" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": null, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "a[3]" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "markdown", |
| 163 | + "metadata": {}, |
| 164 | + "source": [ |
| 165 | + "You can put whatever you want into a list, including other lists!" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": null, |
| 171 | + "metadata": { |
| 172 | + "collapsed": true |
| 173 | + }, |
| 174 | + "outputs": [], |
| 175 | + "source": [ |
| 176 | + "b = [ 42, 15, a, [7,8,9] ]" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "b[3]" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "markdown", |
| 190 | + "metadata": {}, |
| 191 | + "source": [ |
| 192 | + "Putting lists inside lists allows for multidimensional lookup, e.g. can you work out why `b[3][2]` equals `9`?" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [ |
| 201 | + "b[3][2]" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "markdown", |
| 206 | + "metadata": {}, |
| 207 | + "source": [ |
| 208 | + "You can loop over the items in a list using a for loop, e.g." |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": null, |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [], |
| 216 | + "source": [ |
| 217 | + "for x in a:\n", |
| 218 | + " print(x)" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "markdown", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "You can get the number of items in the list using the `len` function." |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": null, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
| 233 | + "source": [ |
| 234 | + "len(a)" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "markdown", |
| 239 | + "metadata": {}, |
| 240 | + "source": [ |
| 241 | + "You can use this as an alternative way of looping over the elements of a list" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "code", |
| 246 | + "execution_count": null, |
| 247 | + "metadata": {}, |
| 248 | + "outputs": [], |
| 249 | + "source": [ |
| 250 | + "for i in range(0,len(a)):\n", |
| 251 | + " print(a[i])" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "metadata": {}, |
| 257 | + "source": [ |
| 258 | + "A string behaves like a list of letters. For example, if we have the string `s = \"Hello World\"`, then `s[0]` is \"H\" and `s[-1]` is d." |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": null, |
| 264 | + "metadata": { |
| 265 | + "collapsed": true |
| 266 | + }, |
| 267 | + "outputs": [], |
| 268 | + "source": [ |
| 269 | + "s = \"Hello World\"" |
| 270 | + ] |
| 271 | + }, |
| 272 | + { |
| 273 | + "cell_type": "code", |
| 274 | + "execution_count": null, |
| 275 | + "metadata": {}, |
| 276 | + "outputs": [], |
| 277 | + "source": [ |
| 278 | + "s[-1]" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "markdown", |
| 283 | + "metadata": {}, |
| 284 | + "source": [ |
| 285 | + "You can loop over every letter in a string in the same way that you can loop over every item in a list." |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "code", |
| 290 | + "execution_count": null, |
| 291 | + "metadata": {}, |
| 292 | + "outputs": [], |
| 293 | + "source": [ |
| 294 | + "for letter in s:\n", |
| 295 | + " print(letter)" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "markdown", |
| 300 | + "metadata": {}, |
| 301 | + "source": [ |
| 302 | + "# Exercises\n", |
| 303 | + "\n", |
| 304 | + "## Exercise 1\n", |
| 305 | + "\n", |
| 306 | + "Create two Python lists called `a` and `b`. Put into these lists the values [2, 4, 6, 8], and [10, 20, 30, 40].\n", |
| 307 | + "Check that `a[2]` equals `6` and `b[-1]` equals 40. (note that you will need to use the menu \"Insert | Insert Cell Below\" to insert more cells below to create space for your code)" |
| 308 | + ] |
| 309 | + }, |
| 310 | + { |
| 311 | + "cell_type": "code", |
| 312 | + "execution_count": null, |
| 313 | + "metadata": { |
| 314 | + "collapsed": true |
| 315 | + }, |
| 316 | + "outputs": [], |
| 317 | + "source": [] |
| 318 | + }, |
| 319 | + { |
| 320 | + "cell_type": "markdown", |
| 321 | + "metadata": {}, |
| 322 | + "source": [ |
| 323 | + "## Exercise 2\n", |
| 324 | + "\n", |
| 325 | + "Now create a loop that loops over each item in `a` and `b` and that calculates and prints out the product `a[i] * b[i]`." |
| 326 | + ] |
| 327 | + }, |
| 328 | + { |
| 329 | + "cell_type": "code", |
| 330 | + "execution_count": null, |
| 331 | + "metadata": { |
| 332 | + "collapsed": true |
| 333 | + }, |
| 334 | + "outputs": [], |
| 335 | + "source": [] |
| 336 | + }, |
| 337 | + { |
| 338 | + "cell_type": "markdown", |
| 339 | + "metadata": {}, |
| 340 | + "source": [ |
| 341 | + "## Exercise 3\n", |
| 342 | + "\n", |
| 343 | + "Modify your code to create a list called `c`. Use the `.append` function to set `c[i] = a[i] * b[i]`. Check your code by making sure that `c[-1]` equals 320." |
| 344 | + ] |
| 345 | + }, |
| 346 | + { |
| 347 | + "cell_type": "code", |
| 348 | + "execution_count": null, |
| 349 | + "metadata": { |
| 350 | + "collapsed": true |
| 351 | + }, |
| 352 | + "outputs": [], |
| 353 | + "source": [] |
| 354 | + } |
| 355 | + ], |
| 356 | + "metadata": { |
| 357 | + "kernelspec": { |
| 358 | + "display_name": "Python 3", |
| 359 | + "language": "python", |
| 360 | + "name": "python3" |
| 361 | + }, |
| 362 | + "language_info": { |
| 363 | + "codemirror_mode": { |
| 364 | + "name": "ipython", |
| 365 | + "version": 3 |
| 366 | + }, |
| 367 | + "file_extension": ".py", |
| 368 | + "mimetype": "text/x-python", |
| 369 | + "name": "python", |
| 370 | + "nbconvert_exporter": "python", |
| 371 | + "pygments_lexer": "ipython3", |
| 372 | + "version": "3.5.3" |
| 373 | + } |
| 374 | + }, |
| 375 | + "nbformat": 4, |
| 376 | + "nbformat_minor": 2 |
| 377 | +} |
0 commit comments