Become a Pro / website · becomeapro.ai / content / lessons / knowledge-work / coding / your-first-small-program.mdx
| 1 | --- |
| 2 | title: "Writing your first small program in Python" |
| 3 | description: "Install Python and VS Code, write a small tip and split calculator, learn variables, input, math, decisions and functions, then add a test." |
| 4 | updated: 2026-12-03 |
| 5 | minutes: 12 |
| 6 | level: Beginner |
| 7 | order: 2 |
| 8 | --- |
| 9 | |
| 10 | 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. |
| 11 | |
| 12 | 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. |
| 13 | |
| 14 | ## Set up (15 minutes, once) |
| 15 | |
| 16 | <Steps> |
| 17 | <Step title="Install Python">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.</Step> |
| 18 | <Step title="Install VS Code">Download it from code.visualstudio.com.</Step> |
| 19 | <Step title="Add the Python extension">In VS Code, open the Extensions panel, search for "Python" and install the one from Microsoft.</Step> |
| 20 | <Step title="Make a project folder">Create a folder called `tip-calculator` and open it in VS Code with File, then Open Folder.</Step> |
| 21 | </Steps> |
| 22 | |
| 23 | ## How a program flows |
| 24 | |
| 25 | <Diagram caption="Most small programs follow the same path: get input, do some work, make decisions, show the output."> |
| 26 | <svg viewBox="0 0 600 200" role="img" aria-label="Program flow: input goes to calculate, then decide, then output"> |
| 27 | <rect x="20" y="60" width="120" height="70" rx="10" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 28 | <text x="80" y="92" fontSize="16" textAnchor="middle" fill="currentColor">Input</text> |
| 29 | <text x="80" y="112" fontSize="14" textAnchor="middle" fill="currentColor">bill, people</text> |
| 30 | <rect x="170" y="60" width="120" height="70" rx="10" className="soft" stroke="currentColor" strokeWidth="2" /> |
| 31 | <text x="230" y="92" fontSize="16" textAnchor="middle" fill="currentColor">Calculate</text> |
| 32 | <text x="230" y="112" fontSize="14" textAnchor="middle" fill="currentColor">tip, total</text> |
| 33 | <rect x="320" y="60" width="120" height="70" rx="10" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 34 | <text x="380" y="92" fontSize="16" textAnchor="middle" fill="currentColor">Decide</text> |
| 35 | <text x="380" y="112" fontSize="14" textAnchor="middle" fill="currentColor">is input OK?</text> |
| 36 | <rect x="470" y="60" width="110" height="70" rx="10" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 37 | <text x="525" y="92" fontSize="16" textAnchor="middle" fill="currentColor">Output</text> |
| 38 | <text x="525" y="112" fontSize="14" textAnchor="middle" fill="currentColor">each pays</text> |
| 39 | <line x1="140" y1="95" x2="168" y2="95" stroke="currentColor" strokeWidth="2" /> |
| 40 | <line x1="290" y1="95" x2="318" y2="95" stroke="currentColor" strokeWidth="2" /> |
| 41 | <line x1="440" y1="95" x2="468" y2="95" stroke="currentColor" strokeWidth="2" /> |
| 42 | <text x="300" y="175" fontSize="14" textAnchor="middle" fill="currentColor">Put the calculation in a function so you can test it</text> |
| 43 | </svg> |
| 44 | </Diagram> |
| 45 | |
| 46 | ## Step 1: Hello and variables |
| 47 | |
| 48 | Create a file called `tip.py` and type: |
| 49 | |
| 50 | ```python |
| 51 | bill = 84.50 |
| 52 | people = 3 |
| 53 | print("Bill:", bill) |
| 54 | print("People:", people) |
| 55 | ``` |
| 56 | |
| 57 | Click the Run button (the triangle at the top right) or type `python3 tip.py` in the VS Code terminal. |
| 58 | |
| 59 | A **variable** is a name that holds a value. `bill` holds 84.50 and `people` holds 3. `print` shows something on the screen. |
| 60 | |
| 61 | ## Step 2: Do the math |
| 62 | |
| 63 | Add these lines: |
| 64 | |
| 65 | ```python |
| 66 | tip_rate = 0.20 |
| 67 | tip = bill * tip_rate |
| 68 | total = bill + tip |
| 69 | each = total / people |
| 70 | print(f"Tip: {tip:.2f}") |
| 71 | print(f"Total: {total:.2f}") |
| 72 | print(f"Each person pays: {each:.2f}") |
| 73 | ``` |
| 74 | |
| 75 | `*` 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. |
| 76 | |
| 77 | ## Step 3: Ask the user |
| 78 | |
| 79 | Hard-coded numbers are not very useful. Replace the first two lines with: |
| 80 | |
| 81 | ```python |
| 82 | bill = float(input("Bill amount: ")) |
| 83 | people = int(input("How many people? ")) |
| 84 | ``` |
| 85 | |
| 86 | `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. |
| 87 | |
| 88 | ## Step 4: Make a decision |
| 89 | |
| 90 | What if someone types 0 people? Dividing by zero crashes the program. Use `if` to check first: |
| 91 | |
| 92 | ```python |
| 93 | if people <= 0: |
| 94 | print("Please enter at least 1 person.") |
| 95 | else: |
| 96 | each = total / people |
| 97 | print(f"Each person pays: {each:.2f}") |
| 98 | ``` |
| 99 | |
| 100 | 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. |
| 101 | |
| 102 | ## Step 5: Put it in a function |
| 103 | |
| 104 | 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: |
| 105 | |
| 106 | ```python |
| 107 | def split_bill(bill, people, tip_rate=0.20): |
| 108 | """Return what each person pays, including tip.""" |
| 109 | if people <= 0: |
| 110 | raise ValueError("people must be at least 1") |
| 111 | total = bill * (1 + tip_rate) |
| 112 | return round(total / people, 2) |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | bill = float(input("Bill amount: ")) |
| 117 | people = int(input("How many people? ")) |
| 118 | print(f"Each person pays: {split_bill(bill, people):.2f}") |
| 119 | ``` |
| 120 | |
| 121 | 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. |
| 122 | |
| 123 | <Callout type="note">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.</Callout> |
| 124 | |
| 125 | ## Step 6: Add a test |
| 126 | |
| 127 | Install **pytest**, a popular testing tool. In the terminal: |
| 128 | |
| 129 | ```bash |
| 130 | python3 -m venv .venv |
| 131 | source .venv/bin/activate # on Windows: .venv\Scripts\activate |
| 132 | pip install pytest |
| 133 | ``` |
| 134 | |
| 135 | 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`: |
| 136 | |
| 137 | ```python |
| 138 | import pytest |
| 139 | from tip import split_bill |
| 140 | |
| 141 | def test_three_people(): |
| 142 | assert split_bill(90, 3) == 36.0 |
| 143 | |
| 144 | def test_no_tip(): |
| 145 | assert split_bill(50, 2, tip_rate=0) == 25.0 |
| 146 | |
| 147 | def test_zero_people_is_an_error(): |
| 148 | with pytest.raises(ValueError): |
| 149 | split_bill(50, 0) |
| 150 | ``` |
| 151 | |
| 152 | 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. |
| 153 | |
| 154 | ## Step 7: Save it with Git |
| 155 | |
| 156 | In the terminal: |
| 157 | |
| 158 | ```bash |
| 159 | git init |
| 160 | git add tip.py test_tip.py |
| 161 | git commit -m "Tip calculator with tests" |
| 162 | ``` |
| 163 | |
| 164 | Now you have a saved snapshot you can always return to. |
| 165 | |
| 166 | ## Using an AI agent on this project |
| 167 | |
| 168 | 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. |
| 169 | |
| 170 | ## Prefer JavaScript? |
| 171 | |
| 172 | The same ideas work in JavaScript, the language of web browsers. Install Node.js from nodejs.org and the same function looks like this: |
| 173 | |
| 174 | ```javascript |
| 175 | function splitBill(bill, people, tipRate = 0.2) { |
| 176 | if (people <= 0) throw new Error("people must be at least 1"); |
| 177 | return Math.round((bill * (1 + tipRate) / people) * 100) / 100; |
| 178 | } |
| 179 | console.log(splitBill(90, 3)); // 36 |
| 180 | ``` |
| 181 | |
| 182 | Run it with `node tip.js`. Tests in JavaScript are often written with Vitest. |
| 183 | |
| 184 | ## Common mistakes |
| 185 | |
| 186 | - Forgetting to convert `input` text to a number |
| 187 | - Mixing tabs and spaces, or uneven indentation |
| 188 | - Naming your file the same as a module you import, like `pytest.py` |
| 189 | - Skipping tests because the program "seems to work" |
| 190 | - Copying AI code you do not understand |
| 191 | |
| 192 | <Resources> |
| 193 | <Resource title="The Python Tutorial" href="https://docs.python.org/3/tutorial/" source="Python Software Foundation" kind="Docs">The official, free introduction to Python.</Resource> |
| 194 | <Resource title="Python in VS Code" href="https://code.visualstudio.com/docs/python/python-tutorial" source="Microsoft" kind="Docs">Setting up and running Python in VS Code.</Resource> |
| 195 | <Resource title="pytest: get started" href="https://docs.pytest.org/en/stable/getting-started.html" source="pytest" kind="Docs">Writing and running your first tests.</Resource> |
| 196 | <Resource title="JavaScript guide" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" source="MDN" kind="Docs">The standard reference for learning JavaScript.</Resource> |
| 197 | </Resources> |