From your first line to confident programs
A Python trail for people writing their very first program, and for those who picked up the language in bits and pieces and want to put it together properly. We start with output, variables and numbers. Then strings, conditions and loops, lists and dictionaries, functions and errors. At the end come classes, the standard library and JSON. Every coding task is checked on the server, and the explanations cover the mistakes beginners trip over most.
- Write programs with conditions, loops, lists and dictionaries
- Split code into functions and handle errors instead of crashing
- Model data with classes and use the standard library
- Read a traceback and find the bug instead of rewriting code at random
First steps
A program, variables and numbers.
- Your first program — print, text in quotes and the order of execution.
- Variables — A name refers to a value and can be rebound.
- Numbers — int and float, two kinds of division, remainder and rounding.
Strings
Indexes, slices, methods and formatting.
- Indexes and slices — Accessing characters, slices and string length.
- String methods — strip, split, join, replace and changing case.
- Formatting — f-strings, decimal places and alignment.
Conditions and loops
if, while and for: the program makes decisions and repeats actions.
- Conditions — if, elif, else and the truth value of things.
- The while loop — Repeat while the condition holds; break and continue.
- The for loop and range — Going through a sequence and a range of numbers.
Lists and tuples
Mutable and immutable sequences.
- Lists — Creating lists, indexes, adding and removing elements.
- Sorting and copies — sort and sorted, min, max and sum.
- Tuples and unpacking — Immutable pairs and several values at once.
Dictionaries and sets
Lookup by key, unique values and comprehensions.
- Dictionaries — Key and value, get, looping over items.
- Sets — Unique values and operations on collections.
- Comprehensions — Lists, dictionaries and sets in a single line.
Functions
def and return, arguments, scope and lambda.
- Functions and return — Your own function, parameters and the return value.
- Arguments — Keyword arguments, default values, *args.
- Scope and lambda — Local names, functions as values, lambda.
Iterating like a pro
enumerate, zip, sorting by key and generators.
- enumerate and zip — The position of an element and looping in parallel.
- Sorting by key — key, sorting by several fields and stability.
- Iterators and generators — yield, lazy evaluation and one-time streams.
Errors
Exceptions, handling them, your own errors and debugging.
- Exceptions — try, except, else and finally.
- Your own exceptions — raise, input validation and your own error classes.
- Debugging — How to read a traceback and look for the cause, not the symptom.
Classes and objects
Your own data types: classes, special methods, dataclass and inheritance.
- Classes — class, __init__, self and methods.
- Special methods — __str__, __repr__, __eq__ and comparing objects.
- Dataclass and inheritance — Less boilerplate and shared behaviour of classes.
The standard library
Modules, collections, dates and JSON.
- Modules and import — math, random and your own modules.
- collections and datetime — Counter, defaultdict and date arithmetic.
- Files and JSON — with open, encoding and exchanging data as JSON.