0
0
Rest APIprogramming~15 mins

Why REST APIs exist - See It in Action

Choose your learning style9 modes available
Understanding Why REST APIs Exist
📖 Scenario: Imagine you want to build a simple app that shows weather information from different cities. To get this data, your app needs to talk to a server that stores weather details. REST APIs help your app and the server communicate smoothly.
🎯 Goal: Learn why REST APIs exist by creating a simple example that shows how a client can request data from a server using REST principles.
📋 What You'll Learn
Create a simple data structure to hold weather information
Set up a configuration variable for the city to look up
Write a function that simulates a REST API call to get weather data for the city
Print the weather information for the chosen city
💡 Why This Matters
🌍 Real World
REST APIs are used everywhere to let apps and websites talk to servers and get data like weather, news, or user info.
💼 Career
Understanding REST APIs is key for software developers, web developers, and anyone working with modern apps that need to share data.
Progress0 / 4 steps
1
Create the weather data dictionary
Create a dictionary called weather_data with these exact entries: 'New York': 'Sunny', 'London': 'Cloudy', 'Tokyo': 'Rainy'
Rest API
Need a hint?

Use curly braces to create a dictionary with city names as keys and weather descriptions as values.

2
Set the city to look up
Create a variable called city and set it to the string 'London'
Rest API
Need a hint?

Assign the string 'London' to the variable city.

3
Write a function to simulate a REST API call
Define a function called get_weather that takes a parameter city_name and returns the weather from weather_data for that city using weather_data.get(city_name, 'No data')
Rest API
Need a hint?

Use the dictionary's get method to safely get the weather or return 'No data' if the city is not found.

4
Print the weather for the chosen city
Write a print statement that shows the text "Weather in {city}: {weather}" where weather is the result of calling get_weather(city)
Rest API
Need a hint?

Call get_weather with city and use an f-string to print the message.