没有合适的资源?快使用搜索试试~ 我知道了~
python_for_economists.pdf
需积分: 9 0 下载量 54 浏览量
2021-04-04
14:09:50
上传
评论
收藏 663KB PDF 举报
温馨提示
给经济学家的 python 介绍
资源推荐
资源详情
资源评论
Contents
1 Introduction to Python 3
1.1 Getting Set-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Syntax and Basic Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Variables: What Stata Calls Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.5 Truth Value Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 Advanced Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.1 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.2 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.3 Dictionaries (also known as hash maps) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.4 Casting and a Recap of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 String Operators and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.1 Regular Expression Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.4.2 Regular Expression Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.4.3 Grouping RE’s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.4 Assertions: Non-Capturing Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.4.5 Portability of REs (REs in Stata) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5 Working with the Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.6 Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2 Applications 24
2.1 Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.1.1 Extraction from Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.1.2 Word Frequency Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.1.3 Soundex: Surname Matching by Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.4 Levenshtein’s “Edit Distance” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2 Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2.1 Using urllib2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.2.2 Logging-in with Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2.3 Making your Scripts Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2.4 Saving Binary Files on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.2.5 Chunking Large Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.6 Unzipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.7 Email Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.8 Crawling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.2.9 A Note on Privacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3 Extensions 35
2
1 INTRODUCTION TO PYTHON
3.1 Scripting ArcGIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1 Introduction to Python
I’ve been a student of three college classes that taught Python from scratch, but I’ve never seen a way of
teaching Python that I thought was appropriate for economists already familiar with scripting languages
such as Stata. I also believe economists are seeking something different from programming languages like
Python from what computer scientists look to do. It it not my intention to delve into scary computational
estimation methods, rather, I believe the programming flexibility that Python affords opens doors to
research projects that can’t be reached with Stata or SAS alone. Whenever possible, I present material
throughout the introduction in ways I believe are most useful when using Python to aid economic research.
The two applications of Python I have found most useful to this end are for text processing and web
scraping, as discussed in the second part of this tutorial. I hope you enjoy using Python as much as I do.
1.1 Getting Set-Up
Python is quite easy to download from its website, python.org. It runs on all operating systems, and comes
with IDLE by default. You probably want to download the latest version of Python 2; Python 3 works a
bit differently.
This tutorial was written for Python 2. Even if you’re interested Python 3 it’s sensible to do the tutorial in
Python 2 then have a look at the differences. By far the most salient difference that beginner should know
is that in Python 2, print is a statement whereas it is a function in Python 3. That means print ‘‘Hello
World’’ in Python 2 becomes print(‘‘Hello World’’) in Python 3.
1.2 Syntax and Basic Data Structures
Pythonese is surprisingly similar to English. In some ways, it’s even simpler than Stata – it may feel good
to ditch Stata’s “&” and “|” for “and” and “or.” You still need to use “==” to test for equality, so that
Python knows you’re not trying to make an assignment to a variable.
Unlike in Stata, indentation matters in Python. You need to indent code blocks, as you will see in
3
1.2 Syntax and Basic Data Structures 1 INTRODUCTION TO PYTHON
examples. Capitalization also matters. Anything on a line following a “#” is treated as a comment (the
equivalent of “//” in Stata).
You can use any text editor to write a Python script. My favorite is IDLE, Python’s Integrated
DeveLopment Environment. IDLE will usually help you with syntax problems such as forgetting to indent.
Unlike other text editors, IDLE also has the advantage of allowing you to run a script interactively with
just a keystroke as you’re writing it. The example code shown throughout the notes shows interactive uses
of Python with IDLE.
Just as you can run Stata interactively or as do-files, you can run Python interactively or as scripts. Just
as 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”).
1.2.1 Variables: What Stata Calls Macros
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. In practice,
global variables are rarely used, so we will not discuss them here.
As with Stata macros, you can assign both numbers and strings to Python variables.
>>> myNumber = 10
>>> p r in t myNumber
10
>>> myString = ” Hello , World ! ”
>>> p r i n t myString
’ Hel l o , World ! ’
>>> myString = 10 ## Python chan ges t he t ype o f th e v a r i a b l e f o r you on the f l y
>>> p r i n t myString
10
You can use either double or single quotation marks for strings, but the same string must be enclosed by
one or the other.
Task 1: Assign two variables to be numbers, and use the plus symbol to produce the sum of those
numbers. Now try subtraction and multiplication. What about division? What is 5/4? What about 5./4.?
How about float(5)/float(4), or int(5.0)/int(4.0)? If you enter data without a decimal point, Python
4
1.2 Syntax and Basic Data Structures 1 INTRODUCTION TO PYTHON
generally treats that as an integer, and truncates when dividing.
Task 2: Assign “Hello” to one variable and “World!” to another. Concatenate (combine) the two string
variables with the plus sign, just as you would add numbers. Doesn’t look right to you? Add in some white
space: var1 + “ ” + var2.
Task 3: What about multiplying a string? What is ‘-’*50?
1.2.2 Lists
Lists are another common data type in Python. To define a list, simply separate its entries by commas and
enclose 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 ith i t e ms 1 , 2 , and 3
>>> myList . append ( 4)
>>> myList = myList + [ 5 ]
>>> myList += [ 6 ] # t h i s i s a s h o rt c ut
>>> myList # here i s t he new l i s t ; i t e m s appear i n the o r d e r they were added
[ 1 , 2 , 3 , 4 , 5 , 6 ]
In the example above, we saw the syntax myList.append(..). In Python, we use objects, such as lists,
strings, or numbers. These objects have predefined methods that operate on them. The list object’s
append(..) method takes one parameter, the item to append.
Task 4: Define a list in which the items are the digits of your birthday.
Indexing 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 item i n myList
1
>>> l en ( myList ) # l en g t h o f myList
6
>>> myList [ 6 ] ## t h i s w i l l c r e a t e an e rr o r , shown below , with comments added
’ Traceback ( most r e c e nt c a l l l a s t ) : ’ # Python t e l l s me about what was hap pening
’ 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 elma t i c l i n e ( i n t h i s case , l i n e 29
5
剩余34页未读,继续阅读
资源评论
albertray
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功