Working with sample data avoids network dependency while still exercising real analysis skills. You will analyze one month of daily weather records and produce a report.
What you will build#
Given records with a day, high temperature, low temperature, and rainfall, the program will:
- Print the number of days.
- Compute the average high and low.
- Find the hottest and coldest days.
- Total the rainfall.
- Count rainy days above a threshold.
- Print a simple text chart of daily highs.
Sample data#
records = [
{"day": 1, "high": 18, "low": 9, "rain": 0.0},
{"day": 2, "high": 21, "low": 11, "rain": 2.4},
{"day": 3, "high": 19, "low": 10, "rain": 0.0},
{"day": 4, "high": 16, "low": 8, "rain": 8.1},
{"day": 5, "high": 23, "low": 12, "rain": 0.0},
{"day": 6, "high": 25, "low": 14, "rain": 0.0},
{"day": 7, "high": 22, "low": 13, "rain": 1.2},
]Use a longer list for practice, but keep the structure identical. A list of dictionaries is easy to read and maps naturally to JSON later.
Averages#
if not records:
raise SystemExit("No weather records supplied")
average_high = sum(record["high"] for record in records) / len(records)
average_low = sum(record["low"] for record in records) / len(records)
print(f"Average high: {average_high:.1f}")
print(f"Average low: {average_low:.1f}")The guard prevents division by zero. SystemExit ends the script cleanly; in a function, you might return None or raise a custom exception instead.
Extremes#
hottest = max(records, key=lambda record: record["high"])
coldest = min(records, key=lambda record: record["low"])
print(f"Hottest day: day {hottest['day']} ({hottest['high']}°)")
print(f"Coldest day: day {coldest['day']} ({coldest['low']}°)")max() and min() return the whole record, not just the number. That lets the report mention the day.
Ties return the first record encountered. If you need all tied days, filter for records matching the extreme value.
Rainfall#
total_rain = sum(record["rain"] for record in records)
rainy_days = [record for record in records if record["rain"] > 0]
heavy_days = [record for record in records if record["rain"] >= 5]
print(f"Total rainfall: {total_rain:.1f} mm")
print(f"Days with rain: {len(rainy_days)}")
print(f"Heavy rain days: {len(heavy_days)}")Define the threshold in one named constant so the rule is visible:
HEAVY_RAIN_MM = 5.0Text chart#
A horizontal bar chart is a good exercise in string formatting:
print("\nDaily highs")
for record in records:
bar = "#" * record["high"]
print(f"{record['day']:>2} | {bar} {record['high']}°")The bar length equals the temperature, which is fine for values around 20. For a wider range, scale the bar:
width = 40
highest = max(record["high"] for record in records)
bar_length = round(record["high"] / highest * width)Guard against a highest value of zero before dividing.
Validation#
Real data can be missing or malformed. For this fixed dataset, validate the shape before analysis:
required = {"day", "high", "low", "rain"}
for record in records:
missing = required - record.keys()
if missing:
raise ValueError(f"Record is missing {sorted(missing)}")
if record["high"] < record["low"]:
raise ValueError(f"High is below low on day {record['day']}")
if record["rain"] < 0:
raise ValueError(f"Rainfall cannot be negative on day {record['day']}")Validation code is not glamorous, but it prevents meaningless averages.
Extending the analysis#
- Group by week and print a summary per week.
- Count days above 20° using a comprehension.
- Compute the difference between high and low for each day and find the largest swing.
- Add a second dataset for another month and compare averages.
- Read records from JSON text with
json.loadsand validate the type of each field.
Testing checklist#
- Run with the seven-day sample and verify each number by hand.
- Try a single-day list.
- Test an empty list and confirm a clear message.
- Check the tie behaviour for hottest day.
- Break one record's fields deliberately and confirm validation catches it.
