🐍 Lesson 43: Weather App (Python Project)
1. Project Overview
In short → A Weather App is a Python project that's great for practicing API integration. As soon as the user types in a city name, the app shows that city's temperature, conditions, and more.
In detail → A Weather App is a great beginner project to practice API integration. The user enters a city name, and the app fetches weather data like temperature and conditions.
2. Why Build a Weather App?
- You get to practice connecting to an API (Application Programming Interface)
- You learn how to parse JSON data
- You can try out both the CLI (Command Line Interface) version and the GUI version
3. Summary
✅ Weather App = API integration practice project
✅ Use OpenWeatherMap API or similar
✅ Parse JSON response data
✅ CLI and GUI versions possible
python
# ===== 1. Weather App Setup =====
import requests
API_KEY = "YOUR_API_KEY" # Replace with actual API key
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
# ===== 2. CLI Version =====
print("===== CLI Weather App =====")
def get_weather(city):
url = f"{BASE_URL}?q={city}&appid={API_KEY}&units=metric"
try:
response = requests.get(url)
data = response.json()
if data["cod"] == 200:
temp = data["main"]["temp"]
desc = data["weather"][0]["description"]
humidity = data["main"]["humidity"]
return f"Temperature: {temp}°C\nCondition: {desc}\nHumidity: {humidity}%"
else:
return "City not found!"
except Exception as e:
return f"Error: {e}"
# Example usage (commented - requires API key)
# city = input("Enter city: ")
# print(get_weather(city))
# ===== 3. Error Handling =====
print(f"\n===== Error Handling =====")
print("✅ Check API response status code")
print("✅ Handle network errors (try...except)")
print("✅ Validate city name exists")
# ===== 4. API Response Structure =====
print(f"\n===== JSON Response Structure =====")
print("Response contains:")
print("- main.temp → temperature")
print("- weather[0].description → condition")
print("- main.humidity → humidity")
print("- cod → status code (200 = success)")
# ===== 5. Notes =====
print(f"\n===== Setup Notes =====")
print("1. Get free API key from openweathermap.org")
print("2. Install: pip install requests")
print("3. Replace 'YOUR_API_KEY' with actual key")
print("4. API has rate limits (free tier)")You should see
===== CLI Weather App ===== ===== Error Handling ===== ✅ Check API response status code ✅ Handle network errors (try...except) ✅ Validate city name exists ===== JSON Response Structure ===== Response contains: - main.temp → temperature - weather[0].description → condition - main.humidity → humidity - cod → status code (200 = success) ===== Setup Notes ===== 1. Get free API key from openweathermap.org 2. Install: pip install requests 3. Replace 'YOUR_API_KEY' with actual key 4. API has rate limits (free tier)