FREE · PYTHON + GENAI

Learn Python.
For Free.

Run real Python in your browser, no installs. Across five short lessons you build one tool end to end: roll a product-line P&L into a clean summary, then call a live GenAI model through an API you control. You set the rules for what it can return, so you see what working with a governed, restricted AI actually looks like, not just chatting with a chatbot.

Python runs in your browser via Pyodide. The capstone calls a restricted AI endpoint that only ever sees your summary.
5
Hands-on Lessons
0
Install Required
100%
Finance Focused
FOUNDATIONS

Build one thing, start to finish

No Hello World, no syntax drills. Across three short lessons you build a single tool: a function that rolls a product-line P&L into a profitability summary. Each lesson explains every line in plain English, then you fill in one blank and run it. By the end the pieces snap together, and in the Applied track you hand it to a live AI model that you call, and control, through an API.

LESSON 01 · FUNCTIONS · brick one
Brick 1 of 3 · start here

Write the rule once

Every product line's gross profit is the same arithmetic: revenue minus COGS. In Excel you'd type =B2-C2 and copy it down every line, every month. In Python you write that rule one time as a function and reuse it on every line. It's the first brick of the tool you're about to build.

HOW IT WORKS

A function is a reusable rule with a name. You define it once with the word def, list the inputs it needs in parentheses, and return the answer. After that, you can run the rule on any numbers just by calling its name, like gross_profit(500000, 150000), instead of retyping the math.

def gross_profit(revenue, cogs):
Names the rule “gross_profit” and says it takes two inputs: a revenue figure and a COGS figure. The colon starts the rule’s body, which is indented below.
return ____
“return” hands back the result. Whatever you write after it becomes the answer the function gives you. The four spaces of indentation are how Python knows this line belongs to the function.
print(gross_profit(500000, 150000))
Calls the rule with Software’s numbers and prints what comes back, so you can see it worked.
THE BLANK  Gross profit is revenue minus COGS. In the blank, write the two input names with a minus sign between them. Use the names, not the actual numbers, so the same rule works for any line you give it.
HOW TO TYPE IT
revenue - cogs
The two input names with a minus sign between them. Use the names, not the numbers, and the spaces around the minus are optional.
IN EXCEL
fx=B2-C2
RevenueCOGSGross Profit
1500,000150,000350,000
One line's gross profit. Now copy it down every product, every month.
IN PYTHON · YOUR TURN
Fill in the blank so gross_profit returns revenue minus COGS, then press Check.
gross_profit.py
LESSON 02 · LOGIC · brick two
Brick 2 of 3 · a separate rule from Lesson 1, snapped together with it next lesson

Teach it to decide

FP&A is about flags: which lines are healthy, which need a look. In Excel that's =IF((B2-C2)/B2>=0.4,"healthy","watch"), which is fine until you nest five of them and hunt the missing parenthesis at 11pm. In Python the decision reads top to bottom like a sentence, and it becomes brick two: a verdict on every line.

HOW IT WORKS

An if statement lets your code make a decision. It checks a condition; if that condition is true, it runs the indented line below it, otherwise it skips down to the next instruction. Here the decision is whether a line's gross margin is 40% or better.

margin = (revenue - cogs) / revenue
Gross profit divided by revenue gives the margin as a decimal, where 0.40 means 40%. We store it in a variable called “margin” so the next line can test it.
if margin >= 0.40:
The test. “>=” means “greater than or equal to”. If margin is 0.40 or higher, Python runs the indented line directly below.
return ____
Runs only when the test passed. A line that clears 40% gets the good label, so this should return that word.
return "watch"
The fallback. If the test failed, Python skips the line above and lands here. Quotes mark “watch” as a word, not a number or a variable.
THE BLANK  The line above already passed the 40% test, so return the word for a good line: the same kind of quoted text as "watch" on the last line, but the positive verdict. Anything under 40% skips down to "watch".
HOW TO TYPE IT
"healthy"
Put the word inside double quotes, exactly like "watch" on the next line. The quotes are what tell Python it is text, not a variable name.
IN EXCEL
fx=IF((B2-C2)/B2>=0.4,"healthy","watch")
RevenueCOGSStatus
1500,000150,000healthy
2300,000220,000watch
One IF is easy. Five nested IFs at quarter-close are not.
IN PYTHON · YOUR TURN
A line with a gross margin of 40% or better is “healthy”. Fill in the blank so it says so.
margin_flag.py
LESSON 03 · PUT IT TOGETHER · the whole P&L
Brick 3 of 3 · reuses gross_profit (Lesson 1) and margin_flag (Lesson 2). Both are re-included in the starter, so this runs on its own.

Roll up every product line

Here's the real payoff of writing rules instead of formulas: hand the function a whole list and it runs on all of it. Your P&L is a list of product lines. One loop totals revenue and COGS, flags each line with the logic from Lesson 2, prices gross profit with the function from Lesson 1, then subtracts operating expenses to land on net profit. In Excel this is a helper column plus SUM plus COUNTIF, re-pointed every time a line moves.

HOW IT WORKS

A for loop walks through a list one item at a time and runs the same steps on each. Here it visits each product line, adds that line's revenue and COGS to running totals, and counts the healthy ones using your Lesson 2 rule. After the loop finishes, you reuse your Lesson 1 rule on the totals, then subtract OpEx for net profit, and bundle everything into one summary.

