“Python has gotten sufficiently weapons grade that we don’t descend into R anymore. Sorry, R people. I used to be one of you but we no longer descend into R.” – Chris Wiggins
In this lecture we will
At this stage, it’s not our intention that you try to replicate all you see.
We will work through what follows at a slow pace later in the lecture series.
Our only objective for this lecture is to give you some feel of what Python is, and what it can do.
Python is a general-purpose programming language conceived in 1989 by Dutch programmer Guido van Rossum.
Python is free and open source, with development coordinated through the Python Software Foundation.
Python has experienced rapid adoption in the last decade and is now one of the most popular programming languages.
Python is a general-purpose language used in almost all application domains such as
Used and supported extensively by Internet services and high-tech companies including
For reasons we will discuss, Python is particularly popular within the scientific community
Meanwhile, Python is also very beginner-friendly and is found to be suitable for students learning programming and recommended to introduce computational methods to students in fields other than computer science.
Python is also replacing familiar tools like Excel as an essential skill in the fields of finance and banking.
The following chart, produced using Stack Overflow Trends, shows one measure of the relative popularity of Python
The figure indicates not only that Python is widely used but also that adoption of Python has accelerated significantly since 2012.
This is driven at least in part by uptake in the scientific domain, particularly in rapidly growing fields like data science and AI.
Python is a high-level language suitable for rapid development.
It has a relatively small core language supported by many libraries.
Other features of Python:
One nice feature of Python is its elegant syntax — we’ll see many examples later on.
Elegant code might sound superfluous but in fact it’s highly beneficial because it makes the syntax easy to read and easy to remember.
Closely related to elegant syntax is an elegant design.
Features like iterators, generators, decorators and list comprehensions make Python highly expressive, allowing you to get more done with less code.
Namespaces improve productivity by cutting down on bugs and syntax errors.
Python has become one of the core languages of scientific computing.
It’s either the dominant player or a major player in
This section briefly showcases some examples of Python for scientific programming.
import numpy as np # Load the library
a = np.linspace(-np.pi, np.pi, 100) # Create even grid from -π to π
b = np.cos(a) # Apply cosine to each element of a
c = np.sin(a) # Apply sin to each element of a
Now let’s take the inner product
b @ c
The number you see here might vary slightly but it’s essentially zero.
(For older versions of Python and NumPy you need to use the np.dot function)
The SciPy library is built on top of NumPy and provides additional functionality.
For example, let’s calculate $ \int_{-2}^2 \phi(z) dz $ where $ \phi $ is the standard normal density.
from scipy.stats import norm
from scipy.integrate import quad
ϕ = norm()
value, error = quad(ϕ.pdf, -2, 2) # Integrate using Gaussian quadrature
value
SciPy includes many of the standard routines used in
See them all here.
The most popular and comprehensive Python library for creating figures and graphs is Matplotlib, with functionality including
Example 2D plot with embedded LaTeX annotations
Example contour plot
Example 3D plot
More examples can be found in the Matplotlib thumbnail gallery.
Other graphics libraries include
You can visit the Python Graph Gallery for more example plots drawn using a variety of libraries.
Python has many libraries for studying graphs.
One well-known example is NetworkX. Its features include, among many other things:
Here’s some example code that generates and plots a random graph, with node color determined by the shortest path length from a central node.
%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10,6)
np.random.seed(1234)
# Generate a random graph
p = dict((i, (np.random.uniform(0, 1), np.random.uniform(0, 1)))
for i in range(200))
g = nx.random_geometric_graph(200, 0.12, pos=p)
pos = nx.get_node_attributes(g, 'pos')
# Find node nearest the center point (0.5, 0.5)
dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in list(pos.values())]
ncenter = np.argmin(dists)
# Plot graph, coloring by path length from central node
p = nx.single_source_shortest_path_length(g, ncenter)
plt.figure()
nx.draw_networkx_edges(g, pos, alpha=0.4)
nx.draw_networkx_nodes(g,
pos,
nodelist=list(p.keys()),
node_size=120, alpha=0.5,
node_color=list(p.values()),
cmap=plt.cm.jet_r)
plt.show()
Here’s a short list of more important scientific libraries for Python.
In this lecture series we will learn how to use many of these libraries for scientific computing tasks in economics and finance.