100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Python courses programming for economists

Rating
-
Sold
3
Pages
13
Uploaded on
08-02-2021
Written in
2019/2020

This is a summary of the "Python"-part of the course: Programming for economists given at Tilburg University. The documents contains a summary of the theory and codes needed to do the exercises in Python.

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Study
Course

Document information

Uploaded on
February 8, 2021
File latest updated on
February 8, 2021
Number of pages
13
Written in
2019/2020
Type
Summary

Subjects

Content preview

Programming for economists – Python courses
Introduction to Python

Types of variables

Int = integer: gehele getallen
Float = floating point: kommagetallen
Str = string: tekst -> “”
Bool = boolean: True or False

Check the type: type(variable)
Convert values into any type: str(), int(), etc.


Pyhton Lists
List = [[a, “a”], [b, “b”], [c, “c”]]

Subsetting lists
List = [0,1,2,3,4,5]
list[-1] : last element of the index = [5]
list[3:5] -> [3,4]  [start=inclusive:end=exclusive]

Slicing
list[3:]  [3,4,5]
list[:3]  [0,1,2]
print(list[1] + list[3])  4

list2 = [[1,2,3],[3,4,5],[6,7,8]]
x[2][:2]  [6,7]

Adding and removing elements
list_ext = list + [6,7]
del(list_ext[0]) #note: all indexes change immediatly after
list_ext[1]= “a” this call, so ‘1’ is now [0] and ‘2’ is [1]
 [1,a,3,4,5,6,7]

If you want to make a copy of list ‘list_copy’ and prevent that changes
in this list also affect ‘list’  list_copy = list(list)

Functions and packages for Python Lists
max(), min(), mean(), median(), len(), type()
round(number, ndigits) e.g. round(7.27,1)  7.3
complex([real[,imag]])
sorted(iterable, reverse = True)
 return a new sorted list from the items in iterable
(reverse=True, big -> small)
list.index(), list.count(), list.append(), list.remove(),
list.reverse()  reverses the order of the elements in the list
str.capitalize(), str.replace(), str.upper()
Numpy (Numeric Python)

,Import numpy as np
! Numpy arrays: contain only one type (int,floats, etc.)
! If you call: numpy_array + numpy_array it sums the firts element of
the first list with the first element of the second list
e.g. list = [1,2,3] ; np_array = np.array([1,2,3])
* list + list  [1,2,3,1,2,3]
* np_array + np_array  [2,4,6]

Numpy subsetting
np_array = np.array([1,2,3,4,5])
np_array > 2  array([False, False, True, True, True])
np_array[np_array>2]  array([3,4,5])

2D Numpy Arrays
Multiple rows/columns: np_2d.shape=4,2 means 4 rows w/ 2 elements each

np_2d[0][2] or np_2d[0,2] selects the third element of the first row
np_2d[:,1:3] selects the second and third element of every row
np_2d[1,:] selects only the entire second row

Basic statistics
np.mean(np_array)
np.median(np_array)
np.std(np_array)
np.corrcoef(np_array)

set an index to a np_array:
e.g. np_positions[‘GK’, ‘M’, ‘A’]
np_heights[191,184,185,…,179]

* gk_heights = np_heights[np_positions == ‘GK’]
This call prints the heights of the goalkeepers (GK)

* gk_heights = np_heights[np_positions != ‘GK’]
This call prints the heights of all other players then GK

, Intermediate Python

Matplotlib
Import matplotlib.pyplot as plt

plt.plot(x,y) ; plt.scatter()
plt.show()

put the x-axis on a log scale: plt.xscale(‘log’)

Histogram

plt.hist(x, bins=#, range=None, normed=False/True, weights=None)
plt.show()

plt.clf(): cleans up so that you can start fresh -> after plt.show()

Customization

plt.xlabel(‘’)
plt.ylabel(‘’)
plt.title(‘’)
plt.yticks([0,2,4])
plt.xticks([])

Add more data: y = [ , , ] + y ; x = [ , , ] + x

plt.scatter(x,y, s= np.array(), c = list, alpha = )
s = size (array)
c = colour (list/array)
alpha = transparancy

plt.grid(True)  gridlines are shown in the plot

Dictionaries in Python

create a dictionary: dict = { ‘’:{‘’: ,‘’: ,‘’:} }
data sorted in alphabetical order
if data belongs together europe = {’spain’:’madrid’, ‘france’:’paris’}
add data to dictionary: europe[‘italy’] = ‘rome’
dict[‘key’] = ‘value’  {‘key’:’value’}
check if it’s added: print(‘italy’in europe)-> True
delete: del(europe[‘australia’])

Dictionary of dictionaries:
europe = {‘spain’:{‘cap’:’madrid’,’pop’:46.77}, ‘france’:
{‘cap’:’paris’,’pop’:66.03}}

Create sub-dict data: (1) data = {‘capital’:’rome’, ‘population’:59,85}
(2) europe[‘italy’] = data

Select elements: europe[‘spain’][‘pop’]  46.77
$4.07
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached


Also available in package deal

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
lisaholling1 Tilburg University
Follow You need to be logged in order to follow users or courses
Sold
130
Member since
5 year
Number of followers
91
Documents
14
Last sold
4 weeks ago

4.3

14 reviews

5
9
4
2
3
2
2
0
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions