Working with APIs in Python
APIs (Application Programming Interfaces) allow you to interact with other software systems. In Python, the requests library is commonly used to interact with APIs.
Making API Requests
Here’s how you can make a simple GET request to an API:
import requests
response = requests.get("https://api.github.com/users/octocat")
data = response.json() # Parse JSON response
print("User data:", data)
This code fetches the data from GitHub's API and parses the JSON response to extract user information.
Activity
Try It Yourself!
Write a program that fetches the weather data from a public weather API and prints the current temperature in your city.
Quick Quiz
Quick Quiz
- What library is used to make HTTP requests in Python?
- How do you parse a JSON response from an API?
Answers: The requests library is used to make HTTP requests. You can parse JSON responses with the response.json() method.