Weather App in Python using API
In this lesson: Build a weather app in Python with a free weather API and requests — city input, JSON parsing, output. In Hindi & English.
What we are building
A weather app asks the user for a city, calls a weather API over the internet, and shows the current temperature and conditions. It is a great project to learn how programs fetch live data from the web using the requests library and read JSON.
The requests library
requests is the standard Python library for talking to web APIs. Install it once:
pip install requests
Then import it in your program. It lets you fetch data from a URL with a single line, requests.get(url).
Calling the API
import requests
city = "Delhi"
key = "YOUR_API_KEY"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={key}&units=metric"
response = requests.get(url)
data = response.json()
We build the URL with the city, our API key, and units=metric for Celsius. requests.get fetches it, and .json() turns the reply into a Python dictionary.
Reading the JSON
temp = data["main"]["temp"]
weather = data["weather"][0]["description"]
print(f"Temperature in {city}: {temp} C")
print(f"Condition: {weather}")
Condition: clear sky
The JSON is a nested dictionary. We reach into it by keys — data["main"]["temp"] for temperature and data["weather"][0]["description"] for the condition.
Handling errors
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data["main"]["temp"], "C")
elif response.status_code == 404:
print("City not found")
else:
print("Error:", response.status_code)
Always check the status code. 200 means success; 404 means the city was not found; other codes mean another problem. This keeps the app from crashing on bad input.
Common mistakes
- Missing or wrong API key. The service refuses the request — sign up and use a valid key.
- Not checking the status code. A 404 or error then crashes when you read the JSON.
- Wrong JSON keys. Print the whole
datafirst to see the exact structure. - Forgetting
units=metric. Without it, temperature comes in Kelvin, not Celsius.
Summary
- A weather app fetches live data from a weather API.
- Use the
requestslibrary and a free API key. requests.get(url)fetches;.json()gives a dictionary.- Read nested keys like
data["main"]["temp"]. - Check the status code (200, 404) to handle errors gracefully.
Next, understand the data format in JSON in Python, or organise it with dictionaries.
Frequently Asked Questions
How does a Python program get weather data?
requests library, sending the city name and an API key. The API replies with JSON data, which the program reads to show the temperature and conditions.What is an API key and why do I need one?
How do I read the JSON response in Python?
response.json() to turn the reply into a Python dictionary, then read the values by their keys — for example data["main"]["temp"] for the temperature.What library do I use to call an API?
requests library is the standard choice. Install it with pip install requests, then use requests.get(url) to fetch data from the API.