Python_Getting_Started - Atlas of Economic Models

Python: Getting Started

NB: anything prefixed with a $ and looking like code is a command to run at the command line.

Wherever possible we'll reuse existing material and will concentrate on material specific to economists. Works that we will refer to include:

Finally, remember that if you encounter any difficulties:

  1. search on the web.

  2. Add your comments here.


This is the absolute basics. See [2] for more details/help on installation.

  1. Install python.
    • On any linux distro this will be installed by default
    • On windows may need to download (and alter your default path as we will be using python from the command line a lot)
  2. Install some additional modules. The easiest way to do this is to use setuptools and easy_install (again on linux this would be much easier ...)
    • install setuptools by downloading and running it

    • install scipy:
      • $ easy_install scipy

    • install matplotlib -- warning this needs extra libraries than the basic python in order to render graphics (if you want to do more than simply write to disk)
      • debian/ubuntu $ apt-get install matplotlib

      • windows: $ easy_install matplotlib (then may need to do more to get graphics working)

Doing Something

Use [1], [2] or any other tutorial you can find to:

  1. Heavily recommend reading/working through sections 2-7 of [1] especially sections 3-5 (absolutely essential!)
  2. Write a function/method that given a list of integers computes (and returns):
    1. their sum
    2. their squared sum
    3. the sum of an arbitrary function f of each of their values (i.e. $$f(x_{1}) + f(x_{2}) + ...$$). f should obviously now be an additional argument to this function
  3. Download files and extract information
    1. Suggested file is:

      • This is from the Millenium Development Goals series and is: "Purchasing power parities (PPP) conversion factor, local currency unit to international dollar"
    2. Download using urllib.urlopen and urllib.urlretrieve
    3. Having downloaded the file extract its contents using python's zipfile library
    4. This is a csv file so now use python's csv library to read into a scipy/numpy array or a simply python list of lists
  4. Plot graphs using matplotlib
    1. e.g. $$y=x, y=x^{2}, y=cos(x)$$ etc)
    2. The PPP material downloaded in the previous step. (use bar chart from pylab/matplotlib)
  5. Using numpy/scipy: main tutorial is here

    1. Create a simple random matrix and compute its eigenvalues
    2. Plot the spectrogram of its eigenvalues.

CategoryGeneral CategoryPython