math
modulepi
function of math
module gives the number of
math.pi
3.141592653589793
math
moduleTo use the compound function notation in Python:
math.log(math.e), math.cos(math.pi)
(1.0, -1.0)
math
moduleSuppose one wants to import specific function(s) from a module:
from math import log, sin
This means one could use sin(x)
and log(x)
directly without adding the math.
prefix.
math
moduleFinally, there is another notation we should pay attention to:
from math import *
This command does not introduce a specific function from math
package but imports _all_many functions.
print()
function is widely used to display information on the screen.print
in a more advanced and professional manner.For example, one could start a new line using the "slash n" notation:
print("Line one\nLine two")
Line one
Line two
Define a tab space by using the "slash t" notation:
print("Word one\tWord two")
Word one Word two
A more comprehensive example:
d = "Fourth line"
print("First line\n"+"Second line\n"+"Third line\n" +d)
First line
Second line
Third line
Fourth line
print("beta = %d, alpha = %f" % (10,2.56))
beta = 10, alpha = 2.560000
An overview of the commonly used format identifier with several examples:
-------------------------------------------------------------------
Format identifier Style Example
-------------------------------------------------------------------
%f floats 149597870700.000000
%d integers 149597870700
%s string "149597870700"
%e exponential notation 1.495979e+11
%g shorter of %e, %f 1.49598e+11
-------------------------------------------------------------------
It is worth noting that
%W.Df
means that a float should be printed with...W
characters and D
digits behind the decimal point:# 输出格式为float
print("Est. value = %f" % (12345.12345))
# 输出格式为integer
print("Est. value = %d" % (12345.12345))
# 输出格式为float,总计宽度8位,且小数点后1位
print("Est. value = %8.1f" % (12345.12345))
# 输出格式为float,总计宽度10位,且小数点后1位
print("Est. value = %10.1f" % (12345.12345))
# 输出格式为float,总计宽度15位,且小数点后3位
print("Est. value = %15.3f" % (12345.12345))
from math import pi
beta = 2.5613214
print(" beta = %5.3f \n pi = %5.2f \n and pi^4 = %e "
% (beta,pi,pi**4))
beta = 2.561
pi = 3.14
and pi^4 = 9.740909e+01
open()
allows us to access a file and then read or write some text to a file to a directory on our device.os.getcwd()
command to get the current working directory:import os # 引入os模块
cwd = os.getcwd() # 定义变量cwd,记录现在工作的路径
cwd
'/home/user'
Let us write some texts into a file named "example_text.txt"
and save it in the current working directory:
write_file = open(cwd+"/example_text.txt", "w")
write_file.write("The 1st line\n"+"The 2nd line\n"+"The 3rd line")
write_file.close()
read_file = open(cwd+"/example_text.txt", "r")
text = read_file.read()
read_file.close()
print(text)
The 1st line
The 2nd line
The 3rd line
Full list of the object types:
Python Tips :
Python的数据类型主要有:int(整数)、string(字符串)、float(浮点数)、complex(复数); bool(布尔型):值为True或False; list(列表):包含若干元素的序列,有序且可重复、可更改; tuple(元组):元组类似于列表,但不可更改; dictionary(字典):键值对的形式呈现,有序且不可重复、可更改; set(集合):集合类似于字典,但不可更改。
a = (10,20,30.56,"Hello")
print(a, type(a))
(10, 20, 30.56, 'Hello') <class 'tuple'>
When defining a tuple, the parentheses are not necessary. For instance,
a = 10,20,30.56,"Hello"
print(a, type(a))
(10, 20, 30.56, 'Hello') <class 'tuple'>
A list in Python is created using the squared brackets, for example:
a = [10,20,30.56,"Hello"]
print(a, type(a))
[10, 20, 30.56, 'Hello'] <class 'list'>
min
, max
and sum
of a Listmin()
, max()
, and sum()
functions to find the lowest, highest, and sum of values of the list, respectively.x = [0,1,2,3,4,5,10.85]
[min(x), max(x), sum(x)]
[0, 10.85, 25.85]
letters = ["a","b","c","d"]
[min(letters), max(letters)] # 输出list字母顺序的最小值与最大值
words = ["Python","Stata","R","Java"]
[min(words), max(words)]
# 输出list首字母顺序最小的string与首字母顺序最大的string
len()
function to count the number of characters.x = "Hello"
y = "my colleagues" # 定义变量y
len(x), len(y) # 输出变量长度,注意空格也算一个长度
(5, 13)
len()
function for a list,
print([x,y])
len([x,y])
['Hello', 'my colleagues]
2
+
operator can concatenate lists and tuples in order.a = [10,20,30]
b = [10,20,30,"Hello"]
c = [1,2,3]
print(a+b)
[10, 20, 30, 10, 20, 30, 'Hello']
The append()
attribute to a list could append another list to the end (not the beginning) of it as a single element (not elements):
a.append(b)
a
[10, 20, 30, [10, 20, 30, 'Hello']]
remove()
notation.a = [10,20,30,"Hello"]
a.remove(20)
a
[10, 30, 'Hello']
a[i]
. a[i]
returns the a
.a = [10,20,30,"Hello"]
a[0], a[1], a[2], a[3]
(10, 20, 30, 'Hello')
Python provides a useful method for backward indexing of lists or tuples. For example,
a = [10,20,30,"Hello"]
#分别输出倒数第一个、第二、第三个element
a[-1], a[-2], a[-3]
('Hello', 30, 20)
a.insert(i,content)
method allows us to add content
as a
.# 将指定的element插入列表索引为0的位置
a = [10,20,30,"Hello"]
a.insert(0,'IESR')
print("Add 'IESR' to position 0:", a)
# 将指定的element插入列表索引为1的位置
a = [10,20,30,"Hello"]
a.insert(1,'IESR')
print("Add 'IESR' to position 1:", a)
# 将指定的element插入列表索引为倒数1的位置
a = [10,20,30,"Hello"]
a.insert(-1,'IESR')
print("Add 'IESR' to position -1:", a)
Add 'IESR' to position 0: ['IESR', 10, 20, 30, 'Hello']
Add 'IESR' to position 1: [10, 'IESR', 20, 30, 'Hello']
Add 'IESR' to position -1: [10, 20, 30, 'IESR', 'Hello']
a[i]
locates a single a[i:j]
notation gives elements i
up to j-1
.a = [10,20,30,"Hello"]
a[0:3] # 输出list编号0,1,2三个element
[10, 20, 30]
Similarly, we can also slice any elements from a list.
print(a[0:2]) # 输出变量a中0,1编号的list
print(a[1:4]) # 输出变量a中1,2,3编号的list
[10, 20]
[20, 30, 'Hello']
As the starting index is 0
, it is equivalent to:
a[0:] # 输出变量a中第0到最后一个元素的list
[10, 20, 30, 'Hello']
a[:-1] # 输出变量a中0到倒数第二个元素的list
[10, 20, 30]
a = [10,20,30,"Hello"]
# 输出变量a中前三个元素
print(a[:3])
# 输出变量a中除第一个(通常计数观念)元素外的所有元素
print(a[1:])
[10, 20, 30]
[20, 30, 'Hello']
For the string variables, the slicing numbers correspond to the alphabetic order of letters. For example, the string variable slicing can be demonstrated as the following:
a= "Hello IESR Colleagues"
# 对于字符串,列表索引对应字符串中字符的顺序
print(a[0:3]) # 输出a中列表索引0,1,2三个字符
print(a[:3]) # 输出a中列表索引0,1,2三个字符
print(a[2:3]) # 输出a中列表索引的第2个字符
print(a[:]) # 输出a的全部字符
Hel
Hel
l
Hello IESR Colleagues
;
,
if-elif-else
statementsif-elif-else
is a decision control statement to
if
statement to
x
is strictly greater than zero.x = 10
if x > 0: # 如果变量x满足x>0
print("x is greater than 0")
x is greater than 0
if-elif-else
statementsif-else
statement evaluates the condition and will run the body of if
statement if the test condition is met.
x = 0
if x > 0: # 如果满足x>0条件
print("x is greater than 0")
else: # 如果不满足x>0条件
print("x is negative or 0")
x is negative or 0
if-elif-else
statementsx = -10
if x == 0: # 如果满足x=0条件
print("x is 0")
elif x > 0: # 如果满足x>0条件
print("x is greater than 0")
else: # 如果不满足以上任意两个条件
print("x is negative")
if-elif-else
statementsA further example:
x = 5
y = 10
z = 15
if x > y:
if x > z:
print("x is greater than y and z")
else:
print("x is greater than y but not greater than z")
elif y > z:
print("x is not greater than y, y is greater than z")
else:
print("z is the largest")
for
statementsfor
and while
loops are used for repeating a group of instructions.for
-loop is used for iterating over a sequence, such as a list or string.for i in ["Kitten","Cat","Feline"]:
print(i, end=", ")
for i in [0,1,2,3]: # 循环list当中的每个数字
print(i, end=", ")
for
statementsfor
-loopNext, we compute:
j = 0
for i in [0,1,2,3]:
j += i
print("i = %d, j = %d" % (i,j))
i = 0, j = 0
i = 1, j = 1
i = 2, j = 3
i = 3, j = 6
[0,1,2,3]
can be replaced with the range(0,4)
function.j = 0
for i in range(0,4):
j += i
print("i = %d, j = %d" % (i,j))
i = 0, j = 0
i = 1, j = 1
i = 2, j = 3
i = 3, j = 6
for
-loop, it is common to include an if
condition.cars = ['audi', 'bmw', 'benz', 'toyota']
for x in cars:
if x == "bmw": ”
print(x.upper(), end=", ")
else:
print(x.lower(), end=", ")
audi, BMW, benz, toyota,
Example demonstrates how to iteratively write texts to a file on our device:
import os
cwd = os.getcwd()
# 写入文件
write_file = open(cwd+"/example_iterative_text.txt", "w")
for i in range(1, 4):
write_file.write("%d x 5 = %d \n" % (i,i*5))
write_file.close()
# 读取文件内容
read_file = open(cwd+"/example_iterative_text.txt", "r")
text = read_file.read()
read_file.close()
print(text)
while
statementswhile
loop executes a block of statements repeatedly until a specific condition is met.x = 5
y = 0
while y < x:
# 循环判断变量y小于变量x,如满足则执行下一行
print(y, end = " ")
y += 1 # 变量y被循环加1重新赋值
print("Exit while-loop")
0 1 2 3 4 Exit while-loop
while
statementsConsider that we want to find out the duration for which 10000 yuan needs to be kept in a savings account to reach 20000 yuan only through annual interest payments at a rate of 1.75%.
amount = 10000 # 初始10000元人民币
rate = 0.0175 # 1.75%的年利率
year = 0
while amount < 20000: # 重复直至amount达到20000
amount = amount + amount*rate
year = year+1
print('We need', year , 'years to reach', amount , 'yuan RMB.')
We need 40 years to reach 20015.973431860315 yuan RMB.
break
and continue
statementsfor i in sequence:
# Code inside the for-loop
if condition:
break
# Will skip below and move to the code outside the for-loop
# Code inside the for-loop
# Code outside the for-loop
break
statementbreak
statement can stop a for
loop.for i in "ABCDEF":
if i == "D":
break
print(i)
print("The end")
A
B
C
The end
continue
statementcontinue
statement allows skipping the remaining code within a loop for the current iteration only.
for i in sequence:
# Code inside the for-loop
if condition:
continue
# Will GO BACK to previous code inside the for-loop
# Code inside the for-loop
# Code outside the for-loop
continue
statementfor i in "ABCDEF":
if i == "D":
continue
print(i)
print("The end")
A
B
C
E
F
The end
from math import log
log(1)
0
To define a Python function by ourselves, we use the def
keyword:
def function_name(arg1, arg2, ....., argn):
# 该函数执行的语句
return results
The Python interpreter finds the def
keyword and remembers this function_name
.
def square_values(x):
return x*x
square_values(10)
100
def upper_lower(s):
return s.upper(), s.lower()
sentence = 'python prograMMing fOr ecoNOmeTRics'
upper_lower(sentence)
('PYTHON PROGRAMMING FOR ECONOMETRICS', 'python programming for econometrics')
results = upper_lower(sentence)
print("The orginal sentence is `%s`. \nIts upper case is `%s`. \nIts lower case is `%s`." % (sentence,results[0],results[1]) )
The orginal sentence is `python prograMMing fOr ecoNOmeTRics`.
Its upper case is `PYTHON PROGRAMMING FOR ECONOMETRICS`.
Its lower case is `python programming for econometrics`.
We define a function to double the content of an object:
def double_k(k):
k = k+k
return k
k_string = "Hello"
double_k(k_string)
'HelloHello'
def double_values(k):
for i in range(len(k)):
k[i] = k[i]*2
print(k)
k_input = [0,1,2,10,20]
double_values(k_input)
[0, 1, 2, 10, 20]
[0, 2, 2, 10, 20]
[0, 2, 4, 10, 20]
[0, 2, 4, 20, 20]
[0, 2, 4, 20, 40]
import copy
def double_values_local1(k):
k_local = copy.copy(k)
for i in range(len(k)):
k_local[i] = k_local[i]*2
return k_local
k_input = [0,1,2,10,20]
double_values_local1(k_input), k_input
([0, 2, 4, 20, 40], [0, 1, 2, 10, 20])
One way to pass the global object into a function is by applying the global
command to ask the function to search for the global object.
def double_values_local2():
global k_input
k_local = copy.copy(k_input)
for i in range(len(k_input)):
k_local[i] = k_local[i]*2
return k_local
k_input = [0,1,2,10,20]
double_values_local2(), k_input
([0, 2, 4, 20, 40], [0, 1, 2, 10, 20])
multiply_m
j
and m
and computes m
has a default value of 3 by setting m=3
in the def
statement.j
and m
.def multiply_m(j, m=3):
print("%d * %d = %d" % (j,m,j*m))
multiply_m(5)
5 * 3 = 15
class ClassName:
<statement-1>
...
<statement-N>
class
statement must be executed before they have any effect.__init__()
: def __init__(self):
self.data = []
Consider a simple example of a class called First_class
:
class First_class:
def __init__(self, x1, x2):
# 该函数首个参数为self
self.r = x1
self.i = x2
x = First_class(x1=3.0, x2=-4.5)
x, x.r, x.i
(<__main__.First_class at 0x7ff7e4928280>, 3.0, -4.5)
class Puppy:
# 所以实例(instances)共用的Class变量
kind = 'Snoopy'
def __init__(self, name):
# 仅一个实例使用的实例变量
self.name = name
d = Puppy('Fido')
print(d.kind) # shared by all instances
print(d.name) # unique instance variable
Snoopy
Fido
In the below code, the function add_trick
is executed by d.add_trick('Piu')
command.
class Puppy:
kind = 'Snoopy'
def __init__(self, name):
self.name = name
self.tricks = []
def add_trick(self, trick):
self.tricks.append(trick)
d = Puppy('Fido')
d.add_trick('Piu')
d.tricks
['Piu']
map
, filter
and lambda
Consider the a for
-loop which multiplies each element in a list [0,1,2,3]
by 2:
y = [] # 定义一个空list
for x in range(4): # 循环x的值在0,1,2,3的范围
z = x*2 # 计算z的值
y.insert(x,z) # 将循环中每个x对应的z值插入list y当中
y
[0, 2, 4, 6]
In the list comprehension way:
[x*1 for x in range(10) if x >5]
# 在list中循环x的取值兵运行对应公式且判断是否满足x>5
[6, 7, 8, 9]
The list comprehension consists of an expression to implement (x*2
in the above case) followed by a for
clause and returns a list of results.
[x*1 for x in range(10) if x >5]
# 在list中循环x的取值兵运行对应公式且判断是否满足x>5
[6, 7, 8, 9]
If using a for
-loop, the above code can be more lengthy:
>>> y = []
>>> for x in range(10):
>>> if x > 5:
>>> y.append(x*1)
def
keyword defines a function with an assigned name,
lambda
) function** could help.f
with one parameter (def f(x): # 定义一个方程 f(x) 注意跟冒号
return(2*x)
f(1)
2
lambda
command translates the above code into a one-line Python style...lambda x: 2*x
<function __main__.<lambda>(x)>
To call this anonymous function:
(lambda x: 2*x)(1)
2
lambda
function.where
def f(x, y, z):
return(7*x + x**5 + 3*y + 6/z)
f(1,2,3)
16.0
The lambda
equivalent code can clarify the typical use of def
:
(lambda x,y,z: 7*x + x**5 + 3*y + 6/z)(1,2,3)
16.0
map(fun, seq)
function applies to a pre-defined function fun
to each element in a sequence seq
, and returns a list with the same length as seq
.def f(x):
return(2*x + x**2)
Now we would like to produce a list that contains the results of for
-loop coding style, this can be done by:
h = []
for x in range(0,11):
h.append(f(x))
h
[0, 3, 8, 15, 24, 35, 48, 63, 80, 99, 120]
Employing the map()
function, a one-line Python does the same job:
list(map(f, range(0,11)))
[0, 3, 8, 15, 24, 35, 48, 63, 80, 99, 120]
When using map
, it is often combined with the anonymous function lambda
such that the function is not needed to be pre-defined using def
.
list(map(lambda x: 2*x + x**2, range(0,11)))
[0, 3, 8, 15, 24, 35, 48, 63, 80, 99, 120]
filter(fun, seq)
function has similar arguments as map
.fun
entering filter
should return either True
or False
which are reserved names.def positive_checker(x):
if x > 0:
return(True)
else:
return(False)
positive_checker(1), positive_checker(-1)
(True, False)
filter
creates a new list populated with the results of calling the provided function on every element in a sequence.list(filter(positive_checker, range(-10,10,1)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
lambda
function, one avoids defining def
beforehand:list(filter(lambda x: x > 0, range(-10,10,1)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
pystata
Python package allows us to call Stata 17 (or later version) from Python.stata_setup
can help.pip install pystata
pip install stata_setup
Suppose Stata is installed in '/Applications/Stata/'
directory and the version is the Stata/MP:
import os
os.chdir('/Applications/Stata/utilities')
from pystata import config
config.init('mp')
___ ____ ____ ____ ____ ©
/__ / ____/ / ____/ 17.0
___/ / /___/ / /___/ MP—Parallel Edition
Statistics and Data Science Copyright 1985-2021 StataCorp LLC
StataCorp
4905 Lakeway Drive
College Station, Texas 77845 USA
800-STATA-PC https://www.stata.com
979-696-4600 stata@stata.com
The stata.run()
function under the pystata
module can be used to execute Stata commands.
from pystata import stata
# 运行一段Stata代码
stata.run(
'''clear all
sysuse auto
sum price''')
See what we can obtain:
. clear all
. sysuse auto
(1978 automobile data)
. sum price
Variable | Obs Mean Std. dev. Min Max
---------+----------------------------------------------
price | 74 6165.257 2949.496 3291 15906
r()
and e()
classes.stata.get_return()
, and stata.get_ereturn()
to store them as dictionary variables.stata.get_return()
{'r(N)': 74.0,
'r(sum_w)': 74.0,
'r(mean)': 6165.256756756757,
'r(Var)': 8699525.974268788,
'r(sd)': 2949.495884768919,
'r(min)': 3291.0,
'r(max)': 15906.0,
'r(sum)': 456229.0}