45
Introduction to Python 2 Chang Y. Chung Office of Population Research 01/14/2014

Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

  • Upload
    vanhanh

  • View
    228

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Introduction to Python 2

Chang Y. Chung

Office of Population Research

01/14/2014

Page 2: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Algorithms + Data Structures = Programs

I Niklaus Wirth (1976)[3]

I Python’s built-in data structures include:

B ListsB DictionariesB Tuples

I We will also briefly talk about:

B ClassesB Exception Handling

1 / 36

Page 3: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Algorithms + Data Structures = Programs

I Niklaus Wirth (1976)[3]

I Python’s built-in data structures include:

B ListsB DictionariesB Tuples

I We will also briefly talk about:

B ClassesB Exception Handling

1 / 36

Page 4: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Algorithms + Data Structures = Programs

I Niklaus Wirth (1976)[3]

I Python’s built-in data structures include:

B ListsB DictionariesB Tuples

I We will also briefly talk about:

B ClassesB Exception Handling

1 / 36

Page 5: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List

I Ordered (indexed) collection of arbitrary objects.

I Mutable – may be changed in place.

2 / 36

Page 6: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List

I Ordered collection of arbitrary objects.

1 L = [] # a new empty list2 L = l i s t ( ) # ditto3

4 L = [1 , 2.5 , "abc" , [56.7 , 78.9]]5 print len (L) # 46 print L[1] # 2.5 (zero-based)7 print L[3][0] # 56.78

9 for x in L :10 print x11 # 112 # 2.513 # "abc"14 # [56.7, 78.9]15

16 print "abc" in L , L . count ( "abc" ) , L . index( "abc" )17 # True 1 2

3 / 36

Page 7: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List

I Mutable – may be changed in place.

1 L = []2 L .append(5)3 print L # [5]4

5 L[0] = 236 print L # [23]7

8 M = [87, 999]9 L . extend(M) # or L += M

10 print L # [23, 87, 999]11

12 del L[2]13 print L # [23, 87]

4 / 36

Page 8: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List

I More examples.

1 def squares( a_ l i s t ) :2 s = [ ]3 for el in a_ l i s t :4 s .append( el ** 2)5 return s6

7 sq = squares([1 ,2 ,3 ,4])8 print sq , sum(sq)9 # [1, 4, 9, 16] 30

I Aliasing vs copying

1 L = [1 ,2 ,3 ,4]2 M = L # aliasing3 L[0] = 874 print M # [87, 2, 3, 4]5

6 L = [1 ,2 ,3 ,4]7 M = l i s t (L) # (shallow) copying. M = L[:] also works8 L[0] = 879 print M # [1,2,3,4]

5 / 36

Page 9: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List

I More examples.

1 def squares( a_ l i s t ) :2 s = [ ]3 for el in a_ l i s t :4 s .append( el ** 2)5 return s6

7 sq = squares([1 ,2 ,3 ,4])8 print sq , sum(sq)9 # [1, 4, 9, 16] 30

I Aliasing vs copying

1 L = [1 ,2 ,3 ,4]2 M = L # aliasing3 L[0] = 874 print M # [87, 2, 3, 4]5

6 L = [1 ,2 ,3 ,4]7 M = l i s t (L) # (shallow) copying. M = L[:] also works8 L[0] = 879 print M # [1,2,3,4]

5 / 36

Page 10: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Given a list,

1 L = [1 , 2, [3 , 4] , 5, "xyz" ]

evaluate the following expressions:

1 L[1] == 12 len (L) == 53 L[2] == 3, 44

5 [3] in L6 L . index( "xyz" ) == 47 L[-1] == "xyz"8 L[-1 ] [ -1] == "z"9

10 any([1 , 2, 3]) == True11 L[9] == None12 len ([0 ,1 ,2 ,]) == 3

6 / 36

Page 11: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Write a function that, given a list of integers, returns anew 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 oneappending only odd numbers into the new one. Returnthe new one.)

I An answer.

1 def only_odd( a_ l i s t ) :2 L = []3 for el in a_ l i s t :4 i f el % 2 == 1:5 L .append( el )6 return L7

