Skip to content

Python

Python is a programming language for expressing and testing quant calculations. Trace a USD cash-flow total and distinguish the language from scientific packages.

Python is a general-purpose programming language used to express calculations, organise data and automate tasks. In quantitative finance, it lets a researcher turn a valuation rule or trading policy into a program that can be rerun, inspected and tested. Its scientific packages extend the language with specialised numerical and data tools. The official tutorial introduces its syntax, dynamic typing and standard library.

A small calculation with a visible contract

Suppose three fictional cash movements settle on the same date in the same USD account: a receipt of USD 30, a payment of USD 10 and a receipt of USD 5. Positive means money received. We want the net cash movement, without discounting or currency conversion.
def net_cashflow(cashflows_usd: list[int]) -> int: """Total signed cash movements in whole USD.""" return sum(cashflows_usd) cashflows_usd = [30, -10, 5] net_usd = net_cashflow(cashflows_usd) print(f"Net cash movement: {net_usd} USD") # Net cash movement: 25 USD
The square brackets create a list. def defines a function; the indented body returns the result of Python's built-in sum. The inputs remain unchanged. This particular function has no hidden market-data lookup or other changing input: the same list of integers gives the same total.
The annotations list[int] and -> int describe the intended inputs and output for readers and tools; they do not validate values at runtime. The function also does not know what USD means. A list containing cash flows in different currencies would need validation and conversion before this calculation.
Fictional same-date USD cash movements: plus 30, minus 10 and plus 5 produce running totals of 30, 20 and 25; net cash movement is plus 25 USD.
Fictional same-date USD cash movements: plus 30, minus 10 and plus 5 produce running totals of 30, 20 and 25; net cash movement is plus 25 USD.
Each floating bar is one signed cash movement; its endpoint gives the running total. The final bar shows the net USD 25. Fictional same-date data; entry order is shown only to trace the arithmetic.

This is cash aggregation, not a profit or investment-return calculation. No initial investment, asset valuation or percentage denominator has been supplied.

The language and its packages do different jobs

LayerWhat it suppliesTypical quant use
Python language and built-insNames, expressions, branches, loops, functions, lists and sumState a rule and control how a calculation runs
Standard libraryModules distributed with Python, such as datetime, csv and decimalHandle dates, files and decimal arithmetic
NumPyMultidimensional arrays and numerical operationsWork with scenario grids and vectors
pandasLabelled tables and time-series operationsJoin observations by instrument or date
NumPy and pandas are third-party packages; the cash-flow example needs neither. Their official NumPy overview and pandas overview describe these different data contracts. Learning Python syntax is a foundation for using either package, but each package has its own rules for shapes, labels and missing values.

What makes a result reproducible

A successful execution tells you that this program ran on those inputs. It does not establish that the financial model, units or source data were appropriate.

Record the Python and package versions used for a research result, keep inputs available, and rerun the program from a clean start. A virtual environment separates a project's interpreter and installed packages from other projects; it does not by itself capture the data or every external dependency. Python's environment tutorial explains the installation boundary.
The example was executed with Python 3.14.4 and uses only built-ins. Its whole-dollar integers avoid decimal representation issues. Real calculations using floating-point arithmetic need suitable accuracy checks and rounding conventions; printing a rounded number does not make the stored value exact. See the Python floating-point tutorial.

Learn the next layer

  • Python Syntax and Control Flow traces expressions, branches and loops through a trading-signal example. The full lesson is free with a signed-in account; the public link shows its preview.
  • Scientific Python organises a model into inputs, transformations, computation and reporting, then adds arrays and numerical checks. Public preview; the full lesson requires membership.

Sources

Last updated
  • python
  • programming
  • cash flows
  • reproducibility
  • scientific computing
← Back to the glossaryBack to top ↑