Home / Knowledge work / Coding
Writing your first small program in Python
Install Python and VS Code, write a small tip and split calculator, learn variables, input, math, decisions and functions, then add a test.
Beginner · 12 min read · Updated December 3, 2026
The fastest way to learn coding is to build something small that works. In this lesson you will write a tip and bill-splitting calculator in Python. Python is a great first language: it reads almost like English, it is used everywhere from websites to data analysis to AI, and you can do useful things with very little code.
You will learn the five ideas that show up in almost every program: variables, input and output, math, decisions and functions. Then you will add a test.
Set up (15 minutes, once)
Install Python
On Windows, install the Python install manager from python.org or the Microsoft Store, then let it add Python to your PATH when it asks (PATH is the list of places your computer looks for commands). On a Mac, use the python.org installer or Homebrew. Then open a terminal and typepython3 --version(orpython --versionon Windows) to check it works.Install VS Code
Download it from code.visualstudio.com.Add the Python extension
In VS Code, open the Extensions panel, search for "Python" and install the one from Microsoft.Install Git
On Windows, install Git for Windows from git-scm.com. On a Mac, typegit --versionin Terminal and accept the offer to install the developer tools. Then tell Git who you are, once per computer:git config --global user.name "Your Name"andgit config --global user.email "you@example.com".Make a project folder
Create a folder calledtip-calculatorand open it in VS Code with File, then Open Folder.
How a program flows
Step 1: Hello and variables
Create a file called tip.py and type:
bill = 84.50
people = 3
print("Bill:", bill)
print("People:", people)
Click the Run button (the triangle at the top right) or type python3 tip.py in the VS Code terminal (python tip.py on Windows).
A variable is a name that holds a value. bill holds 84.50 and people holds 3. print shows something on the screen.
Step 2: Do the math
Add these lines:
tip_rate = 0.20
tip = bill * tip_rate
total = bill + tip
each = total / people
print(f"Tip: {tip:.2f}")
print(f"Total: {total:.2f}")
print(f"Each person pays: {each:.2f}")
* multiplies and / divides. The f before the quotes makes an f-string, which lets you put variables inside text with curly braces. :.2f means "show two decimal places", which is right for money.
Step 3: Ask the user
Hard-coded numbers are not very useful. Replace the first two lines with:
bill = float(input("Bill amount: "))
people = int(input("How many people? "))
input asks a question and waits for an answer. The answer always comes back as text, so float turns it into a decimal number and int turns it into a whole number. Run it and try a few amounts.
Step 4: Make a decision
What if someone types 0 people? Dividing by zero crashes the program. Use if to check first:
if people <= 0:
print("Please enter at least 1 person.")
else:
each = total / people
print(f"Each person pays: {each:.2f}")
The indented lines belong to the if or the else. Python uses indentation (four spaces) to group code, so keep it neat. VS Code does this for you.
Step 5: Put it in a function
A function is a named block of code you can reuse. It takes inputs (called parameters) and gives back a result with return. Rewrite your file like this:
def split_bill(bill, people, tip_rate=0.20):
"""Return what each person pays, including tip."""
if people <= 0:
raise ValueError("people must be at least 1")
total = bill * (1 + tip_rate)
return round(total / people, 2)
if __name__ == "__main__":
bill = float(input("Bill amount: "))
people = int(input("How many people? "))
print(f"Each person pays: {split_bill(bill, people):.2f}")
The function does the math. The bottom part handles input and output. The line if __name__ == "__main__": means "only ask questions when this file is run directly", which lets other files, like tests, use the function without being asked for input.
Step 6: Add a test
Install pytest, a popular testing tool. In the terminal:
python3 -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activate
pip install pytest
The first line makes a virtual environment, a private space for this project's tools so they do not clash with other projects. Then create test_tip.py:
import pytest
from tip import split_bill
def test_three_people():
assert split_bill(90, 3) == 36.0
def test_no_tip():
assert split_bill(50, 2, tip_rate=0) == 25.0
def test_zero_people_is_an_error():
with pytest.raises(ValueError):
split_bill(50, 0)
Run pytest in the terminal. You should see three passing tests. Now change the math in split_bill on purpose to break it and run pytest again. The tests catch it. That is the whole point.
Step 7: Save it with Git
In the terminal:
git init
git add tip.py test_tip.py
git commit -m "Tip calculator with tests"
Now you have a saved snapshot you can always return to.
Using an AI agent on this project
Try asking Claude Code or Codex, inside this folder: "Add an option to round each person's share up to the nearest dollar, and add tests for it." Watch what it changes, read the diff, run pytest yourself, and only commit if you understand every line. Asking the agent to explain its code line by line is one of the best ways to learn.
Prefer JavaScript?
The same ideas work in JavaScript, the language of web browsers. Install Node.js from nodejs.org and the same function looks like this:
function splitBill(bill, people, tipRate = 0.2) {
if (people <= 0) throw new Error("people must be at least 1");
return Math.round((bill * (1 + tipRate) / people) * 100) / 100;
}
console.log(splitBill(90, 3)); // 36
Run it with node tip.js. Tests in JavaScript are often written with Vitest.
Common mistakes
- Forgetting to convert
inputtext to a number - Mixing tabs and spaces, or uneven indentation
- Naming your file the same as a module you import, like
pytest.py - Skipping tests because the program "seems to work"
- Copying AI code you do not understand