8 print only_odd([0 , 1, 2, 3, 4])9 # [1, 3]

7 / 36

Page 12: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Write a function that, given a list of integers, returns anew 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 oneappending only odd numbers into the new one. Returnthe new one.)

I An answer.

1 def only_odd( a_ l i s t ) :2 L = []3 for el in a_ l i s t :4 i f el % 2 == 1:5 L .append( el )6 return L7

8 print only_odd([0 , 1, 2, 3, 4])9 # [1, 3]

7 / 36

Page 13: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz (cont.)

I (tricky) Write a function similar to the previous one. Thistime, however, do not return a new list. Just modify thegiven list so that it has only the odd numbers.(Hint: del L[0] removes the first element of the list, L)

8 / 36

Page 14: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Slice indexI Applies to any sequence types, including list , str, tuple, . . . .

I Has three (optional) parts separated by a colon (:),start : end : step, indicating start through but not past end, bystep; Indices point in-between the elements.

1 +−−−+−−−+−−−+−−−+−−−+−−−+2 | p | y | t | h | o | n |3 +−−−+−−−+−−−+−−−+−−−+−−−+4 0 1 2 3 4 5 65 −6 −5 −4 −3 −2 −1

I Examples:

1 L = [ "p" , "y" , " t " , "h" , "o" , "n" ]2 print L [ :2] # ["p", "y"] first two3 print L[1:3] # ["y", "t"]4 print L[0:5:2] # ["p", "t", "o"]5 print L[-1] # n the last element6 print L [ : ] # ["p", "y", "t", "h", "o", "n"] a (shallow) copy7 print L [3: ] # ["h", "o", "n"]8 print L[-2 : ] # ["o", "n"] last two9 print L [ : : -1] # ["n", "o", "h", "t", "y", "p"] reversed

9 / 36

Page 15: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Suppose that you collect friendship network data amongsix children, each of whom we identify with a number: 0,1, . . . , 5. The data are represented as a list of lists, whereeach element list represents the element child’s friends.

1 L = [[1 , 2] , [0 , 2, 3] , [0 , 1] , [1 , 4, 5] , [3 , 5] , [3]]

For instance, the kid 0 friends with the kids 1 and 2, sinceL[0] == [1, 2] Calculate the average number of friends thechildren have. (Hint: len() returns the list size.)

I An answer:

1 total = 0.0 # make total a float type2 for el in L :3 total += len ( el )4 avg = total / len (L)5 print avg6 # 2.1666

10 / 36

Page 16: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Suppose that you collect friendship network data amongsix children, each of whom we identify with a number: 0,1, . . . , 5. The data are represented as a list of lists, whereeach element list represents the element child’s friends.

1 L = [[1 , 2] , [0 , 2, 3] , [0 , 1] , [1 , 4, 5] , [3 , 5] , [3]]

For instance, the kid 0 friends with the kids 1 and 2, sinceL[0] == [1, 2] Calculate the average number of friends thechildren have. (Hint: len() returns the list size.)

I An answer:

1 total = 0.0 # make total a float type2 for el in L :3 total += len ( el )4 avg = total / len (L)5 print avg6 # 2.1666

10 / 36

Page 17: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz (cont.)

I (tricky)Write a function to check if all the friendshipchoices are reciprocated. It should take a list like previousone and return either True or False. (Hint: You may want touse a utility function below.)

1 def mutual( a_ l is t , ego, alter ) :2 return alter in a_ l i s t [ego] and ego in a_ l i s t [ alter ]

11 / 36

Page 18: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

List Comprehension

I A concise way to create a list. An example:

1 [x for x in range(5) i f x % 2 == 1] # [1, 3]

I An equivalent code using the for loop:

1 L = []2 for x in range(5) :3 i f x % 2 == 1:4 L .append(x) # [1, 3]

I More examples.

