Module 1
Introduction to Python
Overview
This module builds a working Python toolkit from the ground up, with financial applications threaded throughout. Rather than teaching Python in the abstract, each notebook introduces new tools in the context of problems that matter for investment analysis: bond pricing, return distributions, and stock price data. By the end of the module, students are downloading real market data from Yahoo Finance, normalizing prices, and visualizing portfolio growth over time.
The progression moves from pure Python — arithmetic, variables, and functions — through NumPy for vectorized numerical computation, Matplotlib and Seaborn for visualization, and Pandas for tabular data. The final notebook ties everything together with yfinance, showing how to retrieve historical prices, distinguish between unadjusted and adjusted close prices, and apply custom functions to entire DataFrames.
Topics
Setting Up Python
- Environment options: Google Colab (browser-based, no installation) and Anaconda (recommended local setup with bundled libraries)
- Editor options: VS Code with the Jupyter extension, JupyterLab, or PyCharm
- Installing the course libraries (
pandas,numpy,scipy,statsmodels,matplotlib,seaborn,arch,yfinance) in an isolated conda environment
My First Jupyter Notebook
- The structure of a Jupyter notebook: code cells and Markdown cells
- Basic arithmetic and variable assignment; the distinction between
=(assignment) and==(equality) - Variable types: integers, floats, and strings; displaying output with
display()and automatic last-line evaluation
Defining Functions
- Defining reusable functions with
def, indentation, named arguments, andreturn - Computing the present value of an annuity:
pv_annuity(C, r, N)implementing the closed-form formula - Pricing a semi-annual coupon bond:
pv_bond(c, y, F, T)and interpreting why a bond with a coupon below its YTM trades at a discount
Introducing NumPy
- Creating numerical arrays with
np.array()and zero-based indexing - Vectorized operations: passing an array of yields to
pv_bondto price a bond across a range of rates without any loop - Useful functions:
np.log,np.exp,np.round,np.linspace
Plotting Functions
- The Matplotlib and Seaborn libraries:
sns.set_theme(), adding labels, titles, and legends to a figure - Plotting bond prices against yield-to-maturity for 5-year and 30-year bonds; interpreting the convexity of the price-yield relationship
- Visualizing simulated return distributions with
sns.histplotand a kernel density estimate; scatter plots for return co-movement
Working with Data
- Python lists and dictionaries as foundational data structures before introducing Pandas
- Pandas DataFrames: labeled tabular data, creating from a dictionary, and setting a custom index
- Summarizing with
describe(), filtering rows with boolean indexing, and selecting with.loc; plotting directly withdf.plot()
Downloading Financial Data
- The
yfinancelibrary:yf.download()withtickers,start, andauto_adjust - The distinction between
CloseandAdj Close: adjusted prices account for splits and dividends and are required for accurate return calculations - Defining a
normalize()function and applying it withdf.apply()to convert prices into cumulative returns; plotting MSFT’s Close vs. Adj Close to show the impact of dividends