Write the steps once. Let the computer repeat them.
The story so far Your tune-up spreadsheet (Lesson 17.1) works. Now you want to put $200 a month, or $2,400 a year, from tune-ups into an index fund (Lesson 5.3). If it earns 7% a year, how much will you have in 30 years? And what if it earns 4%, or 10%? In a spreadsheet, that's 30 rows for every guess. In Python, it's four lines, and the computer repeats them for you. That's why banks, funds and fintech companies hire people who can code.
Python is a programming language: you write steps in plain-looking text, and the computer runs them, in order.
It repeats work in a blink, handles millions of rows that would break a spreadsheet, and runs the same report again tomorrow with one click.
Store numbers in variables, keep many in a list, repeat steps with a loop, and wrap steps you reuse in a function.
Quant analysts test trading ideas in it. Risk teams run thousands of scenarios. Data analysts at banks clean and study huge data sets.
What you're looking at: a short Python program. It starts with $0, then 30 times it grows the balance by 7% and adds this year's $2,400. The last line prints the result: about $226,706. The green line at the bottom is the output.
Code is a recipe the computer follows.
Python runs your lines from top to bottom. A variable is a name that holds a value: the = sign means "store this", not "is equal to". print() shows a result on the screen.
price = 40
tune_ups = 25
revenue = price * tune_ups
print(revenue)1000- price and tune_ups
- Variables holding 40 and 25, like cells B1 and B2 in Lesson 17.1.
- revenue
- 40 × 25 = 1000. Like a spreadsheet, * means times.
Lists: many numbers under one name
A list keeps many values in square brackets. sum() adds them up, and len() counts them. Python divides with the slash key: sum(months) / len(months) means sum(months)len(months), the average.
months = [20, 25, 30]
print(sum(months))
print(len(months))
print(sum(months) / len(months))75
3
25.0- sum(months)
- 20 + 25 + 30 = 75 tune-ups.
- len(months)
- 3 months in the list: 3.
- The average
- 753 = 25.0. Python shows .0 after a division.
Loops: repeat the steps
A for loop repeats the lines indented under it. range(3) gives the numbers 0, 1 and 2, so the loop runs 3 times. The spaces at the start of a line matter: indented lines are inside the loop, and the first line that isn't indented runs after it ends. Each time, balance = balance * 1.10 takes the old balance, grows it 10%, and stores the new one under the same name.
balance = 1000
for year in range(3):
balance = balance * 1.10
print(round(balance))1331- Each pass
- $1,000 → $1,100 → $1,210 → $1,331, the same compound growth as Lesson 10.1.
- round()
- Rounds to a whole number, so you don't see 1331.0000000000005.
Your savings, 30 years in four lines
Change two numbers and the loop answers a new question. Here, each year the balance grows by the return, then you add $2,400. Move the slider to change the rate and watch the code and its output change.
What you're looking at: the code, what it prints, and your balance every 5 years, with bars for years 5 to 30. Green is what you put in yourself ($2,400 a year); gold on top is growth from the returns.
- At 0%
- 30 × $2,400 = $72,000: only what you put in.
- At 7%
- $226,706. $154,706 of it is growth.
- At 10%
- $394,786. Three more points of return, $168,080 more money.
Functions and decisions
A function wraps steps you reuse: def names it, and return sends back the answer. An if lets code decide: the indented line under if runs only when the test is true; otherwise, the line under else runs.
def profit(tune_ups):
return tune_ups * 30 - 300
print(profit(20))
npv = 1372
if npv > 0:
print("Build it")
else:
print("Skip it")300
Build it- profit(20)
- 20 × ($40 − $10) − $300 = 300, January's profit from Lesson 17.1.
- The if
- The repair van's NPV is $1,372 (Lesson 13.2), more than 0, so it prints Build it.
pandas: a spreadsheet inside your code
Python has free add-ons called libraries. The one finance uses most is pandas. Its DataFrame is a table, like a spreadsheet you control with code. An Excel sheet stops at 1,048,576 rows; a bank's trade records can have hundreds of millions. pandas reads them and sums a column in one line.
import pandas as pd
df = pd.DataFrame({"month": ["Jan", "Feb", "Mar"], "profit": [300, 450, 600]})
print(df["profit"].sum())1350- The table
- Two columns, month and profit, three rows: your first three months from Lesson 17.1.
- df["profit"].sum()
- $300 + $450 + $600 = 1350. The same line works on 300 rows or 300 million.
Two minutes. Just listen.
Tap any line to jump to it.
Code check.
Eight short programs. Read each one and pick what it prints. One try each.
What you're looking at: made-up programs. Each shows the code, then asks one question about it.
The logic, step by step.
To read any short program, take four steps.
Step 1: Read top to bottom
- The savings code
- Set balance to 0, then the loop, then print.
Step 2: Track every variable
- balance
- Starts at 0.
Step 3: Count the loop
- range(30)
- 30 passes: grow by 7%, add $2,400.
Step 4: Check the first pass by hand
- Year 1
- $0 × 1.07 + $2,400 = $2,400. Year 2: $2,400 × 1.07 + $2,400 = $4,968.
Worked example: test three rates at once
You want the 30-year balance at 4%, 7% and 10% without running the code three times. Here are the facts:
- You save: $2,400 a year for 30 years.
- The rates: 4%, 7% and 10%, kept in a list: [0.04, 0.07, 0.10].
- The tool: a loop inside a loop.
for rate in [0.04, 0.07, 0.10]:
balance = 0
for year in range(30):
balance = balance * (1 + rate) + 2400
print(rate, round(balance))0.04 134604
0.07 226706
0.1 394786- Outer loop. It runs 3 times, once for each rate.
- Reset. balance = 0 sits inside the outer loop, so every rate starts fresh.
- Inner loop. 30 passes, the same savings rule as before.
- The answer. $134,604, $226,706 and $394,786. Going from 4% to 10% nearly triples your money, which is why low fees and a good mix of investments matter so much.
A sunflower runs one line of code.
Look closely at a sunflower's center and you'll see spirals. The plant follows one simple rule: each new seed grows about 137.5° around from the last one, a little farther out. Repeated hundreds of times, that one rule packs the seeds with almost no gaps.
What you're looking at: a drawing made with the sunflower's own rule. Each of the 239 seeds was placed 137.5° around from the one before it, a little farther from the center.
Why this sticks. The sunflower doesn't plan each seed; it repeats one step. Code works the same way: you write one careful rule, and a loop applies it 30 times, or 30 million.
The page you'd copy down.
- Variable: price = 40 stores 40. print() shows a result.
- List: months = [20, 25, 30]. sum = 75, len = 3, average 753 = 25.0.
- for loop: repeats the indented lines. range(3) = 0, 1, 2. $1,000 at 10% for 3 years = 1331.
- $2,400 a year for 30 years: $72,000 at 0%, $226,706 at 7%, $394,786 at 10%.
- Function: def names it, return answers. profit(20) = 300.
- if / else: runs a line only when the test is true. NPV $1,372 > 0: "Build it".
- pandas DataFrame: a table in code. Excel stops at 1,048,576 rows; pandas doesn't.
- Always check the first pass of a loop by hand.
Fill in the blanks
Grab a friend or a study group.
Be the computer
One person reads a short loop out loud. Everyone else tracks the variables on paper, pass by pass, and says what it prints.
Try it for real
Open a free notebook like Google Colab, type the savings loop, and change the rate and the years. Who can reach $1 million first, and how?
Spreadsheet or code?
List five finance tasks, like a budget or checking every trade from last year. Decide together which tool fits each one, and why.
Just you and your money.
These stay on this device. Nobody else sees them.
Exam-style questions.
Written the way a college exam would ask them. Each right answer on the first try is worth 10 xp.
On the job: you're an analyst at a wealth management firm
A client, age 25, can invest $500 a month, or $6,000 a year, until 65. They ask how much they might have. Your manager wants three cases. Here's the situation:
- Years: 40.
- Cases: 5% (careful), 7% (middle), 9% (strong).
- Rule: each year the balance grows, then the year's $6,000 is added.
Your task: write the code, give the three results, and add one sentence for the client.
See a model answer
for rate in [0.05, 0.07, 0.09]:
balance = 0
for year in range(40):
balance = balance * (1 + rate) + 6000
print(rate, round(balance))0.05 724799
0.07 1197811
0.09 2027295To the client: you'll put in $240,000 yourself (40 × $6,000). Depending on returns, it could grow to about $725,000, $1.2 million or $2 million. Starting at 25 is your biggest advantage: most of that money is growth, not savings.
Where this leads.
What you're looking at: a map of where this lesson fits in the course. Read it left to right: the lessons it builds on, this lesson in the middle, and where it leads.
- Back · Unit 17Spreadsheets for finance
- Back · Unit 10Percentages and growth
- Back · Unit 10The time value of money
- You are herePython for finance
- Coming soon · Unit 17SQL for finance