1 [x - 5 for x in range(6)] # [-5, -4, -3, -2, -1, 0]2 [abs(x) for x in [-2,-1 ,0 ,1]] # [2, 1, 0, 1]3 [x for x in range(6) i f x == x**2] # [0, 1]4 [1 for x in [87 , 999, "xyz" ] ] # [1, 1, 1]5 [x - y for x in range(2) for y in [7 , 8]] # [-7, -8, -6, -7]

12 / 36

Page 19: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Dictionary

I A collection of key-value pairs.

I Indexed by keys.

I Mutable.

I Also known as associative array, map, symbol table, . . .

I Usually implemented as a hash table.

13 / 36

Page 20: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Dictionary

I A collection of key-value pairs.

I Indexed by keys.

I Mutable.

I Also known as associative array, map, symbol table, . . .

I Usually implemented as a hash table.

13 / 36

Page 21: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Dictionary

I A collection of key-value pairs, indexed by keys.

1 D = {} # an empty dictionary. D=dict() also works2

3 D[ "one" ] = 1 # {"one": 1}4 D[ "two" ] = 25 print D # {"one": 1, "two": 2}6

7 print D. keys ( ) # ["two", "one"] arbitrary order!8 print "three" in D. keys ( ) # False. "three" in D also works9

10 D = {"Apple" : 116, "Big Mac" : 550}11

12 for key in [ "Apple" , "Orange" , "Big Mac" ] :13 i f key in D:14 value = D[key]15 print "{0} has {1} calories " . format (key , value )16 else :17 print "{0} is not found in the dictionary " . format (key)18 # Apple has 116 calories19 # Orange is not found in the dictionary20 # Big Mac has 550 calories

14 / 36

Page 22: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Dictionary

I More Dictionary examples.

1 D = {"China" : 1350, " India ":1221, "US":317}2 for key in D.keys ( ) :3 print "Pop of {0}: {1} mil " . format (key , D[key] )4 # Pop of India: 1221 mil5 # Pop of China: 1350 mil6 # Pop of US: 317 mil7

8 D = {[1 ,2]: 23}9 # TypeError: unhashable type: ’list’

10

11 D = {2: [2 , 3] , 200: [3 , 4] , 95: [4 , 5]} # OK12 print D[2] # [2, 3]13 print D[200] # [3, 4]

15 / 36

Page 23: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

A Data Structure

I SAT has three subsections: Critical Reading,Mathematics, and Writing. A result of taking an SAT examis three scores.

1 # data2 SAT = {"cr " :780, "m":790, "w":760}3 # usage4 print SAT[ "m" ] # 790

I You can take SAT exams more than once.

1 # data2 SATs = [{"cr " :780, "m":790, "w":760},3 {"cr " :800, "m":740, "w":790}]4 # usage5 print SATs[0] # {"cr":780, "m":790, "w":760}6 print SATs[0][ "cr " ] # 780

16 / 36

Page 24: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

A Data Structure

I SAT has three subsections: Critical Reading,Mathematics, and Writing. A result of taking an SAT examis three scores.

1 # data2 SAT = {"cr " :780, "m":790, "w":760}3 # usage4 print SAT[ "m" ] # 790

I You can take SAT exams more than once.

1 # data2 SATs = [{"cr " :780, "m":790, "w":760},3 {"cr " :800, "m":740, "w":790}]4 # usage5 print SATs[0] # {"cr":780, "m":790, "w":760}6 print SATs[0][ "cr " ] # 780

16 / 36

Page 25: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

More Complicated Data Structure

I Hypothetical SAT data for two people: Jane and Mary.

1 SAT = {"Jane" : {"lastname" : "Thompson" ,2 " test " : [{"cr " : 700, "m" : 690, "w":710}] },3 "Mary" : {"lastname" : "Smith" ,4 " test " : [{"cr " : 780, "m" : 790, "w":760},5 {"cr " : 800, "m" : 740, "w":790}] }}6

7 print SAT[ " Jane" ]8 # {"test": ["cr": 700, "m": 690, "w": 710], "lastname": "Thompson"}9

