1. About Python#
1.1. Overview#
In this lecture we will
outline what Python is
compare it to some other languages
showcase some of its abilities.
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.
1.2. What’s Python?#
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.
1.2.1. Common Uses#
Python is a general-purpose language used in almost all application domains such as
AI
communication
web development
CGI and graphical user interfaces
game development
resource planning
multimedia, data science, security, etc., etc., etc.
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.
1.2.2. Relative Popularity#
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.
1.2.3. Features#
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:
multiple programming styles are supported (procedural, object-oriented, functional, etc.)
it is interpreted rather than compiled.
1.2.4. Syntax and Design#
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.
1.3. Scientific Programming#
Python has become one of the core languages of scientific computing.
It’s either the dominant player or a major player in
AI, machine learning and data science
astronomy
chemistry
computational biology
meteorology
natural language processing
etc.
This section briefly showcases some examples of Python for scientific programming.
All of these topics below will be covered in detail later on.
1.3.1. Numerical Programming#
Fundamental matrix and array processing capabilities are provided by the excellent NumPy library.
NumPy provides the basic array data type plus some simple processing operations.
For example, let’s build some arrays
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
-1.4536982728685643e-15
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
0.9544997361036417
SciPy includes many of the standard routines used in
See them all here.
1.3.2. Graphics#
The most popular and comprehensive Python library for creating figures and graphs is Matplotlib, with functionality including
plots, histograms, contour images, 3D graphs, bar charts etc.
output in many formats (PDF, PNG, EPS, etc.)
LaTeX integration
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.
1.3.3. Other Scientific Libraries#
Here’s a short list of more important scientific libraries for Python.
SymPy for symbolic algebra, including limits, derivatives and integrals
pandas for data maniputation
statsmodels for statistical routines
scikit-learn for machine learning
JAX for automatic differentiation, accelerated linear algebra and GPU computing
PyTorch for deep learning
Keras for machine learning
lifelines for survival analysis
GeoPandas for spatial data analysis
Dask for parallelization
Numba for making Python run at the same speed as native machine code
CVXPY for convex optimization
PyTables for managing large data sets
scikit-image and OpenCV for processing and analysing image data
FLAML for automated machine learning and hyperparameter tuning
BeautifulSoup for extracting data from HTML and XML files