Computer Science
Grade 8
20 min
Libraries and APIs
Libraries and APIs
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a library is in programming and explain its purpose.
Differentiate between a library and an API using a clear analogy.
Use the `import` statement in Python to include a standard library like `random` or `math`.
Call functions from an imported library to solve a problem (e.g., `random.randint()`).
Explain what an API (Application Programming Interface) is and how it allows programs to communicate.
Make a simple request to a web API using the `requests` library and print the response.
Read basic API documentation to find an endpoint and understand the data it returns.
Ever wonder how your weather app knows the forecast or how a game creates random enemies? 🧙♂️ It's not magic, it's code!
In this lesson, you'll discover how to...
2
Key Concepts & Vocabulary
TermDefinitionExample
LibraryA collection of pre-written code, like functions and classes, that you can use in your own programs to perform common tasks.The Python `random` library has code to generate random numbers, so you don't have to write the complex math for it yourself.
ModuleA single file containing Python code (functions, classes, variables) that can be imported into another program. A library is often a collection of modules.`random` is a module within Python's standard library. You use `import random` to access its contents.
API (Application Programming Interface)A set of rules and tools that allows different software applications to communicate with each other. It's like a menu at a restaurant that lets you order food from the kitchen without knowing how to coo...
3
Core Syntax & Patterns
The `import` Statement
import library_name
Use this at the beginning of your Python file to make all the functions and classes from a library available to your program.
Dot Notation for Functions
library_name.function_name(arguments)
After importing a library, use the library's name, a dot, and then the function's name to call a specific function from that library.
The `requests.get()` Pattern
response = requests.get("API_ENDPOINT_URL")
Use the `get()` function from the `requests` library to ask a web server for data from a specific API endpoint. The server's reply is stored in the `response` variable.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A student's code to fetch a joke is `import requests; response = requests.get(URL); print(response['setup'])`. This code fails with a `TypeError`. Why?
A.The `requests` library was not imported correctly.
B.The URL was probably typed incorrectly.
C.They tried to treat the Response object like a dictionary before parsing it with `.json()`.
D.The `print` function cannot be used with API data.
Challenging
You have a list of activities: `tasks = ["Homework", "Clean room", "Read book"]`. You want your program to randomly choose and print one of these tasks. Which code snippet is the best way to do this?
A.import random; print(random.choice(tasks))
B.import math; print(math.select(tasks))
C.import requests; print(requests.get(tasks))
D.import random; print(random.randint(0, 2))
Challenging
An API returns the following JSON: `{"data": {"planets": [{"name": "Earth"}, {"name": "Mars"}]}}`. If the parsed data is in a variable `info`, how would you print the string "Mars"?
A.print(info['planets'][1]['name'])
B.print(info['data']['planets']['name'][1])
C.print(info['data'][0]['planets'][1])
D.print(info['data']['planets'][1]['name'])
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free