10 print SAT[ " Jane" ] [ "lastname" ] # Thompson11 print SAT[ " Jane" ] [ " test " ] # [{"cr": 700, "m": 690, "w":710}]12 print SAT[ " Jane" ] [ " test " ][0] # {"cr": 700, "m": 690, "w": 710}13 print SAT[ " Jane" ] [ " test " ] [0] [ " cr " ] # 70014

15 mary1 = SAT[ "Mary" ] [ " test " ][1]16 print mary1[ "cr " ] # 800

17 / 36

Page 26: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Make a dictionary of 2012 SAT percentile ranks for thescores from 660 to 700 and for all three subsections. Thefull table is available at http://tinyurl.com/k38xve8.Given this dictionary, say D, a lookup, D[660]["cr"] should beevaluated to 91.

I An answer.

1 D = {700: {"cr " : 95, "m" : 93, "w" : 96},2 690: {"cr " : 94, "m" : 92, "w" : 95},3 680: {"cr " : 93, "m" : 90, "w" : 94},4 670: {"cr " : 92, "m" : 89, "w" : 93},5 660: {"cr " : 91, "m" : 87, "w" : 92}}6

7 print D[660][ "cr " ] # 91

18 / 36

Page 27: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Make a dictionary of 2012 SAT percentile ranks for thescores from 660 to 700 and for all three subsections. Thefull table is available at http://tinyurl.com/k38xve8.Given this dictionary, say D, a lookup, D[660]["cr"] should beevaluated to 91.

I An answer.

1 D = {700: {"cr " : 95, "m" : 93, "w" : 96},2 690: {"cr " : 94, "m" : 92, "w" : 95},3 680: {"cr " : 93, "m" : 90, "w" : 94},4 670: {"cr " : 92, "m" : 89, "w" : 93},5 660: {"cr " : 91, "m" : 87, "w" : 92}}6

7 print D[660][ "cr " ] # 91

18 / 36

Page 28: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz (cont.)

I (tricky) Write a new dictionary DD such that we look upthe subsection first and then the score. That is, DD["cr"][660]

should be evaluated to 91.(Hint: Start with a dictionary below.):

1 DD = {"cr " : {}, "m" : {}, "w" : {}}

19 / 36

Page 29: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Tuples

I A sequence of values separated by commas.

I Immutable.

I Often automatically unpacked.

20 / 36

Page 30: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Tuples

I A sequence of values separated by commas. Immutable.

1 T = tuple ( ) # empty tuple. T = () works also2 N = (1) # not a tuple3 T = (1 , 2, "abc" ) # a tuple (1, 2, "abc")4 print T[0] # 15 T[0] = 9 # TypeError. immutable

I Often automatically unpacked.

1 T = (2 , 3)2 a, b = T # a is 2, b is 33 a, b = b, a # a and b swapped.4

5 D = {"x" : 23, "y" : 46}6 D. items ( ) # [("y", 46), ("x", 23)]7 for k , v in D. items ( ) :8 print "%s ==> %d" % (k , v) # y ==> 469 # x ==> 23

21 / 36

Page 31: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Class

I class defines a (user-defined) type, a grouping of somedata (properties) and functions that work on the data(methods).

I An object is an instance of a type.

I Examples:

B int is a type; 23 is an object.B str a type; "abc" an object.B "word document file" a type; "my_diary.docx" is an objectB We have been using objects.

22 / 36

Page 32: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Examples of Built-in Types

I The str type has a bunch of methods.

1 "abc" .upper ( ) # ABC2 "abc" . f ind ( "c" ) # 23 "abc" . sp l i t ( "b" ) # ["a", "c"]

I open() function returns a file object (representing anopened file).

1 with open( " test . txt " , "w" ) as my_file :2 my_file . write ( " f i r s t l ine \n" )3 my_file . write ( "second l ine \n" )4 my_file . write ( " third l ine " )5

6 print type( my_file ) # <type "file">7 print dir ( my_file ) # properties and methods8

9 my_file . write ( "something" ) # error. I/O on closed file

23 / 36

Page 33: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Class

I Let’s create a bank account type.

1 class BankAccount :2

3 def __ in i t __ ( self , in it ia l_balance=0):4 se l f . balance = init ia l_balance5

