Skip to content
BetaDE
Lesson 22 of 35Python

Python · Lesson 22 · 45 minutes

Project: weather station

What you'll learn

  • Store measurements in lists and go through them in pairs with zip()
  • Calculate the average, minimum and maximum
  • Show data as a bar chart made of text

Collecting measurements

A weather station measures all the time: temperature, humidity, wind. A robot does the same with its sensors. Measurements almost always end up in a list, and Python has ready-made tools to analyse them:

temperatures = [16, 19, 24, 21, 17, 15, 18]

print("Highest:", max(temperatures))
print("Lowest:", min(temperatures))
print("Sum:", sum(temperatures))
print("Count:", len(temperatures))
print("Average:", sum(temperatures) / len(temperatures))

The average has lots of decimal places. With round(number, 1) only one is left.

Two lists at once: zip()

The days and the temperatures are in two lists that belong together. zip() puts them side by side like a zip fastener:

days = ["Mon", "Tue", "Wed"]
rain = [0, 5, 2]

for day, mm in zip(days, rain):
    print(day, "→", mm, "mm of rain")

# On which day did it rain most?
number = rain.index(max(rain))
print("Wettest day:", days[number])

Charts made of text

You don’t need graphics for a chart. A text times a number repeats the text – and your bar is ready:

print("#" * 5)
print("=" * 12)

points = {"Ida": 7, "Ben": 4, "Mo": 9}
for name, p in points.items():
    print(f"{name:4} {'█' * p} {p}")

{name:4} pads the name to 4 characters. That way all bars start at the same place.

How many summer days?

Often you want to count how many values meet a condition. A short loop or sum() with a condition does the job:

temperatures = [16, 19, 26, 27, 17, 25, 18]

summer_days = sum(1 for t in temperatures if t >= 25)
print("Summer days:", summer_days)

warm = [t for t in temperatures if t >= 20]
print("Warm values:", warm)

Keep building: Count the summer days, also draw the lowest temperatures or save the measurements as a CSV file (one line per day, values separated by commas) – like in the lesson about files. That’s exactly how you’ll later record your robot’s measurements and open them in a spreadsheet.

Your task

Your turn

Analyse the temperatures of one week. Write a function average(values) that returns the average rounded to one decimal place. Then print a bar for every day – one # per degree, e.g. Mon | ################ 16. After that print Warmest day: Wed (24 °C) and as the last line Average: 18.6 °C.

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temperatures = [16, 19, 24, 21, 17, 15, 18]


def average(values):
    pass


# Bar chart and analysis

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temperatures = [16, 19, 24, 21, 17, 15, 18]


def average(values):
    return round(sum(values) / len(values), 1)


for day, degrees in zip(days, temperatures):
    print(f"{day} | {'#' * degrees} {degrees}")

highest = max(temperatures)
day = days[temperatures.index(highest)]
print(f"Warmest day: {day} ({highest} °C)")
print(f"Average: {average(temperatures)} °C")