{ "metadata": { "name": "", "signature": "sha256:ea627698a1f2a7ee66bcf95935af5f48651f5acf12c696987bbc7d7b20b4431b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Introduction to Python 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "May, 2014\n", "\n", "Chang Y. Chung" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Algorithms + Data Structures = Programs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Niklaus Wirth (1976)[3]\n", "\n", "![](files/graphics/wirth.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python's built-in data structures include:\n", "\n", "* Lists\n", "* Dictionaries\n", "* Tuples\n", " \n", "We will also briefly talk about:\n", "\n", "* Classes\n", "* Exception Handling" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Ordered (indexed) collection of arbitrary objects.\n", "* Mutable -- may be changed in place." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Ordered collection of arbitrary objects." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [] # an empty list\n", "L = list() # this works too\n", "\n", "L = [1, 2.5, \"abc\" , [56.7, 78.9]]\n", "print len(L) \n", "print L[1] \n", "print L[3][0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Iterating over list elements." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1, 2.5, \"abc\" , [56.7, 78.9]]\n", "for x in L:\n", " print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* List comes equipped with lots of methods." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"abc\" in L\n", "print L.count(\"abc\")\n", "print L.index(\"abc\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* List is mutable -- may be changed in place." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = []\n", "L.append(5)\n", "print L" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L[0] = 23\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [23]\n", "M = [87, 999]\n", "L.extend(M)\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "del L[2]\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Let's define a function that accepts a list as an argument." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def squares(a_list): \n", " s=[]\n", " for el in a_list:\n", " s.append(el ** 2) \n", " return s\n", " \n", "sq = squares([1, 2, 3, 4]) \n", "print sq, sum(sq)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Aliasing vs. copying" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1, 2, 3, 4]\n", "M = L # aliasing\n", "L[0] = 87\n", "print M" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1, 2, 3, 4]\n", "M = list(L) # (shallow) copying\n", "L[0] = 87\n", "print M" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1, 2, [3, 4], 5, \"xyz\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evalute the following expressions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L[1] == 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(L) == 5" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L[2] == 3, 4" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[3] in L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L.index(\"xyz\") == 4" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz (Cont.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [1, 2, [3, 4], 5, \"xyz\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evalute the following expressions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L[-1] == \"xyz\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L[-1][-1] == \"z\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "any([1,2,3]) == True" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L[9] == None" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len([0,1,2,]) == 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function that, given a list of integers, returns a _new_ list of odd numbers only. For instance, given the list, [0, 1, 2, 3, 4], this function should return a new list, [1, 3]. (Hint: Create a new empty list. Loop over the old one appending only odd numbers into the new one. Return the new one.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def only_odd(a_list):\n", " L = []\n", " for el in a_list:\n", " if el % 2 == 1:\n", " L.append(el)\n", " return L\n", "\n", "# check\n", "print only_odd([0, 1, 2, 3, 4])" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(tricky) Write a function similar to the previous one. This time, however, do not return a new list. Just modify the given list so that it has only the odd numbers. (Hint: del L[0] removes the first element of the list, L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an answer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def only_odd2(a_list):\n", " L = [] \n", " \n", " while len(a_list) > 0:\n", " el = a_list[0] \n", " if el % 2 == 1: \n", " L.append(el)\n", " del a_list[0] \n", " \n", " a_list.extend(L) \n", " \n", "# check\n", "L = [0, 1, 2, 3, 4]\n", "only_odd2(L)\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Here is another answer. Looping last-to-first element -- this way, we preserve the indices as they are, even we remove some elements." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def only_odd3(a_list):\n", " length = len(a_list)\n", " i = 1\n", " while i <= length:\n", " j = length - i\n", " if a_list[j] % 2 == 0:\n", " del a_list[j]\n", " i += 1\n", "\n", "# check\n", "L = [0, 1, 2, 3, 4]\n", "only_odd3(L)\n", "print L" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "In practive, I'd just create a new list with a comprehension." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [0, 1, 2, 3, 4]\n", "\n", "M = [x for x in L if x % 2 == 1]\n", "print M" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Slice Index" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "* Applies to any sequence types, including list, str, tuple, ...\n", "\n", "* Has three (optional) parts separated by a colon (:), start : end : step, indicating start through but not past end, by step; Indices point in-between the elements." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " +\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+ \n", " | p | y | t | h | o | n |\n", " +\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\u2212\u2212\u2212+\n", " 0 1 2 3 4 5 6\n", " \u22126 \u22125 \u22124 \u22123 \u22122 \u22121\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "List slicing Examples:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = ['p', 'y', 't', 'h', 'o', 'n']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[:2] # first two" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[1:3]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[0:5:2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[-1] # the last element" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "L = ['p', 'y', 't', 'h', 'o', 'n']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[:] # a (shallow) copy" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[3:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[-2:] # last two" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print L[::-1] # reversed" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Suppose that you collect friendship network data among six children, each of whom we identify with a number: 0, 1, ..., 5. The data are represented as a list of lists, where each element list represents the element child's friends." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [[1, 2], [0, 2, 3], [0, 1], [1, 4, 5], [3, 5], [3]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For instance, the kid 0 friends with the kids 1 and 2, since" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L[0] == [1, 2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the average number of friends the children have. (Hint: len() function returns the list size.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An answer:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# data again\n", "L = [[1, 2], [0, 2, 3], [0, 1], [1, 4, 5], [3, 5], [3]]\n", "\n", "total = 0.0 \n", "for el in L:\n", " total += len(el)\n", " \n", "avg = total / len(L)\n", "\n", "# check\n", "print avg" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "With a list comprehension, this can be as simple as:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 1.0 * sum([len(x) for x in L]) / len(L)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz (cont.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function to check if _all_ the friendship choices are reciprocated. It should take a list like previous one and return either True or False. (Hint: You may want to use a utility function below.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def mutual(a_list, ego, alter):\n", " return alter in a_list[ego] and ego in a_list[alter]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def all_reciprocated(a_list):\n", " for ego in range(len(L)):\n", " alters = L[ego]\n", " for alter in alters:\n", " if not mutual(a_list, ego, alter):\n", " return False\n", " return True\n", "\n", "# check\n", "L = [[1, 2], [0, 2, 3], [0, 1], [1, 4, 5], [3, 5], [3]]\n", "print all_reciprocated(L)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# another check\n", "L = [[1, 2], [0, 2, 3], [0, 1], [1, 4, 5], [3, 5], [3, 4]]\n", "print all_reciprocated(L)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "List Comprehension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A concise way to create a list. An example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [x for x in range(5) if x % 2 == 1]\n", "print L" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "An equivalent code using the for loop:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = []\n", "for x in range(5):\n", " if x % 2 == 1:\n", " L.append(x)\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "More list comprehension examples:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "[x - 5 for x in range(6)]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[abs(x) for x in [-2,-1,0,1]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[x for x in range(6) if x==x**2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[1 for x in [87, 999, \"xyz\"]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[x - y for x in range(2) for y in [7, 8]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* A collection of key-value pairs.\n", "* Indexed by keys.\n", "* Mutable.\n", "* Also known as associative array, map, symbol table, ...\n", "* Usually implemented as a hash table." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* A collection of key-value pairs, indexed by keys." ] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {} # an empty dict\n", "D = dict() # also works\n", "\n", "D[\"one\"]=1\n", "D[\"two\"]=2\n", "print D" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print D.keys()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"three\" in D.keys()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {\"Apple\": 116, \"Big Mac\": 550}\n", "\n", "for key in [\"Apple\", \"Orange\", \"Big Mac\"]:\n", " if key in D:\n", " print \"{0} has {1} calories\".format(key, D[key])\n", " else:\n", " print \"{0} is not found in the dictionary\".format(key)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "More Dictionary examples." ] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {\"China\": 1350, \"India\":1221, \"US\":317}\n", "\n", "for key in D.keys():\n", " print \"Pop of {0}: {1} mil\".format(key, D[key])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Mutables cannot be keys." ] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {[1,2]: 23}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Values can be almost of any type." ] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {2: [2, 3], 200: [3, 4], 95: [4, 5]}\n", "print D[2]\n", "print D[200]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Data Structure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SAT has three subsections: Critical Reading, Mathematics, and Writing. A result of taking an SAT exam is three scores." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# data\n", "SAT = {\"cr\": 780, \"m\": 790, \"w\": 760}" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# usage\n", "print SAT[\"m\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can take SAT exams more than once." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# data\n", "SAT = [{\"cr\": 780, \"m\": 790, \"w\": 760},\n", " {\"cr\": 800, \"m\": 740, \"w\": 790}]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# usage\n", "print SAT[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[0][\"cr\"]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "More Complicated Data Structure" ] }, { "cell_type": "code", "collapsed": false, "input": [ "SAT = {\"Jane\": {\"lastname\" : \"Thompson\",\n", " \"test\": [{\"cr\": 700, \"m\": 690, \"w\":710}] },\n", " \"Mary\": {\"lastname\" : \"Smith\",\n", " \"test\": [{\"cr\": 780, \"m\": 790, \"w\": 760},\n", " {\"cr\": 800, \"m\": 740, \"w\": 790}] } }" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"][\"lastname\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"][\"test\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "SAT = {\"Jane\": {\"lastname\" : \"Thompson\",\n", " \"test\": [{\"cr\": 700, \"m\": 690, \"w\":710}] },\n", " \"Mary\": {\"lastname\" : \"Smith\",\n", " \"test\": [{\"cr\": 780, \"m\": 790, \"w\": 760},\n", " {\"cr\": 800, \"m\": 740, \"w\": 790}] } }" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"][\"test\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"][\"test\"][0]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print SAT[\"Jane\"][\"test\"][0][\"cr\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "mary1 = SAT[\"Mary\"][\"test\"][1]\n", "print mary1[\"cr\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Make a dictionary of 2012 SAT percentile ranks for the scores from 660 to 700 and for all three subsections. The full table is available at [http://tinyurl.com/k38xve8](http://tinyurl.com/k38xve8).\n", "\n", "* Given this dictionary, say D, a lookup, D[660][\"cr\"] should be evaluated to 91." ] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {700: {\"cr\": 95, \"m\": 93, \"w\": 96}, \n", " 690: {\"cr\": 94, \"m\": 92, \"w\": 95}, \n", " 680: {\"cr\": 93, \"m\": 90, \"w\": 94}, \n", " 670: {\"cr\": 92, \"m\": 89, \"w\": 93}, \n", " 660: {\"cr\": 91, \"m\": 87, \"w\": 92}}\n", "\n", "# check\n", "print D[660][\"cr\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz (cont.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a new dictionary DD such that we look up the subsection first and then the score. That is, DD[\"cr\"][660] should be evaluated to 91. (Hint: Start with a dictionary below.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "DD = {\"cr\": {}, \"m\": {}, \"w\": {}}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for score in D:\n", " subjects = D[score]\n", " for subject in subjects: \n", " DD[subject][score] = subjects[subject]\n", "\n", "# check \n", "print DD[\"cr\"][660] " ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* A sequence of values separated by commas.\n", "* Immutable.\n", "* Often automatically _unpacked_." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* A sequence of values separated by commas. Immutable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "T = tuple()\n", "T = () # also works\n", "\n", "N = (1) # not a tuple\n", "T = (1, 2, \"abc\") # a tuple\n", "print T[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "T[0] = 9 # immutable" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Often automatically unpacked." ] }, { "cell_type": "code", "collapsed": false, "input": [ "T = (2, 3)\n", "a, b = T\n", "print a, b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a, b = b, a # swap\n", "print a, b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "D = {\"x\": 23, \"y\": 46}\n", "D.items() # returns a list" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for k, v in D.items():\n", " print \"%s ==> %d\" % (k, v)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Class defines a (user-defined) type, a grouping of some data (properties) and functions that work on the data (methods).\n", "\n", "* An object is an _instance_ of a type." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Examples:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - int is a type; 23 is an object.\n", " - str a type; \"abc\" an object.\n", " - \"word document file\" a type; \"my_diary.docx\" is an object.\n", " - We have been using objects." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Examples of Built-in Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`str` type has a bunch of methods." ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"abc\".upper()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"abc\".find(\"c\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"abc\".split(\"b\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* `open()` function returns a `file` object (representing an opened file)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"code/test.txt\", \"w\") as my_file:\n", " my_file.write(\"first line\\n\")\n", " my_file.write(\"second line\\n\")\n", " my_file.write(\"third line\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ " print type(my_file)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ " print dir(my_file)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_file.write(\"something\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Class" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class BankAccount:\n", " \n", " def __init__(self, initial_balance=0):\n", " self.balance = initial_balance\n", " \n", " def deposit(self, amount): \n", " self.balance += amount\n", " \n", " def withdraw(self, amount): \n", " self.balance -= amount" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_account = BankAccount(100)\n", "my_account.withdraw(5)\n", "print my_account.balance" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "your_account = BankAccount()\n", "your_account.deposit(100)\n", "your_account.deposit(10)\n", "print your_account.balance" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Implement a Person type (or class) which has three properties (first_name, last_name, and birth_year); and two methods: full_name() and age(). The age() method should take the current year as an argument. You may use the template below." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Person:\n", " \n", " def __init__(self, first_name, last_name, birth_year):\n", " pass\n", " \n", " def full_name(self):\n", " pass\n", " \n", " def age(self, current_year):\n", " pass" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "An answer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Person:\n", " \n", " def __init__(self, first_name, last_name, birth_year):\n", " self.first_name = first_name\n", " self.last_name = last_name\n", " self.birth_year = birth_year\n", " \n", " def full_name(self):\n", " return self.first_name + \" \" + self.last_name\n", " \n", " def age(self, current_year):\n", " return current_year - self.birth_year" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "mr_park = Person(\"jae-sang\", \"Park\", 1977)\n", "print mr_park.full_name()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print mr_park.age(2014)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Inheritance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* A subtype is more specialized basetype." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import webbrowser\n", "\n", "class CoolPerson(Person):\n", " \n", " def __init__(self, name, birth_year, video):\n", " Person.__init__(self, name, None, birth_year)\n", " self.video = video\n", " \n", " def full_name(self):\n", " return self.first_name\n", " \n", " def show_off(self):\n", " url = \"http://www.youtube.com/watch?v={0}\"\n", " webbrowser.open(url.format(self.video))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "psy = CoolPerson(\"PSY\", 1977, \"9bZkp7q19f0\")\n", "print psy.full_name()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print psy.age(2012)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# uncomment the line below and run (Shift+Enter) to see the style. \n", "# psy.show_off()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Exception Handling" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* An exception is raised when a (run-time) error occurs. By default, the script stops running immediately." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [0, 1, 2, 3]\n", "print L[5]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "`try`: ... `except`: ... let us catch the exception and handle it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [0, 1, 2, 3]\n", "\n", "try:\n", " print L[5]\n", " \n", "except IndexError:\n", " print \"no such element\"\n", " \n", "print \"next\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Throwing Exception" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can `raise` (or throw) an exception as well." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def fetch(a_list, index):\n", " if index >= len(a_list):\n", " raise IndexError(\"Uh, oh!\")\n", " return a_list[index]\n", "\n", "print fetch(L, 5)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Script can keep going if you catch and handle the exception." ] }, { "cell_type": "code", "collapsed": false, "input": [ "L = [0, 1, 2, 3]\n", "\n", "try:\n", " print fetch(L, 5)\n", " \n", "except IndexError:\n", " print \"an exception occurred\"\n", " \n", "print \"next\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Another Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ulropen()` in `urllib2` module raises an exception when the web page is not found." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import urllib2\n", "\n", "L = [\"http://yahoo.com\",\n", " \"http://somethingfantastic\",\n", " \"http://bing.com\"]\n", "\n", "# we want to open each page in turn\n", "for url in L:\n", " try:\n", " page = urllib2.urlopen(url, None, 1)\n", " print page.getcode()\n", " except:\n", " print \"failed to open: {0}\".format(url)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "A Data Structure Usage Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* STAN ([http://mc-stan.org](http://mc-stan.org)) is a C++ library / language implementing Markov chain Monte Carlo sampling (NUTS, HMC).\n", "\n", "* STAN provides three interfaces (or API's): R, Python, and shell\n", "\n", "* This is an example of using the Python API, which is provided in a Python module, PyStan [1].\n", "\n", "* In order to run this, you need to install: Cython ([http://cython.org](http://cython.org)), NumPy ([http://www.numpy.org](http://www.numpy.org)), and STAN itself.\n", "\n", "* From PyStan doc ([http://tinyurl.com/olap8sx](http://tinyurl.com/olap8sx), fiting the eight school model in Gelman et al. [2, sec 5.5]" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Data Structure Usage Example (cont.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import PyStan module and put STAN code in a string." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pystan\n", "import matplotlib\n", "\n", "schools_code = \"\"\"\n", "data {\n", " int