6 def deposit ( self , amount) :7 se l f . balance += amount8

9 def withdraw( self , amount) :10 se l f . balance -= amount

I Usage examples.

1 my_account = BankAccount(100)2 my_account . withdraw(5)3 print my_account . balance # 954

5 your_account = BankAccount ( )6 your_account . deposit(100)7 your_account . deposit (10)8 print your_account . balance # 110

24 / 36

Page 34: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Quiz

I Implement a Person type(or class) which has threeproperties (first_name, last_name, and birth_year); and twomethods: full_name() and age(). The age() method should takethe current year as an argument. You may use thetemplate below.

1 class Person :2 def __ in i t __ ( self , f i r s t , last , year ) :3 pass4 def full_name( sel f ) :5 pass6 def age( self , current_year ) :7 pass8

9 # check10 mr_park = Person( " Jae–sang" , "Park" , 1977)11 print mr_park . full_name ( ) # Jae–sang Park12 print mr_park .age(2014) # 37

25 / 36

Page 35: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Inheritance

I A mechanism for code reuse in object-orientedprogramming (OOP).

I A subtype is a specialized basetype.

1 import webbrowser2

3 class CoolPerson(Person ) :4 def __ in i t __ ( self , name, birth_year , video ) :5 Person . __ in i t __ ( self , name, None, birth_year )6 se l f . video = video7 def full_name( sel f ) :8 return sel f . first_name9 def show_off ( se l f ) :

10 url = "http : / /www.youtube .com/watch?v={0}"11 webbrowser .open( ur l . format ( se l f . video ) )12

13 # check14 psy = CoolPerson( "PSY" , 1977, "9bZkp7q19f0" )15 print psy . full_name ( ) # PSY16 print psy .age(2012) # 3517 psy . show_off ( ) # show off the style

26 / 36

Page 36: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Exception Handling

I An exception is raised when a (run-time) error occurs. Bydefault, the script stops running immediately.

1 L = [0 , 1, 2, 3]2 print L[5]3 # IndexError: list index out of range

I try: ... except: ... let us catch the exception and handle it.

1 L = [0 , 1, 2, 3]2 try :3 print L[5]4

5 except IndexError :6 print "no such element"7

8 print "next"9 # no such element

10 # next

27 / 36

Page 37: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Throwing Exception

I We can raise (or throw) an exception as well.

1 def fetch ( a_ l is t , index ) :2 i f index >= len ( a_ l i s t ) :3 raise IndexError ( "Uh, oh! " )4 return a_ l i s t [ index]5

6 print fetch (L , 5)7 # IndexError: Uh, oh!

I Script can keep going if you catch and handle theexception.

1 L = [0 , 1, 2, 3]2 try :3 print fetch (L , 5) # this raises an exception4 except IndexError :5 print "an exception occurred"6 print "next"7 # an exception occurred8 # next

28 / 36

Page 38: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

An Example

I urlopen() in urllib2 module raises an exception when the webpage is not found.

1 import ur l l ib22

3 L = [ "http : / / google .com" ,4 "http : / / google .com/ somethingfantastic" ,5 "http : / / yahoo.com" ]6

7 # we want to open each page in turn8 for ur l in L :9 try :

10 page = ur l l ib2 . urlopen ( ur l )11 print page.getcode ( )12 except ur l l ib2 . HTTPError :13 print " fai led to open: {0}" . format ( ur l )14

15 # 200 (a return code of 200 means OK)16 # failed to open: http://google.com/somethingfantastic17 # 200

29 / 36

Page 39: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

A Data Structure Usage Example

