100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary Python courses programming for economists $3.68   Add to cart

Summary

Summary Python courses programming for economists

 56 views  3 purchases
  • Course
  • Institution

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.

Last document update: 3 year ago

Preview 3 out of 13  pages

  • February 8, 2021
  • February 8, 2021
  • 13
  • 2019/2020
  • Summary
avatar-seller
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

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

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

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.68. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

78252 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$3.68  3x  sold
  • (0)
  Add to cart