Unit 3 · Programming 2 · Lesson 3.16

Libraries and APIs

Nobody writes their own square root. Libraries are collections of ready-made procedures you import and use — and reading their documentation to figure out an unfamiliar one is itself a skill the exam tests.

Big Idea 3 — Algorithms & ProgrammingWeek 4 · Standing on shoulders

Before this lesson — read on Runestone

Focus on import and calling something from a library. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs, opening Week 4. You can write procedures; now you learn to borrow them by the thousand. A library is procedural abstraction at the largest scale — someone else’s tested code, ready to call by name.

2

The Hook

You’ll never write square root. Neither will anyone good. Computing a square root correctly and fast is genuinely hard — and completely solved. So you type math.sqrt(16) and move on. Reinventing it would be a waste, and probably buggier than the version experts already perfected.

A library is a bundle of such procedures. You import it and call what you need. The real skill isn’t memorizing every one — it’s reading the documentation for a function you’ve never seen and using it correctly. That’s a named skill in the CED.

3

The Idea

A library is a collection of prewritten procedures. You bring it in with import, then call its procedures with the library’s name in front:

Python

import math
print(math.sqrt(16))   # 4.0

import random
print(random.randint(1, 6))  # a die roll

What’s happening

# bring in the math library
# call sqrt from it — you did
# not write sqrt
# bring in the random library
# call randint from it

Hover or tap a line to light its twin. math.sqrt means “the sqrt procedure that lives in math.” The library name in front tells Python where to find it.

An API (Application Programming Interface) is the menu of what a library offers and how to call it — each procedure’s name, what arguments it takes, and what it returns. You don’t need the recipe (the code inside); you need the menu (how to order). Reading an API you’ve never seen and using it correctly is the actual skill.
How to use a function you’ve never met. The documentation tells you three things: what to pass in, what comes back, and an example. That’s enough — you don’t read the source. It’s the black box from 3.11, scaled to whole libraries: trust the menu, ignore the kitchen.
4

Try It — Read the Docs, Then Call It

Here’s a documentation entry for a procedure you haven’t used. Read it, then predict what the call returns — exactly what the exam asks you to do.

math.floor(x)
  Returns the largest whole number that is
  less than or equal to x (rounds DOWN).
  Example: math.floor(2.9) → 2
math.floor( )
returns: —
Read the doc, predict the answer, then check.
5

Vocabulary

library Programs
A collection of prewritten procedures you can import and use in your own program.
API Programs
Application Programming Interface — the specification of how to use a library: what each procedure is called, takes, and returns.
documentation Programs
The written guide to a library or function — how to call it correctly, without reading its internal code.
6

Check

Five questions, including “read this doc and predict the result.” Then pick your answer.

0 of 5 answered
7

Impact Check

The whole modern world runs on imported code

Apps talk to maps, payments, weather, and login systems through APIs — menus of procedures other companies expose. A weather app doesn’t measure the sky; it calls a weather API. “Sign in with Google” is an API call. The reason a small team can build something powerful is that they import 95% of it.

This is abstraction and trust at global scale — and its risks scale too. When a widely-used library has a security hole, everything built on it is exposed at once (you’ll see this in Unit 7). “Reading the docs” and knowing what you’re importing isn’t busywork; it’s how professionals stay safe standing on other people’s code.

8

Connections

Came from 3.11 — a library is the black box idea scaled up to thousands of ready-made procedures.
Next in 3.17 — you’ll use one library in depth: random, for dice, coins, and simulations.
Returns in Units 6 & 8 — “API” comes back for web services and for how programs talk to AI models.
Returns on the exam — “use this documented procedure correctly” is an explicit CED skill and a common question type.
← 3.15 Lab: Refactor Day