Home › Learn › Python

Python

40 lessons, 8 modules, Python running in your browser.

Start the course

This is a working Python, not a quiz about Python. Every lesson has a short explanation, a task, and an editor; you write real code, press Run, and CPython (compiled to WebAssembly) runs it right there in the tab and checks the result. The checks tell you what they expected and what they got, hints are one click away, and the solution is there when you want to compare.

The course goes from printing a line to writing classes, in the order the ideas build on each other: values and variables, decisions, loops, the four collection types, strings in depth, functions in depth, and errors and classes. It ends in a project, a text statistics report, that is not started from nothing: one lesson in each module builds one of its parts, saved in this browser as you go, and the project imports them as a module of your own. Forty lessons of ten minutes or so each; you can stop and pick up later.

What you will learn

The final project

The course ends with Project: text statistics: a text statistics report assembled from your own functions. 8 of the lessons build a part of it, saved in this browser as you go, so the project is assembled from your own work rather than started from nothing.

  1. PUNCTUATION (Variables) is the set of characters stripped from the ends of words before they are counted.
  2. word_kind (Functions) sorts a word into short, medium, or long, so the report can count each kind.
  3. count_vowels (for loops and range) counts the vowels in the raw text for one line of the report.
  4. longest_words (Sorting) picks the longest words for the report, longest first and alphabetical among ties.
  5. word_counts (Counting things) turns the list of words into a dictionary of word to count.
  6. clean_words (String methods) turns raw text into the list of lower-case words everything else counts.
  7. sentences (Generators) yields the sentences of a text one at a time, for the sentence count.
  8. Report (Classes) holds the numbers the project computes and prints the summary line.

Lessons

First steps

  1. Hello, worldPrint text with print() and see how the lessons check your work.
  2. VariablesStore values under names and use them later.part: PUNCTUATION
  3. Numbers and mathAdd, divide, take remainders, and raise to powers.
  4. Strings and f-stringsJoin text, put values inside text, and pick characters out.
  5. Reading error messagesLearn to read a traceback by fixing three broken lines.

Making decisions

  1. Comparisons and booleansAsk questions about values and combine the answers.
  2. if and elseRun a block of code only when a condition holds.
  3. elif chainsPick one of several outcomes in order.
  4. FunctionsPackage code under a name, give it inputs, get a result back.part: word_kind
  5. The match statementCompare one value against several patterns.

Loops

  1. for loops and rangeRepeat a block for each number in a range.part: count_vowels
  2. while loopsRepeat until a condition stops being true.
  3. break and continueLeave a loop early or skip one round.
  4. Nested loopsA loop inside a loop covers every combination.
  5. FizzBuzzThe classic loop-and-conditions exercise, done properly.

Lists

  1. ListsOrdered collections you can grow, shrink, and index.
  2. Iterating and enumerateVisit every item, with or without its position.
  3. SlicingTake part of a list or string with start:stop:step.
  4. List comprehensionsBuild a list from another in one readable line.
  5. SortingOrder items by any rule with sorted() and key=.part: longest_words

Dictionaries, tuples, and sets

  1. DictionariesLook values up by key instead of by position.
  2. Looping over dictionariesVisit keys, values, or both.
  3. Tuples and unpackingFixed groups of values, and pulling them apart.
  4. SetsUnordered collections with no duplicates, fast membership.
  5. Counting thingsTally items with a dictionary, then meet Counter.part: word_counts

Strings in depth

  1. String methodsSplit, join, strip, replace, and search text.part: clean_words
  2. Formatting numbersControl decimals, thousands separators, and padding in f-strings.
  3. Characters and reversingWalk through a string one character at a time.
  4. Regular expressionsFind and replace patterns in text with the re module.
  5. Converting typesTurn text into numbers safely and back again.

Functions in depth

  1. Default and keyword argumentsMake parameters optional and calls self-describing.
  2. *args and **kwargsAccept any number of positional or named arguments.
  3. lambda, map, and filterSmall anonymous functions and the tools that take them.
  4. RecursionA function that calls itself on a smaller problem.
  5. GeneratorsProduce values one at a time with yield.part: sentences

Errors, classes, and a project

  1. try and exceptCatch errors and decide what happens instead of crashing.
  2. Raising errorsRefuse bad input loudly with raise.
  3. ClassesBundle data and the functions that work on it.part: Report
  4. Inheritance and dunder methodsBuild a class on top of another and control how objects print.
  5. Project: text statisticsAssemble the parts you built along the way into a report on a paragraph of text.final project