Week 9 of 106 to 7 hours5 exercises

Week 9: Modules and classes

Borrow ready-made tools from the standard library, then model real things with your own classes.

By the end of this week you can

  • Import and use standard-library modules
  • Create your own module-style helper functions
  • Write a class with attributes and methods
  • Know when a class helps and when a dictionary is enough

Modules: tools you can import

A module is a file of ready-made Python code. The standard library ships with Python and covers a huge range: math, random, datetime, collections, json, statistics, re and many more. import brings one in. You can import the whole module and write module.name, or pull in just the pieces you need with from module import name.

When you write your own program in a file, other files can import your functions in the same way. Splitting code into small files by topic keeps programs readable.

Counter is a dictionary made for counting. most_common(2) gives the two most frequent items.

Dates and times

The datetime module handles dates. date(2026, 12, 25) makes a date, and subtracting two dates gives a timedelta that has a .days count. strftime turns a date into text in any layout you like, such as %d %B %Y for 25 December 2026.

Dates are objects with their own methods, just like strings and lists.

Classes: your own kinds of object

You have already used objects: every string, list and dictionary is one, with its own methods. A class lets you define your own kind. It bundles data (attributes) with the actions that work on that data (methods).

__init__ runs when you create an object and sets up its attributes. The first parameter of every method is self, the object itself. Create objects by calling the class like a function.

__str__ decides what print shows for the object.

Inheritance, briefly

A class can build on another by naming it in brackets. The new class gets everything from the parent and can add or replace methods. Use it when one thing really is a kind of another thing, such as a SavingsAccount being a BankAccount. If you are only reusing a few lines, a function is simpler.

Not everything needs a class. For a simple record with a few fields, a dictionary is often enough. Reach for a class when the data and the rules about it belong together.

Each class answers speak() in its own way. Dog and Cat reuse __init__ from Animal.

Common mistakes

  • Forgetting self in a method definition or when reading an attribute inside it (balance instead of self.balance).
  • Naming your own file the same as a module you want to import, such as random.py, which then hides the real one.
  • Creating a class when a dictionary or a function would be simpler.
  • Putting a list as a class-level value so every object shares it; set it in __init__ instead.

Quick recap

  • import brings in modules; the standard library covers a lot.
  • A class groups data (attributes) and actions (methods).
  • __init__ sets up a new object; self is the object itself.
  • Choose a class only when it makes the code simpler.

Practise this week

Work through the 5 exercises for Week 9. Each one tells you which tests pass, and has a hint and a solution if you get stuck.

Open Week 9 exercises

Check your understanding

Pick an answer for each question, then press Check answers.

  1. What does from math import sqrt let you do?
  2. What is self in a method?
  3. When does __init__ run?
  4. class Dog(Animal): means:
  5. Which is the better tool for a record with just name and marks, no behaviour?

This course is for learning. The Python engine runs in your browser and your code is never sent to us.