--- title: "Writing your first small program in Python" description: "Install Python and VS Code, write a small tip and split calculator, learn variables, input, math, decisions and functions, then add a test." updated: 2026-12-03 minutes: 12 level: Beginner order: 2 --- 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) Download it from python.org. On Windows, check the box "Add python.exe to PATH" during install. On a Mac, use the python.org installer or Homebrew. Then open a terminal and type `python3 --version` (or `python --version` on Windows) to check it works. Download it from code.visualstudio.com. In VS Code, open the Extensions panel, search for "Python" and install the one from Microsoft. Create a folder called `tip-calculator` and open it in VS Code with File, then Open Folder. ## How a program flows Input bill, people Calculate tip, total Decide is input OK? Output each pays Put the calculation in a function so you can test it ## Step 1: Hello and variables Create a file called `tip.py` and type: ```python 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. 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: ```python 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: ```python 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: ```python 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: ```python 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. Computers store decimal numbers in a way that can cause tiny rounding errors. For learning, `round` is fine. Real money software uses Python's `decimal` module or stores amounts in whole cents. ## Step 6: Add a test Install **pytest**, a popular testing tool. In the terminal: ```bash 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`: ```python 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: ```bash 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: ```javascript 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 `input` text 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 The official, free introduction to Python. Setting up and running Python in VS Code. Writing and running your first tests. The standard reference for learning JavaScript.