Example: tourism industry

Python for Economists - Harvard University

Python for EconomistsAlex version: October you have not already done so, download the files for the exercises Introduction to Set-Up .. and Basic Data Structures .. : What Stata Calls Macros .. Value Testing .. Data Structures .. (also known as hash maps) .. and a Recap of Data Types .. Operators and Regular Expressions .. Expression Syntax .. Expression Methods .. RE s .. : Non-Capturing Groups .. of REs (REs in Stata) .. with the Operating System .. with Files .. 232 Processing .. from Word Documents .. Frequency Dictionaries .. : Surname Matching by Sounds .. s Edit Distance .. Scraping .. urllib2 .. with Cookies .. your Scripts Robust.

In most programming languages, including Python, the term \variable" refers to what Stata calls a \macro." Just like Stata has local and global macros, Python has global and local variables.

Tags:

  Programming, Python, Economists, Python for economists

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Python for Economists - Harvard University

1 Python for EconomistsAlex version: October you have not already done so, download the files for the exercises Introduction to Set-Up .. and Basic Data Structures .. : What Stata Calls Macros .. Value Testing .. Data Structures .. (also known as hash maps) .. and a Recap of Data Types .. Operators and Regular Expressions .. Expression Syntax .. Expression Methods .. RE s .. : Non-Capturing Groups .. of REs (REs in Stata) .. with the Operating System .. with Files .. 232 Processing .. from Word Documents .. Frequency Dictionaries .. : Surname Matching by Sounds .. s Edit Distance .. Scraping .. urllib2 .. with Cookies .. your Scripts Robust.

2 Binary Files on Windows .. Large Downloads .. Notifications .. Note on Privacy .. 343 Extensions3521 INTRODUCTION TO ArcGIS .. 351 Introduction to PythonI ve been a student of three college classes that taught Python from scratch, but I ve never seen a way ofteaching Python that I thought was appropriate for Economists already familiar with scripting languagessuch as Stata. I also believe Economists are seeking something different from programming languages likePython from what computer scientists look to do. It it not my intention to delve into scary computationalestimation methods, rather, I believe the programming flexibility that Python affords opens doors toresearch projects that can t be reached with Stata or SAS alone.

3 Whenever possible, I present materialthroughout the introduction in ways I believe are most useful when using Python to aid economic two applications of Python I have found most useful to this end are for text processing and webscraping, as discussed in the second part of this tutorial. I hope you enjoy using Python as much as I Getting Set-UpPython is quite easy to download from its website, It runs on all operating systems, and comeswith IDLE by default. You probably want to download the latest version of Python 2; Python 3 works abit tutorial was written for Python 2. Even if you re interested Python 3 it s sensible to do the tutorial inPython 2 then have a look at the differences. By far the most salient difference that beginner should knowis that in Python 2,printis a statement whereas it is a function in Python 3.

4 That meansprint HelloWorld in Python 2 becomesprint( Hello World )in Python Syntax and Basic Data StructuresPythonese is surprisingly similar to English. In some ways, it s even simpler than Stata it may feel goodto ditch Stata s & and | for and and or. You still need to use == to test for equality, so thatPython knows you re not trying to make an assignment to a in Stata, indentation matters in Python . You need to indent code blocks, as you will see Syntax and Basic Data Structures1 INTRODUCTION TO Python examples. Capitalization also matters. Anything on a line following a # is treated as a comment (theequivalent of // in Stata).You can use any text editor to write a Python script. My favorite is IDLE, Python s IntegratedDeveLopment Environment.

5 IDLE will usually help you with syntax problems such as forgetting to other text editors, IDLE also has the advantage of allowing you to run a script interactively withjust a keystroke as you re writing it. The example code shown throughout the notes shows interactive usesof Python with as you can run Stata interactively or as do-files, you can run Python interactively or as scripts. Justas you can run Stata graphically or in the command line, you can run Python graphically (through IDLE)or in the command line (the executable is Python ). Variables: What Stata Calls MacrosIn most programming languages, including Python , the term variable refers to what Stata calls a macro. Just like Stata has local and global macros, Python has global and local variables.

