Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Modeling Data with Classes in Python

Python is an object-oriented programming language, and lends itself to modeling problems using objects. In this hands-on lab, we'll be implementing a few different classes in order to create a todo list. The project has been documented with automated tests to help us verify that the code we've written will meet the requirements. By the time we're finished with this lab, we should be more comfortable creating classes and implementing methods on those classes.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Beginner
Last updated
Sep 21, 2025
Duration
1h 0m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Implement the `Todo` Class

    The Todo class holds on to a few pieces of information:

    • name: The name of the todo
    • description: The description of the todo
    • points: The difficulty/importance rating as an integer greater than zero
    • completed: Whether or not the todo has been completed as a boolean

    Our first step will be to remove the pass line and implement the __init__ method:

    class Todo:
        # doctest omitted
    
        def __init__(self, name, description, points, completed=False):
            self.name = name
            self.description = description
            self.points = points
            self.completed = completed
    

    We only needed to specify one default for completed, and the rest of the arguments are required.

    Next, we'll need to customize how the object is printed out. We can do this by implementing either the __str__ method or the __repr__. The default implementation for __str__ will use __repr__. But since we also want our todos to represent themselves well when we just return them, we'll implement __repr__.

    We also need to add a class-level variable to allow us to convert the boolean value of completed into a readable string.

    class Todo:
        # doctest and __init__ omitted
    
        statuses = { False: 'Incomplete', True: 'Complete' }
    
        def __repr__(self):
            return f"{self.name} ({self.statuses[self.completed]} - {self.points} points): {self.description}"
    

    With these two methods implemented, we have a working Todo class.

  2. Challenge

    Implement the `TodoList` Class

    The TodoList class only receives one argument and implements more functionality:

    • todos: A list of Todo objects

    Our first step will be to remove the pass line and implement the __init__ method:

    class TodoList:
        # doctest omitted
    
        def __init__(self, todos):
            self.todos = todos
    

    It doesn't get much simpler than that.

    Next, we need to implement the average_points function that will calculate the average of the points for all of the Todo objects. The formula for calculating the average is sum_of_points / number_of_todos, so let's write that now:

    class TodoList:
        # doctest and __init__ omitted
        def average_points(self):
            total = 0
            for todo in self.todos:
                total += todo.points
            return total / len(self.todos)
    

    Finally, we need to implement two methods that return subsets of our todos: the completed and incomplete methods. These methods will be almost exactly the same except we'll filter them with a different conditional:

    class TodoList:
        # doctest and other methods omitted
    
        def completed(self):
            results = []
            for todo in self.todos:
                if todo.completed:
                    results.append(todo)
            return results
    
        def incomplete(self):
            results = []
            for todo in self.todos:
                if not todo.completed:
                    results.append(todo)
            return results
    

    With all of these methods implemented, we should have all of the doctests passing, and our classes working as expected.

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight