Skip to main content

Programming fundamentals · Beginner · 13 min

Write your first program: a budget tracker that prints a balance

By Padding · Updated 18 Nov 2026
Python 3.15.2 · python:3.15-slim

You will write budget.py, a short Python 3.15 program that stores a monthly budget and two expenses in variables, works out what is left and prints it in your own currency, plus a plain-words plan for the rest of the tracker.

On this page

A program is a list of instructions written in a file. The computer carries them out one at a time, from the first line to the last, and stops when it runs out. That is the whole idea. Everything else you will learn, from decisions to loops to saving files, is a way of controlling which instructions run and what they work on.

This series teaches programming by building one useful thing: a personal budget tracker in Python. In this first part you write budget.py, which holds a monthly budget and two expenses, works out how much is left and prints a line like Budget 50000, spent 12500, left 37500. It does not ask you anything yet; that comes in part 2. By the end you also have a short plan, in plain words, of what the finished tracker will do.

You have never programmed before? Good. Every line is explained, and every step ends with something you can see on your own screen.

Step 1 · Get Python running and make a project folder

Install Python 3.15 by following the first step of Hello World in Python. It covers Windows, macOS and Linux, and tells you which command your system uses to start Python: python3 on macOS, python3.15 if you installed with uv on Ubuntu, or py on Windows. This tutorial writes python3 everywhere. If yours is different, type yours instead.

Open a terminal (the window where you type commands) and check the version:

Bash
python3 --version

Now make a folder that will hold the tracker for the whole series, and move into it:

Bash
mkdir -p ~/budget-tracker
cd ~/budget-tracker

mkdir -p makes a folder (-p means "do not complain if it already exists"). ~ is short for your home folder. cd changes the folder your terminal is working in, so files you create next land there. Every later part of the series starts in this folder, so keep it.

On Windows, those two lines fail: PowerShell rejects -p, and Command Prompt makes a folder literally called -p. In PowerShell, type mkdir $HOME\budget-tracker and then cd $HOME\budget-tracker instead. $HOME is PowerShell's name for your home folder. (If you use WSL, the Linux terminal on Windows, the lines above work as written.)

Check it worked

The first command prints Python 3.15.2 or a later 3.15 number. After cd, running pwd (print working directory) shows a path ending in budget-tracker. pwd works in PowerShell too.

Step 2 · Write a program that prints two lines

Create a file called budget.py in the folder. On macOS or Linux you can paste the whole block below into the terminal: it writes everything between the two PY markers into the file. On Windows, open your editor, type the two print lines, and save them as budget.py in the budget-tracker folder, then run py budget.py.

This applies to every block like it in the series: whenever a block starts with cat > name <<'PY' (or <<'TXT'), Windows readers put the lines between the first line and the closing marker into a file with that name in their editor, save it in budget-tracker, then run the py line if there is one. Replacing a file means deleting its old contents first.

Bash
cat > budget.py <<'PY'
print("My budget tracker")
print("Month: November")
PY
python3 budget.py

What each part does:

  • print(...) tells Python to show something on the screen. print is a function: a named instruction that Python already knows. The round brackets hold what you hand to it.
  • "My budget tracker" is a string, Python's word for a piece of text. The double quotes mark where the text starts and ends; they are not printed.
  • python3 budget.py starts Python and tells it to run the file. Python reads line 1, does it, reads line 2, does it, and exits.

Order matters. If you swap the two lines and run the file again, the month comes out first. The computer does not guess what you meant; it follows the file top to bottom.

Check it worked

The terminal shows My budget tracker on one line and Month: November on the next, without quotes, and then your prompt comes back.

Step 3 · Store your budget in a variable

A tracker needs to remember numbers. A variable is a name that points at a value, so you can use the name instead of retyping the value. Replace the contents of budget.py with this:

Bash
cat > budget.py <<'PY'
budget = 50000

print("My budget tracker")
print(budget)
PY
python3 budget.py

Line by line:

  • budget = 50000 creates a variable called budget and stores the whole number 50000 in it. Read = as "now refers to", not "is equal to". It is an instruction, and it runs when Python reaches that line.
  • The blank line does nothing. It only makes the file easier for you to read.
  • print(budget) has no quotes. Python looks up the name budget, finds 50000, and prints that. With quotes, print("budget") would print the word budget instead.

Use any number that fits your month in your own currency: 50000 naira, 15000 Kenyan shillings, 800 euros. Do not type a comma inside the number: Python reads 50,000 as two separate numbers, 50 and 0. If you want to group digits so they are easier to read, write 50_000. Python ignores the underscore.

Name rules worth knowing now: a variable name uses letters, digits and underscores, cannot start with a digit and cannot contain spaces. Python is case-sensitive, so budget and Budget are two different names. Use lowercase words joined by underscores, such as monthly_budget. A name that says what it holds saves you from guessing later.

Check it worked

The output is My budget tracker followed by 50000 (or the number you chose), with no quotes around the number.

Step 4 · Add expenses and work out what is left

Now add two expenses and let Python do the arithmetic. Replace budget.py again:

Bash
cat > budget.py <<'PY'
budget = 50000
rent = 8000
transport = 4500

spent = rent + transport
left = budget - spent

print(spent)
print(left)
PY
python3 budget.py

What is new:

  • rent and transport are two more variables, one per expense. Change them to your own figures.
  • spent = rent + transport adds the two values and stores the answer in a new variable, spent. Python works out the right-hand side first, then gives the result its name.
  • left = budget - spent subtracts. Python also has * for multiply and / for divide.
  • The two print lines show the results.

This is where top-to-bottom order becomes a rule you can break by accident. spent has to be created before the line that uses it. If you moved left = budget - spent above spent = rent + transport, Python would stop with an error, because at that moment spent does not exist yet.

If your currency uses cents or kobo, you can write 4500.50, and Python will handle it. Whole numbers keep the output tidy for now; a later tutorial on values and types explains how Python stores both kinds.

Check it worked

With the figures above, the output is 12500 and then 37500. With your own figures, check the arithmetic yourself: the second number plus the first should equal your budget.

Step 5 · Print a readable balance with an f-string

Two bare numbers do not say what they are. An f-string is a string with an f before the opening quote; inside it, anything in curly braces {} is replaced by its value. Replace budget.py with the version you will keep for this part:

Bash
cat > budget.py <<'PY'
# budget.py: a personal budget tracker (part 1)
currency = "NGN"
budget = 50000
rent = 8000
transport = 4500

spent = rent + transport
left = budget - spent

print(f"Budget {budget}, spent {spent}, left {left}")
print(f"You have {currency} {left:,} left this month.")
PY
python3 budget.py

Line by line, for the new parts:

  • # budget.py: ... is a comment. Python ignores everything from # to the end of the line. Comments are notes for people, including you next week.
  • currency = "NGN" stores text in a variable, the same way you stored numbers. Put your own currency code here: KES, GHS, ZAR, USD, GBP, or a symbol such as "₦" or "£". Python 3.15 reads source files as UTF-8, so symbols and letters from any language work in strings.
  • f"Budget {budget}, spent {spent}, left {left}" builds one line of text. Python swaps {budget} for 50000, and so on. Forget the f and Python prints the braces and names literally.
  • {left:,} adds a format after the colon. The comma asks Python to group thousands, so 37500 prints as 37,500. This is only for display; the value stored in left is still the plain number.

Check it worked

With the figures above, the output is exactly:

Text
Budget 50000, spent 12500, left 37500
You have NGN 37,500 left this month.

With your own currency and figures, the words are the same and the numbers are yours. If you see {budget} printed with its braces, the f before the quote is missing.

Step 6 · Fix the three errors beginners hit first

Every programmer sees error messages daily. They are Python telling you which line it could not carry out and why. Make a scratch file called oops.py with one deliberate mistake, so budget.py stays clean (on Windows, create it in your editor as described in step 2):

Bash
cat > oops.py <<'PY'
budget = 50000
print(bugdet)
PY

Run it:

Bash
python3 oops.py

Python stops and prints a traceback. Read it from the bottom: the last line says something like NameError: name 'bugdet' is not defined. Did you mean: 'budget'?, and a line above it names oops.py and line 2. A NameError almost always means a typo, or a variable used before the line that creates it.

Two more you will meet:

  • Quotes around a number. budget = "50000" stores text, not a number. budget - spent then fails with a TypeError that mentions str (string) and int (whole number). Remove the quotes.
  • Joining text and a number with +. print("Left " + left) fails with TypeError: can only concatenate str (not "int") to str. Use an f-string instead: print(f"Left {left}").

When you are done, delete the scratch file (in PowerShell, rm oops.py works too; in Command Prompt, use del oops.py):

Bash
rm oops.py

Check it worked

Running oops.py showed a message ending in NameError that pointed at line 2. After rm, ls shows budget.py in the folder and no oops.py. Running python3 budget.py still prints your balance.

Step 7 · Write down what the finished tracker will do

Programmers plan in plain words before they write code. Save the plan next to your program so you can tick it off as the series goes on. Paste this, or type it into a file called PLAN.txt in your editor:

Bash
cat > PLAN.txt <<'TXT'
Budget tracker plan
1. Store a budget and expenses, print what is left (done)
2. Type each expense in from the keyboard
3. Warn me when spending goes over the budget
4. Keep asking for expenses until I say stop
5. Keep every expense in a list and show a summary
6. Split the program into small named functions
7. Save expenses to a file and load them next time
8. Cope with bad input instead of crashing
TXT
cat PLAN.txt

Each numbered line is one part of this series and introduces one new idea: input, decisions, loops, lists, functions, files and error handling. Part 2 starts with line 2, replacing the fixed rent and transport numbers with amounts you type in.

Check it worked

cat PLAN.txt prints the eight lines, and line 1 ends with (done). Your budget-tracker folder now holds two files, budget.py and PLAN.txt, and both stay there for part 2.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.