6 In practice,global variables are rarely used, so we will not discuss them with Stata macros, you can assign both numbers and strings to Python variables.>>>myNumber = 10>>>p r i n t myNumber10>>>m y S t r i n g = H e l l o , World ! >>>p r i n t m y S t r i n g H e l l o , World ! >>>m y S t r i n g = 10 ## Python c h a n g e s t h e t y p e o f t h e v a r i a b l e f o r you on t h e f l y>>>p r i n t m y S t r i n g10 You can use either double or single quotation marks for strings, but the same string must be enclosed byone or the 1:Assign two variables to be numbers, and use the plus symbol to produce the sum of thosenumbers. Now try subtraction and multiplication. What about division? What is 5/4? What about about float(5)/float(4), or int( )/int( )?

7 If you enter data without a decimal point, Syntax and Basic Data Structures1 INTRODUCTION TO Python generally treats that as an integer, and truncates when 2:Assign Hello to one variable and World! to another. Concatenate (combine) the two stringvariables with the plus sign, just as you would add numbers. Doesn t look right to you? Add in some whitespace:var1+ + 3:What about multiplying a string? What is - *50? ListsLists are another common data type in Python . To define a list, simply separate its entries by commas andenclose the entry list in square brackets. In the example below, we see a few ways to add items to a list.>>>myList = [ 1 , 2 , 3 ] # d e f i n e s new l i s t w i t h i t e m s 1 , 2 , and 3>>>myList.

8 Append ( 4 )>>>myList = myList + [ 5 ]>>>myList += [ 6 ] # t h i s i s a s h o r t c u t>>>myList # h e r e i s t h e new l i s t ; i t e m s a p p e a r i n t h e o r d e r t h e y were added[ 1 , 2 , 3 , 4 , 5 , 6 ]In the example above, we saw the syntax (..). In Python , we useobjects, such as lists,strings, or numbers. These objects have predefinedmethodsthat operate on them. Thelistobject sappend(..) method takes one parameter, the item to 4:Define a list in which the items are the digits of your into a list is simple if you remember that Python starts counting at 0.>>>myList[ 1 , 2 , 3 , 4 , 5 , 6 ]>>>myList [ 0 ] # f i r s t i t e m i n myList1>>>l e n ( myList ) # l e n g t h o f myList6>>>myList [ 6 ] ## t h i s w i l l c r e a t e an e r r o r , shown below , w i t h comments added T r a c e b a c k ( most r e c e n t c a l l l a s t ).

9 # Python t e l l s me a bo ut what was h a p p e n i n g F i l e <p y s h e l l 29> , l i n e 1 , i n<module> # The p r o b e l m a t i c l i n e ( i n t h i s c a s e , l i n e Syntax and Basic Data Structures1 INTRODUCTION TO Python # i n t h e Python i n t e r p r e t e r I had open ) myList [ 6 ] # The p r o b l e m a t i c command I n d e x E r r o r : l i s t i n d e x o u t o f r a n g e # a d e s c r i p t i o n o f t h e problem>>>myList [ 5 ] # oh t h a t was what I meant !6 Task 5:From the list you defined in the previous task, retrieve the first item. Use the len(..) function tofind out how long the list is. Now, retrieve the last 6:Lists can store any data structure as their items.

10 Make a list in which the first item is the name ofthe month of your birthday (a string, so enclosed in quotation marks), the second item is the day of themonth of your birthday (a number), and the last item is the year of your birthday (also a number).Task 7:Lists can even contain lists! Ask your neighbor what his or her birthday is. Make a list in whichthe first item is the list you declared in the previous task, and the second item is the list for yourneighbor s FunctionsFunctions are the equivalent of programs in Stata. A function definition starts withdef, then the functionname followed by parentheses. Any parameters the function takes in should be named in the colon follows the parentheses, and the rest of the function declaration is indented an extra level.


Related search queries