for p in lines:
Repeats the indented block once for every product line. Each time around, “p” is one line’s data (its name, revenue, and cogs).
revenue += p["revenue"]
“+=” means “add to the running total”. p["revenue"] pulls the revenue value out of the current line. The cogs line right below does the same for COGS.
if margin_flag(...) == "healthy":
Reuses brick two from Lesson 2 to check the current line, and adds 1 to the healthy counter when it passes.
gp = ____
After the loop, this sets gross profit for the whole company from the totals you just summed. This is the blank.
return { ... }
Bundles revenue, COGS, gross profit, net profit (gross profit minus OpEx), and the healthy count into one dictionary: your finished summary.
THE BLANK  You already wrote the gross-profit rule in Lesson 1, so reuse it here by calling it on the company totals, passing in revenue and cogs. That’s the whole point of a function: you call it again instead of rewriting the math.
HOW TO TYPE IT
gross_profit(revenue, cogs)
Call your Lesson 1 function by name, then pass the two totals inside parentheses, separated by a comma.
IN EXCEL
fx=SUM(B2:B4)-300000 & =COUNTIF(C2:C4,"healthy")
ProductGross ProfitStatus
1Software350,000healthy
2Hardware80,000watch
3Services80,000healthy
4ROLL-UP510,0002 healthy
Gross profit 510K, minus 300K OpEx = 210K net. In Excel: a SUM, a COUNTIF, and a helper column.
IN PYTHON · YOUR TURN
summarize loops over the P&L. Fill in the blank to price total gross profit with the function you wrote in Lesson 1.
summarize.py

Three bricks in, and you've built a working P&L summary.

The Applied track points this same tool at a programmable table and a live AI model. When you want a guided path to using this at work, our programs map it out by where you're starting from.

APPLIED

Now do what Excel can’t

Same P&L, two more moves: rebuild your profitability summary as a table you program instead of drag, then call a live AI model from your own code, control exactly what it can return, and put it to work on the result. This is where Python stops being “spreadsheets too” and starts doing things spreadsheets never will.

LESSON 04 · PANDAS · the same P&L, programmable
Applied · the same P&L from Lessons 1-3, now as a pandas table

The spreadsheet you can program

Your loop from Lesson 3 works. But pandas gives you the same P&L as a table that lives in code, where a column operation runs on every line at once, defined in one line, with no range that drifts when you insert a product. Same summary, far less code.

HOW IT WORKS

pandas is Python's spreadsheet library. It turns your P&L into a DataFrame, a table with named columns, exactly like a sheet. The superpower: you can create a whole new column in one line, and the calculation applies to every row at once. No dragging, no range to break.

import pandas as pd
Loads the pandas library and nicknames it “pd”, so the rest of the code can say pd.something.
df = pd.DataFrame([ ... ])
Builds the table (everyone calls it “df”) from your three product lines. It has columns named product, revenue, and cogs.
df["gross_profit"] = ____
Creates a brand-new column called gross_profit. Whatever you put on the right is computed for every row at the same time. This is the blank.
df["margin"] = (df["gross_profit"] / df["revenue"]).round(2)
Same trick for margin: gross profit divided by revenue, rounded to 2 places, across the whole column at once. Notice how a column is written df["name"].
df["gross_profit"].sum()
Totals the column. Subtract OpEx from that and you have net profit: the same numbers as Lesson 3, with far less code.
THE BLANK  Mirror the Excel =B2-C2, but on whole columns instead of single cells: subtract the cogs column from the revenue column. A column is written df["name"], so you need the revenue column minus the cogs column. pandas does the subtraction row by row automatically.
HOW TO TYPE IT
df["revenue"] - df["cogs"]
Subtract one whole column from the other. Each column is written df["name"], with the name in double quotes inside square brackets.
IN EXCEL
fx=B2-C2 ⟶ fill down, then =SUM
RevenueCOGSGross Profit
1500,000150,000350,000
2300,000220,00080,000
3200,000120,00080,000
One formula, hand-copied down a column that breaks if a line moves.
IN PYTHON · YOUR TURN
df is your P&L. Fill in the blank to build a gross_profit column, every line's profit at once.
pnl.py
LESSON 05 · AI CAPSTONE · the controlled API call
Applied · sends the summary you built in Lesson 3 to a live model you call yourself

Call the model on your terms

Any chatbot can write a paragraph about your numbers. The difference here is control. You are not pasting into a chat window, you are calling the model from your own code, handing it only this structured summary, and locking down what it is allowed to return. And because it is code, the same call runs one summary now or a hundred at once.

HOW IT WORKS

A dictionary, written with curly braces { }, is a labeled bundle of values. Each entry is a "key": value pair, a name paired with a number or a list. This is exactly the shape you hand an AI model: clean, labeled data it can read, instead of a vague sentence. Because you send only these fields, the model can't be steered off-topic.

"revenue": 1000000,
A key (the label “revenue”) and its value. The model reads the label, so it knows what each number means.
"net_profit": 210000,
Another pair. Together these fields are the whole P&L picture: the same numbers you computed in Lessons 3 and 4.
"products": ____
A value can be a list of items, not just a number. This entry should hold the names of your product lines. This is the blank.
print(summary)
Shows the finished bundle. When you press the button below, this exact dictionary is what gets sent to the live model.
THE BLANK  Make a list of your three product lines: Software, Hardware, and Services. A list goes inside square brackets [ ], with each name in quotes and separated by commas. That’s the only piece the summary is missing.
HOW TO TYPE IT
["Software", "Hardware", "Services"]
A list goes inside square brackets. Put each name in double quotes and separate them with commas.
IN EXCEL
fx=AI("write management commentary")
TaskResult
1Write commentary#NAME?
Excel has no function for this. There is no formula.
IN PYTHON · YOUR TURN
Assemble the summary the model will receive. Fill in the blank with the list of product lines, Check it, then send it to the live model and ask it anything about this P&L.
commentary.py

You just built a tool that rolls up a P&L and calls a guardrailed AI to explain it.

That is the floor, not the ceiling. The programs take this same guardrailed-AI workflow from these snippets to real pipelines on your own data, and point you to the right starting line based on where you are now.

Find your program →