Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 65

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.

💡 You need a free API key (for example from OpenWeatherMap) to call the weather service.

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}")
Temperature in Delhi: 31 C
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)
City not found

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 data first 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 requests library 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?
It calls a weather API over the internet using the 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?
An API key is a personal code that identifies you to the service, like OpenWeatherMap. You sign up for free to get one, and include it in your request. Without it the API will refuse to answer.
How do I read the JSON response in Python?
Call 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?
The requests library is the standard choice. Install it with pip install requests, then use requests.get(url) to fetch data from the API.
How do I handle a city that is not found?
Check the HTTP status code — 200 means success, 404 means the city was not found. Handle non-200 responses with a friendly message instead of crashing.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 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.
Next topicWeb Scraping (BeautifulSoup) Your progress: 65 / 66 (98%) · Lesson 66 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free