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.

First steps

A program, variables and numbers.

  1. Your first program — print, text in quotes and the order of execution.
  2. Variables — A name refers to a value and can be rebound.
  3. Numbers — int and float, two kinds of division, remainder and rounding.

Strings

Indexes, slices, methods and formatting.

  1. Indexes and slices — Accessing characters, slices and string length.
  2. String methods — strip, split, join, replace and changing case.
  3. Formatting — f-strings, decimal places and alignment.

Conditions and loops

if, while and for: the program makes decisions and repeats actions.

  1. Conditions — if, elif, else and the truth value of things.
  2. The while loop — Repeat while the condition holds; break and continue.
  3. The for loop and range — Going through a sequence and a range of numbers.

Lists and tuples

Mutable and immutable sequences.

  1. Lists — Creating lists, indexes, adding and removing elements.
  2. Sorting and copies — sort and sorted, min, max and sum.
  3. Tuples and unpacking — Immutable pairs and several values at once.

Dictionaries and sets

Lookup by key, unique values and comprehensions.

  1. Dictionaries — Key and value, get, looping over items.
  2. Sets — Unique values and operations on collections.
  3. Comprehensions — Lists, dictionaries and sets in a single line.

Functions

def and return, arguments, scope and lambda.

  1. Functions and return — Your own function, parameters and the return value.
  2. Arguments — Keyword arguments, default values, *args.
  3. Scope and lambda — Local names, functions as values, lambda.

Iterating like a pro

enumerate, zip, sorting by key and generators.

  1. enumerate and zip — The position of an element and looping in parallel.
  2. Sorting by key — key, sorting by several fields and stability.
  3. Iterators and generators — yield, lazy evaluation and one-time streams.

Errors

Exceptions, handling them, your own errors and debugging.

  1. Exceptions — try, except, else and finally.
  2. Your own exceptions — raise, input validation and your own error classes.
  3. 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.

  1. Classes — class, __init__, self and methods.
  2. Special methods — __str__, __repr__, __eq__ and comparing objects.
  3. Dataclass and inheritance — Less boilerplate and shared behaviour of classes.

The standard library

Modules, collections, dates and JSON.

  1. Modules and import — math, random and your own modules.
  2. collections and datetime — Counter, defaultdict and date arithmetic.
  3. Files and JSON — with open, encoding and exchanging data as JSON.