I STAN (http://mc-stan.org) is a C++ library / languageimplementing Markov chain Monte Carlo sampling (NUTS,HMC).

I STAN provides three application programming interfaces(or API’s): R, Python, and shell

I This is an example of using the Python API, which isprovided in a Python module, PyStan[1].

I In order to run this, you need to install: Cython(http://cython.org), NumPy (http://www.numpy.org),and STAN itself.

I From PyStan doc (http://tinyurl.com/olap8sx), fittingthe eight school model in Gelman et al. [2, sec 5.5].

30 / 36

Page 40: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Data Structure Usage Example (cont.)I Import PyStan module and put STAN code in a string.1 import pystan2 schools_code = """3 data {4 int<lower=0> J ; / / number of schools5 real y[ J ] ; / / estimated treatment effects6 real<lower=0> sigma[ J ] ; / / s .e . of effect estimates7 }8 parameters {9 real mu;

10 real<lower=0> tau ;11 real eta [ J ] ;12 }13 transformed parameters {14 real theta [ J ] ;15 for ( j in 1: J )16 theta [ j ] <- mu + tau * eta [ j ] ;17 }18 model {19 eta ~ normal(0 , 1);20 y ~ normal( theta , sigma) ;21 }22 """

31 / 36

Page 41: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Data Structure Usage Example (cont.)

I cont.

1 schools_data = {" J " : 8,2 "y" : [28 , 8, -3, 7, -1, 1, 18, 12] ,3 "sigma" : [15 , 10, 16, 11, 9, 11, 10, 18]}4

5 f i t = pystan . stan (model_code=schools_code ,6 data=schools_data , iter=1000, chains=4)7

8 la = f i t . extract (permuted=True)9 mu = la [ "mu" ]

10 # do something with mu here11

12 print str ( f i t ) # (nicely) print fit object13 f i t . plot ( ) # requires matplotlib

I Notice that:

B Input data are supplied in a dictionary.B stan() function in the module runs the model.B The function returns a fit type object, which has several

methods including extract() and plot().

32 / 36

Page 42: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Data Structure Usage Example (cont.)

I Output, in part

1 INFO: pystan :COMPILING THE C++ CODE FOR MODEL anon_model NOW.2 Inference for Stan model: anon_model .3 4 chains , each with iter=1000; warmup=500; thin=1;4 post−warmup draws per chain=500, total post−warmup draws=2...5

6 mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff . . .7 mu 7.8 0.2 5.1 −2.0 4.4 7.9 11.3 17.2 515.0.. .8 tau 6.4 0.3 5.4 0.4 2.6 5.1 8.6 20.5 362.09 eta[0] 0.4 0.0 0.9 −1.5 −0.2 0.4 1.0 2.2 597.0

10 eta[1] −0.0 0.0 0.9 −1.8 −0.6 −0.0 0.5 1.7 582.011 . . .12 theta [6] 10.4 0.3 6.9 −1.9 5.7 9.8 14.3 25.8 594.013 theta [7] 8.3 0.3 7.5 −6.2 3.7 8.0 12.7 25.0 604.014 lp__ −4.9 0.1 2.6−10.5 −6.5 −4.7 −3.2 −0.3 318.015

16 Samples were drawn using NUTS(diag_e ) at Thu Jan 9 17:53:17 For each parameter , n_eff is a crude measure of effective18 and Rhat is the potential scale reduction factor on sp l i t19 convergence , Rhat=1).

33 / 36

Page 43: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Data Structure Usage Example (cont.)

I Plots

34 / 36

Page 44: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

Summary

I List – An ordered collection of objects. Mutable.

I Dictionary – A collection of key-value pairs. Mutable.

I Tuple – A sequence of values separated by commas.Immutable.

I Class – Defines a type, a grouping of properties andmethods.

I try: ... except: ... – Catch and handle exceptions.

35 / 36

Page 45: Introduction to Python 2 - Office of Population Researchopr.princeton.edu/.../2014Jan_PythonPart2Chung.pdf · Introduction to Python 2 ... B Exception Handling 1/36. Algorithms +

References

Stan project team site.http://mc-stan.org/team.html.

Andrew Gelman, John B. Carlin, H. S. S. D. B. R.Bayesian Data Analysis, 2nd ed.Chapman & Hall/CRC Texts in Statistical Science. Chapman and Hall/CRC, July2003.

Wirth, N.Algorithms + Data Structures = Programs, 1st ed.Prentice Hall Series in Automatic Computation. Prentice-Hall, February 1976.

36 / 36