courses / python / level 2

level 2 · medium

intermediate
python.

Build useful programs with dictionaries, files, errors, modules, classes, JSON and tests. Every example runs.

  • 28 lessons
  • 7 units
  • 4 h 29 m in all
  • £0, for good

what’s inside.

For people who finished Level 1, or can already write small programs with variables, decisions, loops and functions.

Python Level 2 picks up where Level 1 ended and builds things that are useful. In 28 lessons you organise data with dictionaries, tuples and sets, shape text, save files and CSVs, handle errors on purpose, borrow from the standard library, read pretend web replies, and write your own classes and tests. It ends with a level check: a budget tracker that saves and reads its own CSV file.

every lesson goes the same way.

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

unit 1

Look it up

Store values under names and find any of them in a flash.

4 lessons · 37 min

  1. A dictionary looks things up9 minStore values under names and fetch any one of them by its name.
  2. Changing a dictionary9 minAdd, replace and remove pairs, and check what is there first.
  3. Looping over a dictionary9 minVisit every pair in a dictionary and add up its numbers.
  4. Counting with a dictionary10 minTally votes, colours or spending, one category at a time.

unit 2

Tuples, sets and sorting

Pick the right shape for your data and put it in order.

4 lessons · 37 min

  1. Tuples keep things together9 minGroup values that belong together and unpack them in one line.
  2. Putting things in order8 minSort numbers and words, smallest first or biggest first.
  3. Sorting by your own rule10 minSort records by price, rating or anything else you choose.
  4. Sets keep one of each10 minDrop duplicates and compare two groups in a single line.

unit 3

Text in depth

Cut text up, glue it back together and make numbers look neat.

4 lessons · 36 min

  1. Splitting text into pieces9 minCut a line of text into a list of words or values.
  2. Joining a list into text8 minTurn a list back into one neat line, with the separator you choose.
  3. Formatting numbers9 minShow money with two decimal places, big numbers with commas, and percentages.
  4. Lining up columns10 minPad text and numbers to a fixed width for neat tables and receipts.

unit 4

Errors on purpose

Catch errors, raise your own and prove your code works.

4 lessons · 38 min

  1. Catching an error10 minStop a crash in its tracks and let your program carry on.
  2. else and finally9 minRun code only when nothing went wrong, and clean up whatever happens.
  3. Raising your own errors10 minRefuse bad values with a clear message, and catch it where you choose.
  4. Checking with assert9 minProve a function gives the right answers before you trust it.

unit 5

The standard library

Borrow ready-made tools for maths, chance, dates and data.

4 lessons · 37 min

  1. Importing a module8 minBorrow ready-made tools that come with Python, starting with maths.
  2. Random with a seed9 minRoll dice and pick winners, and make the results repeat when you test.
  3. Working with dates10 minCount the days to an event and print dates the British way.
  4. JSON: data as text10 minTurn dictionaries and lists into text any program can read, and back again.

unit 6

Files and the web

Save data in files and read replies from the web.

4 lessons · 41 min

  1. Writing a file10 minSave text to a file so it is still there after the program ends.
  2. Reading a file9 minRead a whole file at once, or work through it line by line.
  3. CSV files11 minSave rows of data that spreadsheets can open, and read them back.
  4. Talking to the web11 minBuild a web address that asks a question, and read the JSON reply.

unit 7

Your own objects

Design your own kinds of value, then test them like a professional.

4 lessons · 43 min

  1. A class is a blueprint10 minDesign your own kind of value, then make as many as you like.
  2. Methods and __str__10 minTeach your objects to do things and to describe themselves.
  3. Inheritance11 minBuild a new class on top of an old one, and change only what is different.
  4. Testing with unittest12 minGroup your tests into a test case and let unittest run them all.
end of level 2 level check: a budget tracker Build one small project with everything from this level. Pass it and your face levels up. see the check

next, level 3

advanced
python.

Write Python like a professional: generators, decorators, async, tests and safe tools you can ship. Every example runs.

see level 3