Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

argument
A value you put inside the parentheses when you call a function, which the function then receives.
block
A group of lines that belong together under one opening line, such as the body of an if or a loop.
branch
A path a program can take when a condition sends it one way or another.
call site
The place in the code where a function is called, as opposed to where it is defined.
class
A template that bundles data with the code that works on it; each copy has its own values.
dictionary
A collection of key and value pairs, where a key is used to look up its value.
exception
An error that stops the normal flow of a program unless some code catches it.
function
A named piece of code that does one job and can be called whenever you need it.
function object
A function treated as a value, so it can be stored in a variable, passed to another function, or returned.
indentation
The spaces at the start of a line that show which block it belongs to.
integer
A whole number with no decimal point, such as 3, 0 or -12.
list
An ordered collection of values that you can grow, shrink and reach into by position.
parameter
A name in a function definition that stands for the value the caller will supply.
return value
The result a function hands back to the code that called it.
scope
The part of a program where a name can be used; a name made inside a function is not visible outside it.
traceback
The report Python prints when a program fails, listing the calls that led to the error.
variable
A name that refers to a value, so you can use that value again later or change it.

Python from the ground up

A small program, end to end

Everything at once: a tiny to-do list that uses a function, a loop, a list and a file — and what to build next.

Part 11 of 15

Advanced18 min readBack to Courses

Everything, in one small program

A program worth keeping is rarely one clever line; it is a handful of ordinary pieces arranged clearly. The to-do list below uses only what this course has already taught: a list to hold the items, a function to add one, a loop to print them, and a file to keep them.

pass means "nothing here yet". It is the honest placeholder for a body you have not written, and it keeps the file runnable while you work.

The shape of the program

todos = ["buy flour", "feed the starter"]

def add(item):
    pass  # one job: add the item, and say so

# a loop to print them, a file to save them

Plan in words first

Before typing, say what the program does in two or three plain sentences: keep a list of things to do; a function adds one and confirms it; print the list numbered; save it to a file and say so.

A plan in words costs a minute and saves ten. It also decides the shape — a list here, a function there — before the details start arguing with each other.

The to-do list, finished

Finish the program: a function that adds an item and confirms it, the list printed as numbered lines, and every item saved to todos.txt before a final line that says Saved.

Output
Click "Run" to execute your code

Where to go next

You have met the whole shape of the language now: values and names, decisions, loops, functions, lists and dictionaries, files, errors, classes and modules. Nothing on that list is optional for real work, and all of it is now yours to practise.

The way to practise is to build something small that you actually want — a bill splitter, a quiz, a habit tracker. Read other people’s code when it confuses you, and reach for the official tutorial when a word is unfamiliar. The next program should be one you would miss if it stopped working.

The smallest version that runs

The first version of a program should do one visible thing. For the to-do list that is printing the two starting items; everything else — the function, the numbering, the file — is added to a program that already runs. A working skeleton is easier to extend than a complete program that has never executed.

This is not a preference for small ambitions. It is a way of keeping the number of possible causes small. When the skeleton runs and the next slice does not, the new lines are the only suspects.

A program is read far more often than it is run

The reader may be you in a month, with no memory of why a line is there. Names that say what they hold, functions that do one thing, and a short comment at each step are not decoration; they are how the program survives the interval.

The test is simple: can you explain each line out loud without guessing? A line you cannot explain is either unnecessary or in need of a better name. Deleting it is often the fastest improvement available. That explanation is also the comment worth writing — the reason, not the syntax.

The best next program is one you actually want to exist. Want is what makes you finish it, and finishing is what teaches.

Build it in runnable pieces

A program this size is easier to finish in slices than in one pass. Get the list printing first, then add the function, then the file. After each slice, run it: a small program that works tells you which slice broke, while a large one that fails tells you only that something did.

Print the state between slices — the list after adding an item, the text before writing it. The output is not clutter; it is the only view you have of what the program currently believes.

Name the data before the steps

Start by naming the data: a list of text for the to-dos, a dictionary for counts, a filename for the report. The shape decides which operations are easy — appending to a list, looking up a key, writing a line.

Then name the steps as verbs: add, print, save. Each verb is a candidate for a function or a short block. If a step needs the word “and”, split it.

Three capstone traps: writing every piece before running any of it, so the first error could be anywhere; defining a function after the line that calls it (definitions run top to bottom like everything else); and writing file lines without \n, so the saved list becomes one long line.

Try these next

  • Write the numbered print loop first, before the function or the file, and run it.
  • Add the function and print the list again; only then add the save step.
  • Open the saved file and print it back, so the last check is the file’s contents rather than a claim.

A word count that saves a report

Finish the program: count how often each word appears, print the counts in alphabetical order, write the same lines to counts.txt, and print Saved when the file is closed.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Which structure suits "a word and its meaning"?