Solving Problems with Built-in Python Types

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

To be effective with Python we need to be comfortable using the basic data types that Python has to offer. In this Hands-On Lab, we’ll be utilizing basic types to fix some failing automated tests, and ensuring that the application is working correctly. By the time we’re finished with this hands-on lab we should be more comfortable using the different data types in Python, like strings, numbers, dictionaries, and lists.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Implement `print_todo` function

The print_todo function needs to print out a string that uses the name and body values from a dictionary. To do this, we can utilize the print function and an f-string (using Python 3.6 and higher). Here’s one way we could implement this function:

def print_todo(todo):
    print_todo takes in a todo dictionary and prints it out
    with by separating the `name` from the `body` using a colon (:).

    >>> todo = {'name': 'Example 1', 'body': 'This is a test task', 'points': '3'}
    >>> print_todo(todo)
    Example 1: This is a test task
    print(f"{todo['name']}: {todo['body']}")

If we run the tests, we should see there are now no failures related to print_todo.

Implement `take_first` function

The take_first function needs to return two objects to us, in the form of the first todo and the remaining todos. We can achieve this by returning a tuple of values, and the pop function on a list should allow us to get the first item. Here’s one way we might implement this function to get the tests passing:

def take_first(todos):
    take_first receives a list of todos and removes the first todo
    and returns that todo and the remaining todos in a tuple

    >>> todos = [{'name': 'Example 1', 'body': 'This is a test task', 'points': '3'},
    ... {'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'}]
    >>> todo, todos = take_first(todos)
    >>> todo
    {'name': 'Example 1', 'body': 'This is a test task', 'points': '3'}
    >>> todos
    [{'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'}]
    todo = todos.pop(0)
    return (todo, todos)
Implement `sum_points` function

The sum_points function needs to receive two todo dictionaries and return the sum of the point values. The doctest shows the point values will be strings, so we’ll need to convert them to be integers before we perform our addition. Here’s one way we could get these tests passing:

def sum_points(todo1, todo2):
    sum_points receives two todo dictionaries and returns the sum of their `point` values.

    >>> todos = [{'name': 'Example 1', 'body': 'This is a test task', 'points': '3'},
    ... {'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'}]
    >>> sum_points(todos[0], todos[1])
    return int(todo1['points']) + int(todo2['points'])

Additional Resources

We've been tasked with building a terminal-based todo list application, and it's in its earliest stages. A co-worker has written and documented a few functions we'll need to implement. By the time we've finished, we should have written implementations for the print_todo, take_first, and sum_points functions within ~/tasker/

Thankfully, our co-worker documented the code using doctest, so we have some automated tests we can run to ensure our implementation meets the requirements. To run the tests, we'll use the following command from within the ~/tasker directory:

$ python3.7 -m doctest -v
3 items had failures:
   1 of   2 in tasker.print_todo
   2 of   2 in tasker.sum_points
   3 of   4 in tasker.take_first
8 tests in 4 items.
2 passed and 6 failed.
***Test Failed*** 6 failures.

By the time we've implemented these functions, we'll have proven our knowledge of some of Python's built-in types such as strings, integers, dictionaries, lists, and tuples.

More information about formatted string literals, or "f-strings", can be found here.

To enter Insert mode in vim, hit esc then i. To save the file, use esc then :wq!

What are Hands-on Labs

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!