{ "metadata": { "name": "", "signature": "sha256:a3e7e00655192f2fe64a1880b50d675644627f8ccbd21f636e8dbbbef3536aa1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## There are four main types of collections of data (\"Sequence objects\") ##\n", "\n", "\t\u2022\tLists: a mutable array of data\n", "\t\u2022\tTuples: ordered, immutable list\n", "\t\u2022\tSets: unordered collection of unique elements\n", "\t\u2022\tDictionaries: keyword/value lookup\n", "\n", "The value in each element can be whatever (type) you want.\n", "> string is actually a sequence object" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### List ###\n", "#### denoted with a brackets ####" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = [1,2,3] ; print len(v), type(v)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3 \n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "v[0:2]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[1, 2]" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "v = [\"eggs\",\"spam\",-1,(\"monty\",\"python\"),[-1.2,-3.5]]\n", "len(v)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "5" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "v[0] =\"green egg\"\n", "v[1] += \",love it.\"\n", "v[-1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "[-1.2, -3.5]" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "v[-1][1] = None ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['green egg', 'spam,love it.', -1, ('monty', 'python'), [-1.2, None]]\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "v = v[2:] ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[-1, ('monty', 'python'), [-1.2, None]]\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": true, "input": [ "# let's make a proto-array out of nested lists\n", "vv = [ [1,2], [3,4] ]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "print len(vv)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "determinant = vv[0][0]*vv[1][1] - vv[0][1]*vv[1][0]\n", "print determinant" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "the main point here: lists are **changeable** (\"mutable\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### lists can be extended & appended ###" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = [1,2,3]\n", "v.append(4) \n", "v.append([-5]) ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, [-5]]\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> Lists can be considered objects.\n", "**Objects** are like animals: they know how to do stuff (like eat and sleep), they know how to interact with others (like make children), and they have characteristics (like height, weight).\n", "\n", "> \"Knowing how to do stuff\" with itself is called a method. In this case \"append\" is a method which, when invoked, is an action that changes the characteristics (the data vector of the list itself)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = v[:4]\n", "w = ['elderberries', 'eggs']\n", "v + w" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "[1, 2, 3, 4, 'elderberries', 'eggs']" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "v.extend(w) ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, 'elderberries', 'eggs']\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "v.pop()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "'eggs'" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, 'elderberries']\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "v.pop(0) ; print v ## pop the first element" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2, 3, 4, 'elderberries']\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " * `.append()`: adds a new element\n", " * `.extend()`: concatenates a list/element\n", " * `.pop()`: remove an element" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### lists can be searched, sorted, & counted ####" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = [1,3, 2, 3, 4, 'elderberries']\n", "v.sort() ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 3, 4, 'elderberries']\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`reverse` is a keyword of the `.sort()` method" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v.sort(reverse=True) ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['elderberries', 4, 3, 3, 2, 1]\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`.sort()` changes the the list in place " ] }, { "cell_type": "code", "collapsed": false, "input": [ "v.index(4) ## lookup the index of the entry 4" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "1" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "v.index(3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "2" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "v.count(3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "2" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "v.insert(0,\"it's full of stars\") ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[\"it's full of stars\", 'elderberries', 4, 3, 3, 2, 1]\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "v.remove(1) ; print v" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[\"it's full of stars\", 'elderberries', 4, 3, 3, 2]\n" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### IPython is your new best friend ##\n", "\n", "1. Type `v.` then the Tab button\n", "\n", "2. Type `v.re` then the Tab button\n", "\n", "3. Type `v.remove?`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "## try it here" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### List ###\n", "#### iteration ####" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = ['cat', 'window', 'defenestrate']\n", "for x in a:\n", " print x, len(x)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "cat 3\n", "window 6\n", "defenestrate 12\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "for i,x in enumerate(a):\n", " print i, x, len(x)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 cat 3\n", "1 window 6\n", "2 defenestrate 12\n" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "for x in a:\n", " print x," ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "cat window defenestrate\n" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The syntax for iteration is... \n", "\n", " for variable_name in iterable:\n", " # do something with variable_name" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The `range()` function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = range(4) ; print x\n", "total = 0\n", "for val in range(4):\n", " total += val\n", " print \"By adding \" + str(val) + \\\n", " \" the total is now \" + str(total)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3]\n", "By adding 0 the total is now 0\n", "By adding 1 the total is now 1\n", "By adding 2 the total is now 3\n", "By adding 3 the total is now 6\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", "`range`([`start`,] `stop`[, `step`])\n", "\u2192 list of integers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "total = 0\n", "for val in range(1,10,2):\n", " total += val\n", " print \"By adding \" + str(val) + \\\n", " \" the total is now \" + str(total)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "By adding 1 the total is now 1\n", "By adding 3 the total is now 4\n", "By adding 5 the total is now 9\n", "By adding 7 the total is now 16\n", "By adding 9 the total is now 25\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "a = ['Mary', 'had', 'a', 'little', 'lamb']\n", "for i in range(len(a)):\n", " print i, a[i]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 Mary\n", "1 had\n", "2 a\n", "3 little\n", "4 lamb\n" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Tuple ###\n", "denoted with parentheses" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (12,-1)\n", "print type(t)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "print isinstance(t,tuple)\n", "print len(t)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "2\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "t = (12,\"monty\",True,-1.23e6)\n", "print t[1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "monty\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "print t[-1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-1230000.0\n" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "t[-2:] # get the last two elements, return as a tuple" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "(True, -1230000.0)" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "x = (True) ; print type(x)\n", "x = (True,) ; print type(x)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "type(()), len(())" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "(tuple, 0)" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "type((,))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m type((,))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "single-element tuples look like `(element,)`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "cannot change a tuple\n", "but you can create new one with concatenation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t[2] = False" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "t[0:2], False, t[3:]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "((12, 'monty'), False, (-1230000.0,))" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "## the above is not what we wanted... need to concatenate\n", "t[0:2] + False + t[3:]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate tuple (not \"bool\") to tuple", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m## the above is not what we wanted... need to concatenate\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mFalse\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate tuple (not \"bool\") to tuple" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "y = t[0:2] + (False,) + t[3:] ; print y" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(12, 'monty', False, -1230000.0)\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "t*2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "(12, 'monty', True, -1230000.0, 12, 'monty', True, -1230000.0)" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Casting Back and Forth ###" ] }, { "cell_type": "code", "collapsed": true, "input": [ "a = [1,2,3,(\"b\",1)]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "b = tuple(a) ; print b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1, 2, 3, ('b', 1))\n" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "print list(b)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, ('b', 1)]\n" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "set(a)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "{1, 2, 3, ('b', 1)}" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "list(set(\"spam\"))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "['a', 'p', 's', 'm']" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> casting only affects top-level structure, not the elements " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Why use tuples when you have lists? ####" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A tuple is something that you probably don\u2019t want changed\n", "continents = (\"North America\", \"South America\", \"Europe\", \"Asia\", \"Australia\", \"Antarctica\")\n", "\n", "# Something you might want to add and subtract from\n", "tasks = [\"learn Python\",\"eat dinner\",\"climb Mt. Everest\"] " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "# something you probably don't want in your tasks...\n", "tasks.append(\"find Atlantis\")\n", "print tasks" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['learn Python', 'eat dinner', 'climb Mt. Everest', 'find Atlantis']\n" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "# tuples are immutable!\n", "continents.append(\"Atlantis\")" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# tuples are immutable!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcontinents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Atlantis\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "first_name, last_name = \"Jack\", \"Hewitt\" # This is a tuple assignment\n", "print \"My name is\", first_name, last_name # This statement prints a tuple" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My name is Jack Hewitt\n" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "# This function returns a tuple (more on this later\u2026) \n", "import cmath\n", "r,phi = cmath.polar(-1.); print r,phi" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.0 3.14159265359\n" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Sets ###\n", "#### denoted with a curly braces ####" ] }, { "cell_type": "code", "collapsed": false, "input": [ "{1,2,3,\"bingo\"}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "{1, 2, 3, 'bingo'}" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "print type({1,2,3,\"bingo\"})" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "print type({})" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(set())" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "set(\"spamIam\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "{'I', 'a', 'm', 'p', 's'}" ] } ], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "sets have unique elements. They can be\n", "compared, differenced, unionized, etc." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = set(\"sp\"); b = set(\"am\"); print a ; print b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set(['p', 's'])\n", "set(['a', 'm'])\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "c = set([\"a\",\"m\"])\n", "c == b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 59, "text": [ "True" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "\"p\" in a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "True" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "\"ps\" in a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "False" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "q = set(\"spamIam\")\n", "a.issubset(q)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 62, "text": [ "True" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "a | b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 63, "text": [ "{'a', 'm', 'p', 's'}" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "q - (a | b)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 64, "text": [ "{'I'}" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "q & (a | b)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "{'a', 'm', 'p', 's'}" ] } ], "prompt_number": 65 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Like lists, we can use as (unordered) buckets\n", "`.pop()` gives us a random element" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# this is pretty volitile...wont be the same\n", "# order on all machines\n", "for i in q & (a | b):\n", " print i, " ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a p s m\n" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": true, "input": [ "q.remove(\"a\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "q.pop()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 68, "text": [ "'p'" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "print q.pop()\n", "print q.pop()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "s\n", "m\n" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "print q.pop()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I\n" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "q.pop()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "KeyError", "evalue": "'pop from an empty set'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'pop from an empty set'" ] } ], "prompt_number": 71 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Dictionaries ##\n", "denoted with a curly braces and colons" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {\"favorite cat\": None, \"favorite spam\": \"all\"}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 72 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "these are key: value, key: value, ..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print d[\"favorite cat\"]\n", "d[0] ## this is not a list and you dont have a keyword = 0" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] }, { "ename": "KeyError", "evalue": "0", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"favorite cat\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m## this is not a list and you dont have a keyword = 0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 0" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "e = {\"favorite cat\": None, \"favorite spam\": \"all\", \\\n", " 1: 'loneliest number'}\n", "e[1] == 'loneliest number'" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "True" ] } ], "prompt_number": 74 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "dictionaries are **UNORDERED***. \n", ">You cannot assume that one key comes before or after another\n", "\n", "* you can use a special type of ordered dict if you really need it:\n", "\n", "http://docs.python.org/whatsnew/2.7.html#pep-372-adding-an-ordered-dictionary-to-collections" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### 4 ways to make a Dictionary ###" ] }, { "cell_type": "code", "collapsed": true, "input": [ "# number 1...you've seen this\n", "d = {\"favorite cat\": None, \"favorite spam\": \"all\"}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "# number 2\n", "d = dict(one = 1, two=2,cat = 'dog') ; print d" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'cat': 'dog', 'two': 2, 'one': 1}\n" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "# number 3 ... just start filling in items/keys\n", "d = {} # empty dictionary\n", "d['cat'] = 'dog'\n", "d['one'] = 1\n", "d['two'] = 2\n", "d" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 77, "text": [ "{'cat': 'dog', 'one': 1, 'two': 2}" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "# number 4... start with a list of tuples\n", "mylist = [(\"cat\",\"dog\"), (\"one\",1),(\"two\",2)]\n", "print dict(mylist)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'one': 1, 'two': 2, 'cat': 'dog'}\n" ] } ], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "dict(mylist) == d" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 79, "text": [ "True" ] } ], "prompt_number": 79 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Dictionaries: they can be complicated (in a good way) ####" ] }, { "cell_type": "code", "collapsed": true, "input": [ "d = {\"favorite cat\": None, \"favorite spam\": \"all\"}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'favorites': {'cat': None, 'spam': 'all'}, \\\n", " 'least favorite': {'cat': 'all', 'spam': None}}\n", "print d['least favorite']['cat']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "all\n" ] } ], "prompt_number": 81 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "remember: the backslash (\\) allows you to across break lines. Not technically needed when defining a dictionary or list" ] }, { "cell_type": "code", "collapsed": true, "input": [ "phone_numbers = {'family': [('mom','642-2322'),('dad','534-2311')],\\\n", " 'friends': [('Sylvia','652-2212')]}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "for group_type in ['friends','family']:\n", " print \"Group \" + group_type + \":\"\n", " for info in phone_numbers[group_type]:\n", " print \" \",info[0], info[1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Group friends:\n", " Sylvia 652-2212\n", "Group family:\n", " mom 642-2322\n", " dad 534-2311\n" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "# this will return a list, but you dont know in what order! \n", "phone_numbers.keys()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "['friends', 'family']" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "phone_numbers.values()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "[[('Sylvia', '652-2212')], [('mom', '642-2322'), ('dad', '534-2311')]]" ] } ], "prompt_number": 85 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "`.keys()` and `.values()`: are called `methods` on dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for group_type in phone_numbers.keys():\n", " print \"Group \" + group_type + \":\"\n", " for info in phone_numbers[group_type]:\n", " print \" \",info[0], info[1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Group friends:\n", " Sylvia 652-2212\n", "Group family:\n", " mom 642-2322\n", " dad 534-2311\n" ] } ], "prompt_number": 86 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "we cannot ensure ordering here of the groups" ] }, { "cell_type": "code", "collapsed": false, "input": [ "groups = phone_numbers.keys()\n", "groups.sort()\n", "for group_type in groups:\n", " print \"Group \" + group_type + \":\"\n", " for info in phone_numbers[group_type]:\n", " print \" \",info[0], info[1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Group family:\n", " mom 642-2322\n", " dad 534-2311\n", "Group friends:\n", " Sylvia 652-2212\n" ] } ], "prompt_number": 87 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`.iteritems()` is a handy method,\n", "returning key,value pairs with each iteration" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for group_type, vals in phone_numbers.iteritems():\n", " print \"Group \" + group_type + \":\"\n", " for info in vals:\n", " print \" \",info[0], info[1]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Group friends:\n", " Sylvia 652-2212\n", "Group family:\n", " mom 642-2322\n", " dad 534-2311\n" ] } ], "prompt_number": 88 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Some examples of getting values:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "phone_numbers['co-workers']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "KeyError", "evalue": "'co-workers'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mphone_numbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'co-workers'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'co-workers'" ] } ], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "phone_numbers.has_key('co-workers')" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "False" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "print phone_numbers.get('co-workers')" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "phone_numbers.get('friends') == phone_numbers['friends']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 92, "text": [ "True" ] } ], "prompt_number": 92 }, { "cell_type": "code", "collapsed": false, "input": [ "print phone_numbers.get('co-workers',\"all alone\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "all alone\n" ] } ], "prompt_number": 93 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### setting values ####\n", "\n", "you can edit the values of keys and also `.pop()` & `del` to remove certain keys" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# add to the friends list\n", "phone_numbers['friends'].append((\"Jeremy\",\"232-1121\"))\n", "print phone_numbers" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'friends': [('Sylvia', '652-2212'), ('Jeremy', '232-1121')], 'family': [('mom', '642-2322'), ('dad', '534-2311')]}\n" ] } ], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "## Sylvia's number changed\n", "phone_numbers['friends'][0][1] = \"532-1521\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m## Sylvia's number changed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mphone_numbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'friends'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"532-1521\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 95 }, { "cell_type": "code", "collapsed": true, "input": [ "phone_numbers['friends'][0] = (\"Sylvia\",\"232-1521\"); \n", "print phone_numbers['friends']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[('Sylvia', '232-1521'), ('Jeremy', '232-1121')]\n" ] } ], "prompt_number": 96 }, { "cell_type": "code", "collapsed": true, "input": [ "## I lost all my friends preparing for this Python class\n", "phone_numbers['friends'] = [] # sets this to an empty list" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 97 }, { "cell_type": "code", "collapsed": false, "input": [ "## remove the friends key altogether\n", "print phone_numbers.pop('friends')" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[]\n" ] } ], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "print phone_numbers" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'family': [('mom', '642-2322'), ('dad', '534-2311')]}\n" ] } ], "prompt_number": 99 }, { "cell_type": "code", "collapsed": true, "input": [ "del phone_numbers['family']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 100 }, { "cell_type": "code", "collapsed": false, "input": [ "print phone_numbers" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{}\n" ] } ], "prompt_number": 101 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "`.update()` method is very handy, like `.append()` for lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "phone_numbers.update({\"friends\": [(\"Sylvia's friend, Dave\", \"532-1521\")]})\n", "print phone_numbers" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'friends': [(\"Sylvia's friend, Dave\", '532-1521')]}\n" ] } ], "prompt_number": 102 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## List Comprehension ##\n", "\n", "You can create lists \"on the fly\" by asking simple questions of other iterateable data structures" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "example: I want a list of all numbers from 0 - 100 whose lowest two bits are both one (e.g., 3, 7, ...) but is not divisible by 11" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mylist = []\n", "for num in range(101):\n", " if (num & 2) and (num & 1) and (num % 11 != 0.0):\n", " mylist.append(num)\n", "print mylist" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[3, 7, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 59, 63, 67, 71, 75, 79, 83, 87, 91, 95]\n" ] } ], "prompt_number": 103 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist=[num for num in range(101) if (num & 2) \\\n", " and (num & 1) and (num % 11 != 0.0)]\n", "print mylist" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[3, 7, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 59, 63, 67, 71, 75, 79, 83, 87, 91, 95]\n" ] } ], "prompt_number": 104 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "example: I want a list of all mesons whose masses are between 100 and 1000 MeV" ] }, { "cell_type": "code", "collapsed": false, "input": [ "particles = \\\n", "[{\"name\":\"\u03c0+\" ,\"mass\": 139.57018}, {\"name\":\"\u03c00\" ,\"mass\": 134.9766}, \n", " {\"name\":\"\u03b75\" ,\"mass\": 47.853}, {\"name\":\"\u03b7\u2032(958)\",\"mass\": 957.78}, \n", " {\"name\":\"\u03b7c(1S)\", \"mass\": 2980.5}, {\"name\": \"\u03b7b(1S)\",\"mass\": 9388.9}, \n", " {\"name\":\"K+\", \"mass\": 493.677}, {\"name\":\"K0\" ,\"mass\": 497.614}, \n", " {\"name\":\"K0S\" ,\"mass\": 497.614}, {\"name\":\"K0L\" ,\"mass\": 497.614},\n", " {\"name\":\"D+\" ,\"mass\": 1869.62}, {\"name\":\"D0\" ,\"mass\": 1864.84},\n", " {\"name\":\"D+s\" ,\"mass\": 1968.49}, {\"name\":\"B+\" ,\"mass\": 5279.15},\n", " {\"name\":\"B0\" ,\"mass\": 5279.5}, {\"name\":\"B0s\" ,\"mass\": 5366.3},\n", " {\"name\":\"B+c\" ,\"mass\": 6277}]\n", "\n", "# data source: http://en.wikipedia.org/wiki/List_of_mesons\n", "\n", "my_mesons = [ (x['name'],x['mass']) for \\\n", " x in particles if x['mass'] <= 1000.0 and x['mass'] >= 100.0]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 105 }, { "cell_type": "code", "collapsed": false, "input": [ "# get the average\n", "tot = 0.0\n", "for x in my_mesons: tot += x[1]\n", "print \"The average meson mass in this range is \" + str(tot/len(my_mesons)) \\\n", " + \" MeV/c^2.\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The average meson mass in this range is 459.835111429 MeV/c^2.\n" ] } ], "prompt_number": 106 }, { "cell_type": "code", "collapsed": false, "input": [ "my_mesons[0][0]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 107, "text": [ "'\\xcf\\x80+'" ] } ], "prompt_number": 107 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_mesons[0][0]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u03c0+\n" ] } ], "prompt_number": 108 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "adapted from the UC Berkeley Python Bootcamp by J Bloom\n" ] } ], "metadata": {} } ] }