This course teaches you to program in Python, starting from nothing. You don’t need to have written a program before. You start by printing a line of text, and you finish with intermediate programs that you can show an employer. Python is a general-purpose language: people use it for scripts that automate work, web services, data analysis, science, and teaching.
You learn by writing code. The first modules teach values, decisions, loops, and functions, in notebooks where each answer is checked as soon as you run it. Then you work with text, lists, dictionaries, and sets. Next you write programs that run from a terminal, save your work in Git, read and write files, handle errors, and test your code with pytest. The last part covers classes, generators, decorators, context managers, databases, web APIs, and packaging.
Along the way you build three portfolio projects and publish each on GitHub: a terminal game, a tested command-line tool for weather data, and a capstone project of your own design. The course is free and self-paced, with no deadlines and no grades: 18 modules, three assignments, three self-tests, and the three projects.
Start with setting up your computer. The suggested pace lists every module and project in order.
Learning objectives¶
By the end of the course, you can:
LO-1: Write, run, and debug short Python programs with values, variables, decisions, loops, and functions, predicting what each line does.
LO-2: Represent and process data with strings, lists, tuples, dictionaries, and sets, and choose the structure that fits a task.
LO-3: Organize programs as scripts and modules that run from a terminal, and keep a project’s history and dependencies with Git, GitHub, virtual environments, and pip.
LO-4: Read and write text, CSV, and JSON files, and process the nested data they hold.
LO-5: Make programs reliable: handle errors with exceptions, find bugs systematically, and test code with pytest and continuous integration.
LO-6: Design programs with classes, inheritance, composition, special methods, and type hints, and justify the design.
LO-7: Process data lazily with iterators and generators, and extend functions and manage resources with decorators and context managers.
LO-8: Build, document, and publish an installable Python package that stores data in SQLite or calls a web API, with tests, linting, and type checks.
Each module’s objectives serve the course objective in brackets. Every exercise, lab, assignment, and self-test question names the objectives it practices.
Part 1: First programs¶
Module 1: First programs [LO-1]¶
LO-1.1: Run Python code three ways (a notebook cell, the interactive interpreter, and a script file), and choose the right one for a task.
LO-1.2: Write programs that print text and the results of arithmetic, with comments that explain them.
LO-1.3: Use an error message to find the line and the kind of error (a syntax error, a name error, or a type error) in a short program, and fix it.
Module 2: Values, variables, and expressions [LO-1]¶
LO-2.1: Predict the type and value of expressions over integers, floats, strings, and booleans, including
/,//,%,**, and operator precedence.LO-2.2: Trace a program’s variables through a sequence of assignments, and draw each name referring to its value.
LO-2.3: Read input with
input, convert it withint,float, andstr, and explain the error that a missing conversion causes.LO-2.4: Format numbers and text with f-strings, and round and compare floats correctly (
round,math.isclose), explaining why0.1 + 0.2 == 0.3isFalse.
Module 3: Decisions [LO-1]¶
LO-3.1: Evaluate Boolean expressions built from comparisons,
and,or, andnot, and predict which values Python treats as false.LO-3.2: Write
if,elif, andelsestatements that choose among cases, and order the conditions so that every case can be reached.LO-3.3: Test a decision with inputs that reach every branch and sit on the boundaries between branches, checking each with
assert.
Module 4: Loops [LO-1]¶
LO-4.1: Write
whileloops that stop, and find the condition that makes a loop run forever or never run.LO-4.2: Write
forloops overrangeand over a string, and predict the values arangeproduces and how many times a loop runs.LO-4.3: Apply the accumulator, counter, search-with-
break, and input-validation loop patterns, and trace a loop’s variables in a table.LO-4.4: Write nested loops, and predict how many times the inner body runs.
Module 5: Functions [LO-1]¶
LO-5.1: Define functions with parameters and return values, and explain the difference between returning a value and printing it.
LO-5.2: Predict which variable a name refers to inside and outside a function (local and global scope).
LO-5.3: Call functions with positional, keyword, and default arguments, and write docstrings that say what a function takes and returns.
LO-5.4: Break a problem into small functions, and test each one with
assert.
Part 2: Working with data¶
Module 6: Strings and text [LO-2]¶
LO-6.1: Index and slice strings, including negative indices and steps, and explain why a string can’t be changed in place.
LO-6.2: Transform and search text with string methods (
split,join,strip,replace,find,lower, andstartswith) and theinoperator.LO-6.3: Process text character by character and word by word, to count, validate, and clean it.
LO-6.4: Convert between characters and code points with
ordandchr, and explain why a string’s length counts code points, not bytes or the characters a reader sees.
Module 7: Lists and tuples [LO-2]¶
LO-7.1: Build, index, slice, and change lists with loops and list methods (
append,insert,pop,remove, andsort).LO-7.2: Predict the effects of aliasing (two names for one list, a list passed to a function, and a shallow copy), and draw the names and the objects they refer to.
LO-7.3: Build lists with list comprehensions, and rewrite a loop as a comprehension and a comprehension as a loop.
LO-7.4: Use tuples, unpacking,
enumerate,zip, andsortedwith akeyfunction, and choose between a tuple and a list for a given use.
Module 8: Dictionaries and sets [LO-2]¶
LO-8.1: Use dictionaries to look up, count, and group values, with
get,items, andcollections.Counter.LO-8.2: Represent records and nested data with dictionaries and lists, and read values from nested data.
LO-8.3: Use sets to test membership, remove duplicates, and combine collections with set operations.
LO-8.4: Choose among a list, a tuple, a dictionary, and a set for a task, and justify the choice by the operations the task needs and their cost, measured with
timeit.
Part 3: Building programs¶
Module 9: Scripts, modules, and the command line [LO-3]¶
LO-9.1: Use a terminal to move between folders, list files, and run a Python script with arguments, on Windows, macOS, or Linux.
LO-9.2: Split a program into modules, import them, and use
if __name__ == "__main__":so a file works as both a script and a module.LO-9.3: Read command-line arguments with
argparse, and report errors with a message and an exit status.LO-9.4: Find a standard-library module for a task in the documentation, and use it (for example
random,math,datetime, orstatistics).LO-9.5: Create a virtual environment, install a pinned package into it with pip, and record a project’s dependencies in a requirements file.
Module 10: Version control with Git and GitHub [LO-3]¶
LO-10.1: Record a project’s history with Git: create a repository, stage and commit changes, and read the log and the differences between versions.
LO-10.2: Write commit messages that say what changed and why, and keep generated files out of the repository with
.gitignore.LO-10.3: Publish a repository on GitHub, push changes to it, and write a
README.mdin Markdown that tells a stranger what the project does and how to run it.LO-10.4: Recover from mistakes by restoring a file and reverting a commit, and try a change on a branch before merging it.
Module 11: Files and data formats [LO-4]¶
LO-11.1: Read and write text files with
withandpathlib, naming the encoding, and build paths that work on every operating system.LO-11.2: Read and write CSV files with the
csvmodule, converting each field to the right type.LO-11.3: Read and write JSON, converting between JSON and Python’s dictionaries and lists.
LO-11.4: Write a recursive function that walks nested lists and dictionaries, and name its base case.
Module 12: Errors, exceptions, and debugging [LO-5]¶
LO-12.1: Read a traceback, and name the exception, the line that raised it, and the chain of calls that led there.
LO-12.2: Handle exceptions with
try,except,else, andfinally, catching only the exceptions a block can recover from.LO-12.3: Raise exceptions with messages that say what was wrong, and choose between raising an exception and returning a special value.
LO-12.4: Find a bug by a systematic method (reproduce it, shrink the input, form and test a hypothesis), using printed values and a debugger’s breakpoints and stepping.
Module 13: Testing with pytest [LO-5]¶
LO-13.1: Write pytest tests for a function’s typical cases, edge cases, and errors (with
pytest.raises).LO-13.2: Use parametrized tests and fixtures, including
tmp_pathfor code that writes files.LO-13.3: Write a failing test before the code that makes it pass, and capture a bug in a failing test before fixing it.
LO-13.4: Run tests on every push with a GitHub Actions workflow, and read its result.
Part 4: Intermediate Python¶
Module 14: Classes and objects [LO-6]¶
LO-14.1: Define a class with
__init__, attributes, and methods, and create and use its instances.LO-14.2: Keep an object valid by checking arguments in
__init__and in methods, and state the invariant (the condition that must always hold) that the checks protect.LO-14.3: Give a class a readable
__repr__and equality with__eq__, or generate both with a dataclass.LO-14.4: Annotate functions and classes with type hints, and explain what Python checks while a program runs and what it leaves to a type checker.
Module 15: Inheritance, composition, and special methods [LO-6]¶
LO-15.1: Write a subclass that extends and overrides its base class’s methods, using
super().LO-15.2: Compare inheritance with composition for a design, and justify one of them.
LO-15.3: Implement special methods for operators, comparison, length, and iteration (
__add__,__lt__,__len__,__iter__, and__next__), and explain duck typing.
Module 16: Generators, decorators, and context managers [LO-7]¶
LO-16.1: Write generator functions and generator expressions, and show with a measurement when a generator uses less memory than a list.
LO-16.2: Combine iterators into a pipeline with
itertools(islice,chain, andgroupby) and built-ins (any,all,sum, andmaxwithkey).LO-16.3: Write closures and decorators, keep the wrapped function’s name with
functools.wraps, and cache results withfunctools.cache.LO-16.4: Manage resources with
with, and write a context manager withcontextlib.contextmanager.
Module 17: Databases and web APIs [LO-8]¶
LO-17.1: Store and query data in SQLite with the
sqlite3module (create tables, insert, select, and update) using placeholders, and show how building a query with string formatting allows SQL injection.LO-17.2: Request JSON from a web API with
urllib.request, handling HTTP status codes, timeouts, and network errors.LO-17.3: Test code that calls a web API without the network, with a local server or a stand-in from
unittest.mock, and keep API keys out of code and repositories.
Module 18: Packaging and quality tools [LO-8]¶
LO-18.1: Structure a project as an installable package with
pyproject.toml, thesrclayout, and a command-line entry point, and install it in editable mode.LO-18.2: Lint and format code with Ruff and check types with mypy, fixing each finding instead of silencing it.
LO-18.3: Release a version: a semantic version number, a changelog, a license, a Git tag, and continuous integration that runs the tests, the linter, and the type checker.
Prerequisites¶
You don’t need any programming experience. You need a computer running Windows, macOS, or Linux on which you may install software, and you need to read English instructions and do arithmetic with percentages and averages. The course teaches the terminal and Git as it goes.
Self-check. If you can do all five of these, you are ready:
Download a file, and find it again in a folder you chose.
Create a folder, rename a file, and move the file into the folder.
Install a program on your computer.
Work out 15% of 80, and the average of 4, 7, and 10.
Follow a numbered list of instructions, and notice when a step’s result doesn’t match what the instructions say it should be.
If you already program in another language, take Self-test 1 first, and skip the modules whose questions you answer correctly.
Suggested pace¶
The course is self-paced, so it has no deadlines: take as long as you need. This pace takes 23 weeks at about 7 hours a week, about 160 hours in all. Most modules take about 5.5 hours: about 2 for the reading, 2 for the lab, and 1.5 for the practice. Modules 1 and 16 take about 7. An assignment adds about 3 hours, a self-test about 2, Project 1 about 8, Project 2 about 12, and the capstone project about 22.
| Week | Topic | Reading | Then |
|---|---|---|---|
| 1 | First programs | Module 1 | |
| 2 | Values, variables, and expressions | Module 2 | |
| 3 | Decisions | Module 3 | |
| 4 | Loops | Module 4 | |
| 5 | Functions | Module 5 | PA1 |
| 6 | Strings and text | Module 6 | |
| 7 | Lists and tuples | Module 7 | |
| 8 | Dictionaries and sets | Module 8 | PA2 |
| 9 | Scripts, modules, and the command line | Module 9 | Self-test 1 (modules 1–8) |
| 10 | Version control with Git and GitHub | Module 10 | |
| 11 | Project 1: a terminal game | The Project 1 brief | |
| 12 | Files and data formats | Module 11 | |
| 13 | Errors, exceptions, and debugging | Module 12 | |
| 14 | Testing with pytest | Module 13 | Self-test 2 (modules 1–13) |
| 15–16 | Project 2: a command-line data tool | The Project 2 brief | |
| 17 | Classes and objects | Module 14 | |
| 18 | Inheritance, composition, and special methods | Module 15 | Capstone proposal |
| 19 | Generators, decorators, and context managers | Module 16 | PA3 |
| 20 | Databases and web APIs | Module 17 | Capstone milestone |
| 21 | Packaging and quality tools | Module 18 | Self-test 3 (all modules) |
| 22–23 | The capstone project | The capstone brief | Capstone release |
Checking your work¶
The course has no grades. Instead, every piece comes with a way to check it, so you can tell what you have learned and what to review:
| Piece | How you check it | Objectives |
|---|---|---|
| Labs 1–18 | Every check cell in the notebook passes | Each lab’s module |
| PA1: Numbers, decisions, and functions (notebook) | Its visible tests | LO-1.2, 2.1, 2.3, 2.4, 3.1–3.3, 4.1–4.4, 5.1, 5.3, 5.4 |
| PA2: Text and collections (notebook) | Its visible tests, and the scoring notes for its written answer | LO-6.1–6.4, 7.1–7.4, 8.1–8.4, 4.3, 5.4 |
| Project 1: A terminal game | The brief’s checklist and rubric | LO-5.4, 7.1, 8.1, 8.2, 9.2–9.4, 10.1–10.3, 3.2, 4.3 |
| Project 2: A command-line data tool | The published acceptance tests, your own tests, and the rubric | LO-9.2, 9.3, 9.5, 10.1, 10.2, 11.1–11.3, 12.2, 12.3, 13.1–13.4, 8.1, 8.2, 10.3 |
| PA3: Objects and iterators (notebook) | Its visible tests | LO-14.1–14.4, 15.1, 15.3, 16.1–16.4, 12.3, 13.1 |
| Self-test 1, after Module 8 | The answers and scoring notes in the page | Modules 1–8 |
| Self-test 2, after Module 13 | The answers and scoring notes in the page | Modules 1–13, weighted toward 9–13 |
| Self-test 3, after Module 18 | The answers and scoring notes in the page | All modules, weighted toward 14–18 |
| Capstone project: proposal, milestone, and release | The brief’s checklists and rubric, and the course’s project checker | LO-10.3, 10.4, 13.1–13.4, 14.1–14.4, 15.2, 17.1–17.3 as your idea needs them, 18.1–18.3 |
Labs check each exercise as soon as you run its check cell. Until you answer an exercise, its check fails on purpose.
Assignments check your code with visible tests, which you can run as often as you like. The course keeps a further set of hidden tests for reviewers, so passing the visible tests is a good sign but not proof; test your own edge cases too.
Self-tests mix questions from many modules. Take each one in one sitting, without notes or a computer, then open the answers and score yourself.
Projects go on GitHub, where anyone you show them to can read your code, run your tests, and see your history of commits.
Later pieces revisit earlier objectives on purpose, so that you keep practicing them.
Policies¶
Pace: the course is self-paced and has no deadlines. The suggested pace is a guide, not a requirement.
Cost: the course costs nothing. You need a computer on which you may install software and, from Module 10, a free GitHub account.
Collaboration and academic integrity: discuss ideas with anyone, but write your own code and answers. Don’t share or look at another learner’s solution. Name everyone you discussed an assignment or project with, and cite any code you adapted.
Generative AI tools: in Modules 1–13, PA1, PA2, Projects 1 and 2, and the self-tests, you may ask an AI tool to explain a concept, an error message, or documentation, but not to write or fix your code or your answers. Turn off AI code completion in your editor for that part of the course too: writing the code yourself is how you learn to program. From Module 14 on, in PA3 and the capstone project, you may use AI tools, coding assistants included, if your work says which parts they wrote and you can explain every line. End every assignment and project, and your capstone’s
README.md, with a short note saying which tools you used and how, or “none”.Secrets and personal data: never commit a password, an API key, or another person’s personal information to a repository, and publish only data you made or may share.
Accommodations: every page of the course is HTML or Markdown, which screen readers can read, and nothing is timed except the self-tests’ suggested sittings, which you can lengthen as you need. If you take the course through an institution, its disability services office arranges any other accommodations.
Course materials¶
Errata: corrections to published material, by edition
The companion textbook, free online: Allen B. Downey, Think Python, 3rd edition (O’Reilly, 2024). Each module’s further reading names the chapters that match it.
Text and figures are under the Creative Commons Attribution 4.0 International license (CC BY 4.0), and code is under the MIT License in the repository’s
LICENSEfile.