# ECON 873 Computational Tutorial 1¶

## What is Python¶

Python is an open source programming language that is used in a variety of applications. It is a fast growing language, used in many computer science programs. It is my personal preference for small programming applications. My reasons for introducing you to Python are:

• It is my language of choice, so I am better able to teach this language.
• I think it will look good on your resume.
• It is flexible, with many packages available for solving a wide variety of problems: matrix operations, statistics, numerical operations, graphing, etc.
• You will be able to transfer the skills you learn here to other languages (i.e. Matlab, R, Stata).

You should go to https://www.quant-econ.net and read Part 1: Programming in Python. It talks about the language and gives some simple examples. For those going on to a PhD, the lectures solve a host of common problems in quantitative economics.

By the end of this lecture note you should have:

• Installed Python
• Ran your first code in Python

First, go to https://store.continuum.io to get the Anaconda distribution of Python. On the left hand side, the second box has a download button for Anaconda. Click it. Now choose the correct version for your operating system. I use windows and chose the 32 bit option. Click on the link and the .exe file for the installer should download. It may take some time, but Python will be installed on your computer.

Next, you will have to figure out where Python installed on your computer. I find it helpful to go to the start menu and type in Ipython. You should find the Ipython consolea and the IPython Notebook. Select the notebook. It will open a web browser and a command prompt window. You want the web browser, which will show the IPython Dashboard. Select New Notebook in the top right. This should open up a new browser tab with a blank notebook. You are now ready to type your first pieces of Python Code.

Try adding 1 and 1. Be sure to hit SHIFT + ENTER.

In [1]:
1 + 1

Out[1]:
2

You may want to alternate between code cells and text cells like me. To do so you can choose Cell Type from the Cell drop down menu. Or you can type CTRL+M+M to select a Markdown cell. You can check all such keyboard shortcuts in the Help dropdown menu.

We can check that certain packages were installed correctly. For this course we will make use of NumPy, SciPy and Matplotlib. These are discussed in the Quant-Econ notes. This is how you import a package:

In [3]:
import numpy as np
from matplotlib import pyplot as plt


The first statement imports a package called numpy. We include the as np bit so that when we refer to numpy we don't have to type the whole name. For example, if we want to create sequnce of numbers we can use the np.linspace method as follows:

In [5]:
a_vector = np.linspace(0, 10, 5)
print 'This is what we created'
a_vector

This is what we created

Out[5]:
array([  0. ,   2.5,   5. ,   7.5,  10. ])

Suppose we want to read the documentation for the linspace method:

In [8]:
np.linspace?


matplotlib is a handy tool for creating plots. We first want to make sure the plots show up in the workbook. Next we will plot the DRS froduction function:

$$y = f(k) = k^\alpha$$

For $k\in\{0,5\}$ and $\alpha = 0.33$.

In [12]:
%matplotlib inline

alpha = 0.33

def f(k):
return k ** alpha

kgrid = np.linspace(0,5,80)

plt.plot(kgrid, f(kgrid), 'r-', linewidth = 4, label = 'Production Function')
plt.xlabel('k')
plt.ylabel('f(k)')
plt.title('The Neo-Classical Production Function')
plt.legend(loc ='upper left')
plt.show()


For your computational assignment I am going to ask you to send me the link to a webpage like the ones I am sending you. In order to do this:

• Go to File + Download As + IPython. This will give you a file that can be opened in notepad or word.
• Copy the contents and go to https:/https://gist.github.com/. Paste the contents in the white space and select Create Public Gist
• Go to https:/http://nbviewer.ipython.org/. Copy the URL from the gist webpage into the box and hit GO. This will publish your notebook. - You can then send the link to anyone to view. Even if they don't have Python.

Hopefully this has been relatively painless. If you have any problems getting Python installed or opening the IPython Notebook email me at [email protected] Look forward to seeing everyone next Wednesday.