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.
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]
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
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
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller lisaholling1. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $3.64. You're not tied to anything after your purchase.