Skip to content
🐍Your First Return Calculation

Your First Return Calculation

beginner Python 5 minvariablesarithmeticf-strings
What you'll learn
  • variables
  • arithmetic
  • f-strings
  • simple return
  • percentage return
  • profit

Your First Return Calculation 📈

Scene: day one of your trading-floor internship. The desk head slides a ticket across your desk: "Bought MSFT at $300, just sold at $345 - good trade?" You answer "you made $45!" and she raises an eyebrow: "$45 on how much money? Give me a percentage." Welcome to the very first calculation every finance professional performs - and the first program you will write that real analysts write too.

In finance, return tells you how much money you made (or lost) on an investment - relative to what you put in.

Two key formulas

Profit (how many dollars you made):

code
profit = selling_price - buying_price

Percentage return (how much you made relative to what you invested):

code
return_pct = profit / buying_price * 100

🧠 The mental model: profit is the score, return is the grade

Saying "I scored 45 points" means nothing until you know whether the test was out of 300 or out of 45,000. Dividing by the money you invested turns a raw dollar score into a grade that makes any two trades comparable:

TradeProfitYou investedReturn
MSFT$45$30015.0% - excellent
Mega-cap block trade$45$45,0000.1% - noise

Same $45 profit, completely different stories. That division by buying_price is the entire point of the formula - and it is exactly the line you will write yourself below.

Example

You buy AAPL at $150, sell at $165:

  • Profit = 165 − 150 = $15
  • Return = 15 / 150 × 100 = 10%

f-strings - formatting numbers nicely

Python
profit = 15.0
print(f"Profit: ${profit:.2f}")   # Profit: $15.00

The :.2f means "show 2 decimal places".


📜 Why This Formula Exists

The percentage return formula seems obvious now, but formalising it took centuries.

1202 - Leonardo Fibonacci (Liber Abaci) introduced compound interest arithmetic to Western merchants, who previously used Roman numerals. His book transformed Italian banking and created the first systematic framework for comparing investments.

1900 - Louis Bachelier wrote his PhD thesis "Théorie de la spéculation" - the first mathematical model of stock prices as a random walk. He derived what we now call simple returns as the building block of his probabilistic framework. His thesis advisor, the mathematician Henri Poincaré, gave it a lukewarm reception; the finance community ignored it for 60 years.

1965 - Paul Samuelson (Nobel 1970) rediscovered Bachelier's work and formalised log returns - showing that if prices follow geometric Brownian motion, then log returns are normally distributed, making them ideal for statistical modelling.

1973 - Black, Scholes & Merton baked log returns into their options pricing formula. From that point forward, every serious quantitative model uses log returns internally - simple returns remain the language of client reporting, but log returns are the language of mathematics.


Your Task

You bought MSFT at $300 and sold at $345. The starter code already computes the dollar profit and prints a tidy trade report - but the return line has a bug straight from the desk head's nightmare: it reports the dollar profit as if it were a percentage.

Predict before you run: what will the Return: line show as-is? Run it and check your prediction. Then fix the # 👉 YOUR TURN line so the report shows the true percentage return - do the mental arithmetic first: 45 / 300 = 0.15, so the fixed report should read 15.0%.