A self study course that takes you from installing Python to writing tested, packaged programs that read files, talk to web APIs, process data and run work concurrently. Every example is a complete program you can run. Every teaching unit ends with questions and exercises, and the solutions follow them.
Who it is for. People new to Python, whether or not they have programmed before, and developers coming from another language who want to write Python the way Python programmers do. No prior knowledge is assumed. If you already code, skim the first units and do their exercises; they are where the Python specific habits are introduced.
What you need. A computer running Windows, macOS or Linux, Python 3.13 or newer, and a code editor. Setting up Python walks you through all three. The examples were tested on CPython 3.15. Where a feature is newer than 3.13 the text says so.
How each unit works. It opens with what you will be able to do. Then it explains one idea at a time, each with a short program and its exact output. Pitfall boxes flag the mistakes beginners and experienced developers alike tend to make. It closes with Check yourself questions (answers included), exercises, and worked solutions. It helps to try each exercise before you read its solution: comparing the two is where most of the learning happens.
How to use this course
| Phase | Units | What you will be able to do | Rough time |
|---|---|---|---|
| Get going | Introduction, Setting up | Run Python, use the REPL and a virtual environment | 2 to 3 h |
| The language core | Values, names and types · Operators and expressions · Control flow · Strings and text formatting · Collections and comprehensions | Write small programs with data, decisions and loops | 12 to 16 h |
| Structuring code | Functions · Iterators and generators · Functional tools and decorators · Modules and packages · Errors and exceptions · Classes and objects | Organise code into reusable, robust pieces | 16 to 20 h |
| Working with the world | Text, bytes and Unicode · Regular expressions · Files and data formats · A tour of the standard library · HTTP and web APIs | Read and write real data and talk to services | 14 to 18 h |
| Professional practice | Packaging and tooling · Testing with pytest · Concurrency · Working with data | Ship, test and scale your code | 12 to 16 h |
| Capstone | Blackjack simulator | Design, build and test a complete program | 6 to 10 h |
The times include reading and the exercises. They vary widely with experience, so treat them as a planning aid, not a target. The Python cheat sheet is a one page reference to keep open while you work. The two appendices, Idiomatic Python and Practice problems, are for revision and interview preparation once you have finished the core units. The last unit, References and further reading, lists the documentation behind the course and suggests where to go next.
Conventions
- Code and output. A grey block is a complete program. The block right after it, when there is one, shows exactly what the program prints. To see the same output yourself, you can copy the program into a file, for example
example.py, and run it withpython example.py(orpy example.pyon Windows). - Comments in the first line. A first line comment such as
# Raises ValueErrortells you the program stops with that error on purpose, to show what the error looks like.# Output variesmeans your result will differ, for example timings or today's date.# Input: textshows what was typed at aninput()prompt when the output was recorded. A# Needs ...line, such as# Needs network access, means the example depends on something outside the program, for example the internet or an extra package. - f-strings. Many examples print with
f"...". Thefbefore the quotes lets you put values inside braces, as inf"{name} is {age}". Anything after a colon inside the braces, such as:<8or:.2f, controls the layout. Strings and text formatting explains them in full. - Looking ahead. The first units use a few things before their own unit explains them, mainly
forloops,ifandtry/except. Each appears in its simplest form, so you can read it almost as English. Control flow and Errors and exceptions have the details. - Shell commands are shown for PowerShell (Windows) and bash (macOS and Linux). Lines in them are commands you type, not Python.
- Links in the text point to the unit where a topic is covered properly. You can follow them, or carry on and come back later.
How to practise
These are suggestions, not rules. Take the ones that help you.
- You learn more when you type the examples than when you paste them, at least for the first few units. It also helps to change something and predict what will happen before you run it.
- A scratch file per unit, and a virtual environment for the course, keep your practice code tidy.
- The exercises teach most when you try them before reading the solutions. If you get stuck, you can reread the section, then look at only the first few lines of the solution.
- Every exercise is optional. Its statement describes what a finished answer does, so you can check your own work against it.
- When a program fails, the traceback is easiest to read from the bottom up. Errors and exceptions shows you how.