没有合适的资源?快使用搜索试试~ 我知道了~
python_for_economists.pdf
需积分: 9 0 下载量 101 浏览量
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币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于单片机智能电子密码锁设计(proteus仿真+程序) (1)输入密码:通过4*4矩阵键盘输入6位密码; (2)修改密码:可以对初始密码进行修改; (3)显示电路:使用LCD1602显示密码锁运行状
- 微电网分层控制中二次控制,集中控制,分布式协调控制,事件触发,运行效果良好
- 三菱Q系列L系列程序 三菱L程序,主站L02PLC QX42.QY42P等输入输出模块.L系列定位控制模块 3C-FPC组装机 三菱JE系列伺服控制,绝对定位,X,Y,Z,R模组取
- MATLAB代码:计及碳捕集电厂灵活运行方式及需求响应的综合能源系统日前调度模型 仿真平台:MATLAB yalmip+cplex 包含新能源消纳、热电联产、电锅炉、储能电池、天然气、碳捕集CCS、计
- 新能源汽车车载双向OBC,PFC,LLC,V2G 双向 充电 新能源汽车车载双向OBC,PFC,LLC,V2G 双向 充电桩 电动汽车 车载充电机 充放电机 MATLAB仿真模型 (1)基于V2G技术
- 潮流追踪法,采用牛拉法计算任意拓扑结构系统网损,支路功率,考虑分布式电源接入情况,采用潮流追踪法计算负荷和分布式电源进行网损分摊
- 三相PWM整流器仿真模型 包括基于开关表的直接功率控制,滞环电流控制,有限集模型预测直接功率控制,有限集模型预测电流控制,均为输入三相对称交流电,220V 50Hz,直流侧输出760V,且直流输出电压
- 17 16届智能车十六届国二代码源程序,基础四轮摄像头循迹识别判断 逐飞tc264龙邱tc264都有 能过十字直角三岔路环岛元素均能识别,功能全部能实现 打包出的龙邱逐飞都有,代码移植行好,有基础的
- 西门子1500PLC程序 BMS系统 医药洁净室程序 串级PID 温度误差正负0.2(控温湿度强烈推荐) 程序有详细注释,很方便能看懂; 在运行医药厂房BMS PLC程序; 串级PID,分程调节,控
- 西门子224 XP程序源码,包括pcb,原理图 ,bom PLC 224 全套生产量产方案 非常具有参考价值
- matlab 图像分割gui可视化代码 ,代码功能有 图像灰度化,显示灰度直方图,阈值分割法,区域分割法,梯度边缘分割法,canny边缘分割,拉普拉斯边缘分割,并且可以进行各个方法的比较
- 电动汽车备用能力分析 对电动汽车备用能力的评估需置于合理的、计及用户响应意愿的市场机制下来考察 首先设计出兼顾系统调控需求与用户出行需求的充(放)电合约机制,提出了EV短时备用能力计算方法和响应电价
- Agv伺服驱动器方案开发,本人在AGV行业三年,有丰富的行业经验
- Comsol金属开口环倍频SHG转效率计算
- (断开git服务器合并本地两个分支代码)Git操作技巧:本地合并两个分支代码详细步骤与冲突解决方法
- 交错并联Boost PFC仿真电路模型,控制方法采用输出电压外环,电感电流内环的双闭环PI控制方式 控制效果:交流侧输入电流畸变小,波形良好,输出直流电压可完好跟随给定,两相电感电流均流很好,如展示
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功