This content is from the Scientific Python QuickStart by Thomas J. Sargent and John Stachurski, originally published as the JupyterBook example. We will be gradually adding more introductory Python materials by actuarial contributors. In the meantime, consider also referring to the Python Study Group microsite for materials from the 2021 YDAWG-organised sessions.
This is a short sequence of lectures on Python programming for scientific work, written by Thomas J. Sargent and John Stachurski.
These lectures were built using the new Sphinx-based Jupyter Book 2.0 tool set, as part of the ExecutableBookProject. They are intended mainly as a demonstration of these tools. Instructions for how to build them from source can be found in the Jupyter Book documentation.
These lectures are a mini-version of a longer series of lectures, also built using Jupyter Book 2.0. That series contains more features and applications but also requires more libraries and takes longer to run.
Both lecture series are taken from a more complete set of lectures on open source computing hosted by QuantEcon.
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 commonly used programming languages.
Popular textbooks on Python programming include [Gut16] and [Hil16].
Python is a general-purpose language used in almost all application domains such as
CGI and graphical user interfaces
multimedia, data processing, security, etc., etc., etc.
Python is beginner-friendly and routinely used to teach computer science and programming in top computer science programs.
Python is particularly popular within the scientific and data science communities.
It is steadily replacing familiar tools like Excel 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.
For example, the popularity of pandas, a library for data analysis with Python has exploded, as seen here.
(The corresponding time path for MATLAB is shown for comparison)
Note that pandas takes off in 2012, which is the same year that we see Python’s popularity begin to spike in the first figure.
Overall, it’s clear that
Python is one of the most popular programming languages worldwide.
Python is a major tool for scientific computing, accounting for a rapidly rising share of scientific work around the globe.
Python is a high-level language suitable for rapid development.
It has a relatively small core language supported by many libraries.
Multiple programming styles are supported (procedural, object-oriented, functional, etc.)
Python is interpreted rather than compiled.
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.
Remembering how to read from files, sort dictionaries and other such routine tasks means that you don’t need to break your flow in order to hunt down correct syntax.
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
Its popularity in economics is also beginning to rise.