Step 3 of 6

Read the data

You have the forecast. Let’s pick it apart.

Goal for this lesson: print the first 12 hours of the forecast in a neat table.

What’s inside hourly

The hourly part holds two matching lists: time and temperature_2m. The first time goes with the first temperature, the second with the second, and so on. Python’s zip walks along both lists together.

Step 1: Update weather.py

Keep URL and get_forecast as they are. Replace everything from if __name__ down with the new function and main block, so the whole file looks like this:

import requests

URL = (
    "https://api.open-meteo.com/v1/forecast"
    "?latitude=29.42&longitude=-98.49"
    "&hourly=temperature_2m&temperature_unit=fahrenheit&forecast_days=2&timezone=auto"
)


def get_forecast():
    response = requests.get(URL, timeout=10)
    response.raise_for_status()
    return response.json()


def hourly_readings(data, count=12):
    hourly = data["hourly"]
    return list(zip(hourly["time"], hourly["temperature_2m"]))[:count]


if __name__ == "__main__":
    for time, temp in hourly_readings(get_forecast()):
        print(f"{time}  {temp:.1f} °F")

Step 2: Run it

python3 weather.py

You’ll see 12 lines, each with a time and a temperature.

Check yourself

Change count=12 to count=5 in the function and run it again. You should see 5 lines. Change it back when you’re done.