\n",
" \n",
" \n",
" \n",
"

\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "92e96b50",
"metadata": {},
"source": [
"# About Python\n",
"\n",
"> “Python has gotten sufficiently weapons grade that we don’t descend into R\n",
"> anymore. Sorry, R people. I used to be one of you but we no longer descend\n",
"> into R.” – Chris Wiggins"
]
},
{
"cell_type": "markdown",
"id": "d4ddc65f",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"In this lecture we will\n",
"\n",
"- outline what Python is \n",
"- compare it to some other languages \n",
"- showcase some of its abilities. \n",
"\n",
"\n",
"At this stage, it’s **not** our intention that you try to replicate all you see.\n",
"\n",
"We will work through what follows at a slow pace later in the lecture series.\n",
"\n",
"Our only objective for this lecture is to give you some feel of what Python is, and what it can do."
]
},
{
"cell_type": "markdown",
"id": "5ab06cd4",
"metadata": {},
"source": [
"## What’s Python?\n",
"\n",
"[Python](https://www.python.org) is a general-purpose programming language conceived in 1989 by Dutch programmer [Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum).\n",
"\n",
"Python is free and open source, with development coordinated through the [Python Software Foundation](https://www.python.org/psf/).\n",
"\n",
"Python has experienced rapid adoption in the last decade and is now one of the [most popular programming languages](https://www.tiobe.com/tiobe-index/)."
]
},
{
"cell_type": "markdown",
"id": "97921288",
"metadata": {},
"source": [
"### Common Uses\n",
"\n",
"Python is a general-purpose language used in almost all application domains such as\n",
"\n",
"- AI \n",
"- communication \n",
"- web development \n",
"- CGI and graphical user interfaces \n",
"- game development \n",
"- resource planning \n",
"- multimedia, data science, security, etc., etc., etc. \n",
"\n",
"\n",
"Used and supported extensively by Internet services and high-tech companies including\n",
"\n",
"- [Google](https://www.google.com/) \n",
"- [Netflix](https://www.netflix.com/) \n",
"- [Meta](https://opensource.fb.com/) \n",
"- [Dropbox](https://www.dropbox.com/) \n",
"- [Amazon](https://www.amazon.com/) \n",
"- [Reddit](https://www.reddit.com/) \n",
"\n",
"\n",
"For reasons we will discuss, Python is particularly popular within the scientific community\n",
"\n",
"Meanwhile, Python is also very beginner-friendly and is found to be suitable for\n",
"students learning programming and recommended to introduce computational methods\n",
"to students in fields other than computer science.\n",
"\n",
"Python is also replacing familiar tools like Excel as an essential skill in the fields of finance and banking."
]
},
{
"cell_type": "markdown",
"id": "9d715072",
"metadata": {},
"source": [
"### Relative Popularity\n",
"\n",
"The following chart, produced using Stack Overflow Trends, shows one measure of the relative popularity of Python\n",
"\n",
"![https://python-programming.quantecon.org/_static/lecture_specific/about_py/python_vs_matlab.png](https://python-programming.quantecon.org/_static/lecture_specific/about_py/python_vs_matlab.png)\n",
"\n",
" \n",
"The figure indicates not only that Python is widely used but also that adoption of Python has accelerated significantly since 2012.\n",
"\n",
"This is driven at least in part by uptake in the scientific domain, particularly in rapidly growing fields like data science and AI."
]
},
{
"cell_type": "markdown",
"id": "a4f8f563",
"metadata": {},
"source": [
"### Features\n",
"\n",
"Python is a [high-level language](https://en.wikipedia.org/wiki/High-level_programming_language) suitable for rapid development.\n",
"\n",
"It has a relatively small core language supported by many libraries.\n",
"\n",
"Other features of Python:\n",
"\n",
"- multiple programming styles are supported (procedural, object-oriented, functional, etc.) \n",
"- it is interpreted rather than compiled. "
]
},
{
"cell_type": "markdown",
"id": "f3fcb1b4",
"metadata": {},
"source": [
"### Syntax and Design\n",
"\n",
"\n",
"\n",
"One nice feature of Python is its elegant syntax — we’ll see many examples later on.\n",
"\n",
"Elegant code might sound superfluous but in fact it’s highly beneficial because it makes the syntax easy to read and easy to remember.\n",
"\n",
"Closely related to elegant syntax is an elegant design.\n",
"\n",
"Features like iterators, generators, decorators and list comprehensions make Python highly expressive, allowing you to get more done with less code.\n",
"\n",
"[Namespaces](https://en.wikipedia.org/wiki/Namespace) improve productivity by cutting down on bugs and syntax errors."
]
},
{
"cell_type": "markdown",
"id": "3a99896d",
"metadata": {},
"source": [
"## Scientific Programming\n",
"\n",
"\n",
"\n",
"Python has become one of the core languages of scientific computing.\n",
"\n",
"It’s either the dominant player or a major player in\n",
"\n",
"- AI, machine learning and data science \n",
"- astronomy \n",
"- chemistry \n",
"- computational biology \n",
"- meteorology \n",
"- natural language processing \n",
"- etc. \n",
"\n",
"\n",
"This section briefly showcases some examples of Python for scientific programming.\n",
"\n",
"- All of these topics below will be covered in detail later on. "
]
},
{
"cell_type": "markdown",
"id": "467f96dc",
"metadata": {},
"source": [
"### Numerical Programming\n",
"\n",
"\n",
"\n",
"Fundamental matrix and array processing capabilities are provided by the excellent [NumPy](http://www.numpy.org/) library.\n",
"\n",
"NumPy provides the basic array data type plus some simple processing operations.\n",
"\n",
"For example, let’s build some arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09322d6a",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np # Load the library\n",
"\n",
"a = np.linspace(-np.pi, np.pi, 100) # Create even grid from -π to π\n",
"b = np.cos(a) # Apply cosine to each element of a\n",
"c = np.sin(a) # Apply sin to each element of a"
]
},
{
"cell_type": "markdown",
"id": "a484be10",
"metadata": {},
"source": [
"Now let’s take the inner product"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72839c0d",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"b @ c"
]
},
{
"cell_type": "markdown",
"id": "0125651f",
"metadata": {},
"source": [
"The number you see here might vary slightly but it’s essentially zero.\n",
"\n",
"(For older versions of Python and NumPy you need to use the [np.dot](http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html) function)\n",
"\n",
"The [SciPy](http://www.scipy.org) library is built on top of NumPy and provides additional functionality.\n",
"\n",
"\n",
"\n",
"For example, let’s calculate $ \\int_{-2}^2 \\phi(z) dz $ where $ \\phi $ is the standard normal density."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "705a08e0",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"from scipy.stats import norm\n",
"from scipy.integrate import quad\n",
"\n",
"ϕ = norm()\n",
"value, error = quad(ϕ.pdf, -2, 2) # Integrate using Gaussian quadrature\n",
"value"
]
},
{
"cell_type": "markdown",
"id": "9827e2f8",
"metadata": {},
"source": [
"SciPy includes many of the standard routines used in\n",
"\n",
"- [linear algebra](http://docs.scipy.org/doc/scipy/reference/linalg.html) \n",
"- [integration](http://docs.scipy.org/doc/scipy/reference/integrate.html) \n",
"- [interpolation](http://docs.scipy.org/doc/scipy/reference/interpolate.html) \n",
"- [optimization](http://docs.scipy.org/doc/scipy/reference/optimize.html) \n",
"- [distributions and statistical techniques](http://docs.scipy.org/doc/scipy/reference/stats.html) \n",
"- [signal processing](http://docs.scipy.org/doc/scipy/reference/signal.html) \n",
"\n",
"\n",
"See them all [here](http://docs.scipy.org/doc/scipy/reference/index.html)."
]
},
{
"cell_type": "markdown",
"id": "30c0dacd",
"metadata": {},
"source": [
"### Graphics\n",
"\n",
"\n",
"\n",
"The most popular and comprehensive Python library for creating figures and graphs is [Matplotlib](http://matplotlib.org/), with functionality including\n",
"\n",
"- plots, histograms, contour images, 3D graphs, bar charts etc. \n",
"- output in many formats (PDF, PNG, EPS, etc.) \n",
"- LaTeX integration \n",
"\n",
"\n",
"Example 2D plot with embedded LaTeX annotations\n",
"\n",
"![https://python-programming.quantecon.org/_static/lecture_specific/about_py/qs.png](https://python-programming.quantecon.org/_static/lecture_specific/about_py/qs.png)\n",
"\n",
" \n",
"Example contour plot\n",
"\n",
"![https://python-programming.quantecon.org/_static/lecture_specific/about_py/bn_density1.png](https://python-programming.quantecon.org/_static/lecture_specific/about_py/bn_density1.png)\n",
"\n",
" \n",
"Example 3D plot\n",
"\n",
"![https://python-programming.quantecon.org/_static/lecture_specific/about_py/career_vf.png](https://python-programming.quantecon.org/_static/lecture_specific/about_py/career_vf.png)\n",
"\n",
" \n",
"More examples can be found in the [Matplotlib thumbnail gallery](https://matplotlib.org/stable/gallery/index.html).\n",
"\n",
"Other graphics libraries include\n",
"\n",
"- [Plotly](https://plot.ly/python/) \n",
"- [seaborn](https://seaborn.pydata.org/) — a high-level interface for matplotlib \n",
"- [Altair](https://altair-viz.github.io/) \n",
"- [Bokeh](http://bokeh.pydata.org/en/latest/) \n",
"\n",
"\n",
"You can visit the [Python Graph Gallery](https://www.python-graph-gallery.com/) for more example plots drawn using a variety of libraries."
]
},
{
"cell_type": "markdown",
"id": "847c8926",
"metadata": {},
"source": [
"### Networks and Graphs\n",
"\n",
"Python has many libraries for studying graphs.\n",
"\n",
"\n",
"\n",
"One well-known example is [NetworkX](http://networkx.github.io/).\n",
"Its features include, among many other things:\n",
"\n",
"- standard graph algorithms for analyzing networks \n",
"- plotting routines \n",
"\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc4c81e",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import networkx as nx\n",
"import matplotlib.pyplot as plt\n",
"np.random.seed(1234)\n",
"\n",
"# Generate a random graph\n",
"p = dict((i, (np.random.uniform(0, 1), np.random.uniform(0, 1)))\n",
" for i in range(200))\n",
"g = nx.random_geometric_graph(200, 0.12, pos=p)\n",
"pos = nx.get_node_attributes(g, 'pos')\n",
"\n",
"# Find node nearest the center point (0.5, 0.5)\n",
"dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in list(pos.values())]\n",
"ncenter = np.argmin(dists)\n",
"\n",
"# Plot graph, coloring by path length from central node\n",
"p = nx.single_source_shortest_path_length(g, ncenter)\n",
"plt.figure()\n",
"nx.draw_networkx_edges(g, pos, alpha=0.4)\n",
"nx.draw_networkx_nodes(g,\n",
" pos,\n",
" nodelist=list(p.keys()),\n",
" node_size=120, alpha=0.5,\n",
" node_color=list(p.values()),\n",
" cmap=plt.cm.jet_r)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "8770e519",
"metadata": {},
"source": [
"### Other Scientific Libraries\n",
"\n",
"Here’s a short list of more important scientific libraries for Python.\n",
"\n",
"- [SymPy](http://www.sympy.org/) for symbolic algebra, including limits, derivatives and integrals \n",
"- [pandas](http://pandas.pydata.org/) for data maniputation \n",
"- [statsmodels](http://statsmodels.sourceforge.net/) for statistical routines \n",
"- [scikit-learn](http://scikit-learn.org/) for machine learning \n",
"- [JAX](https://github.com/google/jax) for automatic differentiation, accelerated linear algebra and GPU computing \n",
"- [PyTorch](https://pytorch.org/) for deep learning \n",
"- [Keras](https://keras.io/) for machine learning \n",
"- [Pyro](https://pyro.ai/) and [PyStan](https://pystan.readthedocs.org/en/latest/) for Bayesian data analysis \n",
"- [lifelines](https://lifelines.readthedocs.io/en/latest/) for survival analysis \n",
"- [GeoPandas](https://geopandas.org/en/stable/) for spatial data analysis \n",
"- [Dask](https://docs.dask.org/en/stable/) for parallelization \n",
"- [Numba](http://numba.pydata.org/) for making Python run at the same speed as native machine code \n",
"- [CVXPY](https://www.cvxpy.org/) for convex optimization \n",
"- [PyTables](http://www.pytables.org) for managing large data sets \n",
"- [scikit-image](https://scikit-image.org/) and [OpenCV](https://opencv.org/) for processing and analysing image data \n",
"- [FLAML](https://mlflow.org/docs/latest/index.html) for automated machine learning and hyperparameter tuning \n",
"- [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) for extracting data from HTML and XML files \n",
"\n",
"\n",
"In this lecture series we will learn how to use many of these libraries for\n",
"scientific computing tasks in economics and finance."
]
}
],
"metadata": {
"date": 1718235142.1408877,
"filename": "about_py.md",
"kernelspec": {
"display_name": "Python",
"language": "python3",
"name": "python3"
},
"title": "About Python"
},
"nbformat": 4,
"nbformat_minor": 5
}