Course contentsLesson 13 of 13← Course overview
  1. 01Meet Python12 min
  2. 02Set up and run Python12 min
  3. 03Variables and data types15 min
  4. 04Strings and input15 min
  5. 05Decisions with conditionals15 min
  6. 06Repeat work with loops15 min
  7. 07Functions and return values18 min
  8. 08Work with lists16 min
  9. 09Dictionaries and tuples16 min
  10. 10Understand errors and exceptions14 min
  11. 11Read and write files16 min
  12. 12Modules and the standard library14 min
  13. 13Build a small data summary20 min

Python & data · Lesson 13

Build a small data summary

Edit lesson

Combine lists, dictionaries, loops, and functions to answer a question about a small dataset.

20 minProgress saves on this device
In this lesson
  • Read a small collection of records
  • Compute a total and average
  • Handle an empty collection

Helpful background: Understand loops, functions, lists, and dictionaries.

Start with a question

Suppose you want to know how many visitors came to a park over three days. Each dictionary represents one day, and each visitors value is a non-negative count.

Real datasets can contain missing or invalid values. This small example assumes each record has a visitors key holding a number.

A small datasetpython
days = [
    {"day": "Monday", "visitors": 12},
    {"day": "Tuesday", "visitors": 18},
    {"day": "Wednesday", "visitors": 15},
]
print(len(days))  # 3

Calculate a total

Start at zero and add each record's visitors value. For these three days, the total is 45.

Put reusable logic in a function so you can try it with other collections.

Sum the recordspython
def total_visitors(records):
    total = 0
    for record in records:
        total += record["visitors"]
    return total

sample = [{"visitors": 12}, {"visitors": 18}, {"visitors": 15}]
print(total_visitors(sample))  # 45

Calculate an average safely

An average is the total divided by the number of records. Dividing by zero raises ZeroDivisionError, so decide what an empty collection should mean before calculating.

This example returns None when there are no records. A caller can then show a helpful message instead of treating missing data as zero.

Handle an empty datasetpython
def average_visitors(records):
    if not records:
        return None
    total = sum(record["visitors"] for record in records)
    return total / len(records)

print(average_visitors([{"visitors": 12}, {"visitors": 18}]))  # 15.0
print(average_visitors([]))  # None

Explain the result

A useful summary answers the question and mentions what the data does not show. Three days do not establish a long-term trend, and an average alone does not explain why the counts changed.

Try a different set of counts, a single record, and an empty list. Check your result by hand before trusting the program.

Knowledge check

Why does average_visitors check for an empty list?

Try first; feedback appears after you check.
Apply it

Your turn

Add a fourth day with 20 visitors to the first dataset and calculate the new total.

BUILT TOGETHER

Help make this explanation better.

Have a clearer explanation, a useful example, or a correction? Share it with the community.

Contributors

Be the first to add your name when you improve this lesson.

How to get credited
Previous lessonCourse overview