Module 1 · Getting Started with Python - Chapter 01

Why Python for Finance

Why the world's trading desks run on Python - and the surprising reason a 'slow' language is actually fast where it counts.

PY
What you'll learn
  • ·Why traders learn Python
  • ·Is Python really slow?
  • ·Python as glue: C & Rust cores
  • ·NumPy vs pure Python (30x)
  • ·Who uses Python on the Street
  • ·How this course works

Open any job posting from a hedge fund, a proprietary trading desk, or a fintech startup, and one word keeps appearing: Python. Not C++, not Java, not some secret in-house language - plain, readable Python, the same language taught in school computer labs. If you've ever wondered why the most sophisticated trading shops on earth chose a language a beginner can read aloud, this chapter is your answer. And by the end of it you'll understand the single idea that makes Python both easy and fast - a combination that sounds impossible until you see how it works.

Let's start with the objection you've probably already heard.

But isn't Python slow?

Yes - and no. The honest answer is the most important idea in this whole course, so let's get it exactly right.

Pure Python is slow. When Python runs a loop, it handles one value at a time, interpreting each step as it goes. For heavy number-crunching - millions of prices, thousands of options - that one-at-a-time style genuinely would be too slow for serious work.

Here's the twist: you almost never do the heavy maths in pure Python. Instead you call a library - a ready-made toolkit - whose number-crunching core is written in a fast, compiled language like C or Rust and bolted on underneath Python. You write a few friendly lines on top; the real work happens in the fast engine below. You get the readability of Python and the speed of C at the same time.

Think of driving a car. You turn a light steering wheel and press a pedal - that's Python, simple and human. Under the bonnet sits a powerful engine you never touch directly - that's the compiled core. You'd never try to steer by reaching in and turning the crankshaft by hand. Python is the steering wheel; C and Rust are the engine.

You write: Python easy to read, quick to write calls Fast compiled cores - the engine NumPy (C) - pandas (C) - TA-Lib (C) TensorFlow (C++) - OpenAlgo (Rust) - OpenGreeks (Rust)
Python is the steering wheel; C and Rust are the engine.

Almost every tool you'll meet in finance follows this exact pattern:

  • NumPy - arrays and fast maths, with a core written in C
  • pandas - tables and time series, built on that same C core
  • TA-Lib - technical indicators, in C
  • TensorFlow - deep learning, in C++ and your graphics card
  • OpenAlgo - the order-and-data engine many Indian traders use, with a Rust core
  • OpenGreeks - option Greeks at high speed, also in Rust

You write Python. They do the heavy lifting at near-native speed. Don't take my word for it - let's put them in a race.

Proof: the same maths, about 30 times faster

We'll add up the squares of five million numbers two ways: once with a plain Python loop, and once with NumPy. Identical maths, identical answer - watch the clock.

EX 1The same sum, two ways - and one compiled core winsPYch01/01_python_vs_numpy.py
# Is Python slow? Let's settle it with a stopwatch.
# We add up the squares of 5 million numbers two ways and time each.
import time

import numpy as np

N = 5_000_000

# Way 1: pure Python - a plain loop, one number at a time (interpreted).
start = time.perf_counter()
total = 0.0
for i in range(N):
    total += i * i
pure_python = time.perf_counter() - start

# Way 2: NumPy - the SAME maths, but run inside a compiled C core.
start = time.perf_counter()
numbers = np.arange(N, dtype="float64")
total_numpy = (numbers * numbers).sum()
numpy_time = time.perf_counter() - start

print(f"Pure Python loop : {pure_python * 1000:8.1f} ms")
print(f"NumPy (C core)   : {numpy_time * 1000:8.1f} ms")
print(f"Same answer?       {np.isclose(total, total_numpy)}")
print(f"NumPy is about {pure_python / numpy_time:.0f}x faster - you wrote less and waited less.")
Live output
Pure Python loop :    468.2 ms
NumPy (C core)   :     14.9 ms
Same answer?       True
NumPy is about 31x faster - you wrote less and waited less.

The plain loop took roughly half a second; NumPy did the very same sum in about fifteen milliseconds - around thirty times faster - and returned the exact same answer. You didn't just wait less, you also wrote less. That one result is the entire philosophy of Python for finance in a single line: write simple Python, lean on fast libraries.

Key idea

Pure Python is slow, but you rarely use pure Python for the heavy work. The maths runs inside compiled C and Rust cores - so your code stays easy to read and runs fast. Best of both worlds.

Did you know?

pandas - the library you'll lean on most in this course - was born inside a hedge fund. A developer named Wes McKinney created it in 2008 while working at AQR Capital Management, because he needed a better way to wrangle financial data in Python. The name comes from "panel data", an econometrics term - not the animal. Finance didn't just adopt Python's tools; it built some of the most important ones.

So who actually uses Python?

If Python were a toy, the most demanding firms in the world would have dropped it long ago. The opposite happened.

  • JP Morgan built Athena, a giant risk-and-pricing platform, around Python.
  • Goldman Sachs opened up its famous internal systems so its people could drive them from Python.
  • Jane Street, Two Sigma and other elite quant shops lean on Python every day for research, data and machine learning.
  • Across India, the proprietary trading desks behind a large share of daily volume run their research and automation in Python too.

These firms can afford any technology they want. They chose the one that lets a smart human express an idea quickly - and then quietly runs it on a fast engine. That's the same deal on offer to you, for free, starting right now.

Note

You don't need a maths degree or a computer-science background to follow along. If you can read a price chart and work out a percentage, you already have what you need. We'll build the rest together, one small step at a time.

What you'll build over the next 39 chapters

Here's the journey ahead, in plain terms:

  • Module 1 - Getting Started: install Python, set up your tools, and learn the absolute basics - variables, numbers and text.
  • Module 2 - Core Programming: the building blocks - lists, dictionaries, decisions, loops and functions.
  • Module 3 - Libraries, Files & Robust Code: use other people's code, read and write files, handle dates, and fix errors calmly.
  • Module 4 - Data Analysis: the real workhorses - NumPy and pandas - plus charts that actually communicate.
  • Module 5 - Python for the Markets: put it all together on real Indian and US data - OHLCV bars, live quotes, returns and signals.

And a few promises about how we'll work together:

  • Real data, always. US prices come from a free library called yfinance; Indian quotes come from OpenAlgo. No made-up numbers.
  • Runnable examples. Every code box in this course was actually run, and you're seeing its genuine output - exactly like the benchmark above.
  • Plain English. No piece of jargon arrives without a plain-words explanation first.
  • No big, scary projects. Just clear, bite-sized lessons that stack neatly on top of one another.

Try it yourself

You don't need to write any code yet - just think these through:

  • Re-read the benchmark output. Roughly how many of the slow Python loops would fit inside the time NumPy needed for one?
  • Name two libraries from this chapter and the language each one's fast core is written in.
  • Finish this sentence in your own words: "Pure Python is slow, but..."

Recap

  • Pure Python is slow, because it handles work one step at a time - but that rarely matters.
  • The heavy maths runs inside libraries with compiled C, C++ or Rust cores (NumPy, pandas, TA-Lib, TensorFlow, OpenAlgo, OpenGreeks), so your code stays easy to read and fast to run.
  • Our benchmark proved it: NumPy did the same sum about 30 times faster than a plain loop, for the same answer.
  • The world's top desks - JP Morgan, Goldman Sachs, Jane Street and India's prop desks - use Python for precisely this reason.
  • Over 40 chapters you'll go from zero to handling real market data, with runnable examples and real prices throughout.

Convinced? Good. In the next chapter we'll get Python onto your own computer - the painless way - and run your very first line of code.