AW Dev Rethought

⚖️ There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies - C.A.R. Hoare

⚡ Python Power Shots – 🌤️ Get Live Weather Info


Description:

📌 Introduction

Want to check the weather right from your terminal? ☁️

With just a few lines of Python, you can fetch and display live weather updates for any city — no setup, no API keys.

This Power Shot uses the public wttr.in API, a lightweight web service that returns weather data as plain text or JSON.


🔎 Explanation

  • requests.get() → fetches live data from the URL https://wttr.in/?format=j1.
  • The JSON response includes temperature, humidity, weather conditions, and more.
  • You can print a clean summary directly in the terminal.
  • Works globally for any city — just change the location name.

✅ Key Takeaways

  • 🌤️ Get real-time weather data in seconds.
  • ⚙️ Works without API keys or sign-ups.
  • 🌍 Fetch data for any city worldwide using simple HTTP requests.

Code Snippet:

# Import requests to make HTTP requests
import requests

# --- Step 1: Define the city you want weather info for ---
city = "Pune"  # You can replace this with any city name

# --- Step 2: Fetch weather data from wttr.in ---
url = f"https://wttr.in/{city}?format=j1"
response = requests.get(url)

# --- Step 3: Check if the request was successful ---
if response.status_code == 200:
    data = response.json()

    # --- Step 4: Extract useful information ---
    current = data["current_condition"][0]
    temperature = current["temp_C"]
    feels_like = current["FeelsLikeC"]
    humidity = current["humidity"]
    weather_desc = current["weatherDesc"][0]["value"]

    # --- Step 5: Display the weather report ---
    print(f"🌍 Weather in {city}:")
    print(f"🌡️ Temperature: {temperature}°C (Feels like {feels_like}°C)")
    print(f"💧 Humidity: {humidity}%")
    print(f"☁️ Condition: {weather_desc}")

else:
    print("⚠️ Failed to fetch weather data. Please check your city name or internet connection.")

Link copied!

Comments

Add Your Comment

Comment Added!