📘 Lesson · Lesson 97
Weather API Program
About this Project
💡 At a Glance
This program fetches live weather for a city from a weather API using the requests library.
The Program
Python
import requests
city = "Delhi"
api_key = "YOUR_API_KEY"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
print("City:", data["name"])
print("Temp:", data["main"]["temp"], "C")
print("Weather:", data["weather"][0]["description"])
else:
print("City not found")City: Delhi
Temp: 32 C
Weather: clear sky
Summary
requests.get()calls the API;.json()parses the response.- Get a free API key from OpenWeatherMap to run it.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.