CH
CalcHub
Back to Guides
Beginner

Python for Finance: Getting Started

Python is the language of modern finance. From hedge funds to fintech startups, it powers everything from data analysis to algorithmic trading. The good news? You don't need to be a maths genius โ€” Python does the maths for you.

Why Python?

Python dominates finance because of three things: pandas (data manipulation), numpy (fast maths), and a massive community that has already solved most problems you will face. It is also genuinely easy to learn compared to C++ or Java.

Setting Up Your Environment

You need three things: Python itself, a few libraries, and somewhere to write code. Here is the fastest path to a working setup.

# Install Python from python.org (3.10+ recommended) # Then open a terminal and install the key libraries: pip install pandas numpy matplotlib yfinance jupyter # pandas  โ€” spreadsheets in code (DataFrames) # numpy   โ€” fast numerical operations # matplotlib โ€” charts and plots # yfinance โ€” free stock data from Yahoo Finance # jupyter  โ€” interactive notebooks

Jupyter Notebooks

Jupyter lets you write code in cells and see results instantly โ€” perfect for exploring data. Launch it with:

jupyter notebook

Your browser opens and you can create a new Python notebook. Each cell runs independently, so you can experiment without rerunning everything.

Your First Finance Script

Let's fetch real stock data, calculate daily returns, and plot the results. Copy-paste this and run it:

import yfinance as yf import matplotlib.pyplot as plt # Download 1 year of Apple stock data aapl = yf.download("AAPL", period="1y") # Calculate daily percentage returns # pct_change() computes (today - yesterday) / yesterday aapl["Daily_Return"] = aapl["Close"].pct_change() * 100 # Print the last 5 rows print(aapl[["Close", "Daily_Return"]].tail()) # Plot the closing price plt.figure(figsize=(10, 5)) plt.plot(aapl.index, aapl["Close"], color="green", linewidth=1) plt.title("Apple (AAPL) โ€” Closing Price (1 Year)") plt.xlabel("Date") plt.ylabel("Price ($)") plt.grid(True, alpha=0.3) plt.tight_layout() plt.show() # Plot the daily returns plt.figure(figsize=(10, 3)) plt.bar(aapl.index, aapl["Daily_Return"], color="teal", width=1) plt.title("Apple โ€” Daily Returns (%)") plt.xlabel("Date") plt.ylabel("Return (%)") plt.grid(True, alpha=0.3) plt.tight_layout() plt.show()

What just happened?

yfinance downloaded real market data into a pandas DataFrame (think of it as a spreadsheet). pct_change() calculated the percentage change between each day. matplotlib drew two charts. That is genuinely all you need to start analysing stocks.

Key Concepts You Just Used

  • DataFrame โ€” A table of data with labelled rows and columns (like Excel)
  • pct_change() โ€” Calculates the percentage change between consecutive values
  • Daily return โ€” How much the stock moved today compared to yesterday, as a percentage
  • matplotlib โ€” The standard Python plotting library

Where to Go From Here

You now have a working Python environment and you have fetched real stock data. Next, we will dig deeper into analysing that data โ€” multiple stocks, moving averages, and comparisons.