Topic 3.14: Libraries | AP CSP Big Idea 3 | APCSExamPrep.com
Libraries
After this lesson, you will be able to:
- Explain that a library is a reusable collection of procedures and a form of procedural abstraction
- Import a library and call its procedures with the correct arguments
- Read an API or documentation to predict what a procedure call returns
- Explain why reusing well-tested library code saves time and reduces errors
- Recognize that a library hides its internal implementation behind its API
- Separate a library call from your own student-developed procedure on the Create Task
You use libraries every day without thinking about it. When your phone finds the square root for a calculator, sorts your photos by date, or draws a map, it is calling procedures someone else already wrote and tested. You did not build those from scratch, and neither should your programs. A library lets you reach for a ready-made, reliable procedure by name. This lesson is about how to import one, read its documentation, and predict exactly what it gives back.
What a Library Is
A library is a collection of procedures that other programmers have already written, tested, and packaged so you can reuse them. Instead of writing your own square-root routine from scratch, you reach into a library and call the procedure that is already there. A library is a form of procedural abstraction: it lets you use powerful operations by name without knowing, or caring, how they are built internally.
In Python you bring a library into your program with import, then call its procedures with the library name, a dot, and the procedure name. The example below imports the math library and calls its sqrt procedure.
import math x = 16 print(math.sqrt(x))
x <- 16 result <- squareRoot(x) DISPLAY(result)
Notice what you did not have to do: you never wrote the algorithm that computes a square root. You only had to know the procedure's name and what it expects. That is the whole point of a library. On the right, AP pseudocode has no import keyword, but the idea is identical: a library provides a procedure like squareRoot that you simply call.
The CED (AAP-3.D) says a library is a collection of procedures that may be used in creating new programs, and that using existing, correct library code saves time and reduces errors. You are not expected to memorize any real library. You are expected to read a short description of a procedure and predict what a call to it produces.
floor(x) returns the largest integer that is not greater than x. What does this print?import math print(math.floor(6.99))
An API Is the Instructions
An API, short for application program interface, is the description of how to use a library: the names of its procedures, what arguments each one takes, and what each one returns. The API is the contract between you and the library. You read the API, sometimes called the documentation, to learn how to call a procedure correctly, exactly the way you read the instructions on a microwave without knowing the electronics inside.
Because the API hides the internal details, two things follow. First, you must supply the right arguments in the right order, because that is what the documentation promises. Second, you can predict a procedure's result from its documented behavior alone. The math library documents that floor rounds down and ceil rounds up:
import math # floor: largest integer not greater than x print(math.floor(7.9)) # ceil: smallest integer not less than x print(math.ceil(7.1))
DISPLAY(floor(7.9)) DISPLAY(ceil(7.1))
Reading that documentation tells you floor(7.9) is 7 and ceil(7.1) is 8, even though you never see the code that computes them. That is what an exam question means when it hands you a described procedure and asks for the result.
A library procedure only behaves as documented if you call it as documented. If the API says pow(base, exponent) needs two arguments and you pass one, the program raises an error, not a guess. A favorite trap gives you a procedure with a specific argument order and a call that swaps or drops an argument. Always match the call to the API.
9.0?print(math.sqrt(81))
Use It, or Rebuild It Yourself
Why does a library matter? Because reusing existing, well-tested code saves development time and reduces errors. Someone already debugged the square-root procedure across millions of inputs. If you rewrote it yourself you would spend hours and probably introduce a bug the library long ago fixed. Choosing to call a library procedure instead of reimplementing it is one of the most practical decisions a programmer makes.
You can also build your own small library: a set of procedures you write once and then call by name throughout a program. That is exactly what procedural abstraction gives you, and it is why a library and a procedure are the same idea at different scales. Here is a two-procedure mini library and a call to it:
# a tiny library of procedures def area(w, h): return w * h print(area(4, 3))
PROCEDURE area(w, h) { RETURN(w * h) } DISPLAY(area(4, 3))
Once area exists, the rest of the program just calls area(4, 3) and trusts it to return 12. The caller does not re-derive width times height every time. That reuse is the benefit, whether the procedure came from Python's math library or from a library you wrote yourself.
ceil(x) returns the smallest integer not less than x. Type the exact value this prints.import math print(math.ceil(4.1))
Predicting a Result From Documentation
Most library questions on the exam are not about writing code at all. They give you a procedure's documented behavior and one or two calls, then ask what is displayed. Use a short, mechanical routine:
- Read the documentation sentence and restate the rule in your own words.
- Identify each argument in the call and match it to the documented parameter.
- Apply the rule to those exact argument values.
- Do not assume any behavior the documentation did not promise.
For instance, a library documents clamp(v, lo, hi) as: returns v when it lies between lo and hi, returns lo when v is smaller, and returns hi when v is larger. Then clamp(12, 0, 10) must return 10, because 12 is larger than the high bound. You solved that with the documentation alone, never seeing inside clamp. That is exactly the skill AAP-3.D is testing.
When a question asks what a library gives you, the answer is a way to use a procedure without knowing its implementation. You interact through the API. Any answer choice that claims you must understand the internal code to call a library procedure is describing the opposite of abstraction, and is wrong.
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Library | A collection of procedures that can be reused in new programs | Python's math
|
| API | The description of how to use a library's procedures | documentation for sqrt
|
| Documentation | Written explanation of what a procedure does and how to call it | "floor rounds down" |
| import | Python statement that brings a library into a program | import math |
| Procedural abstraction | Using a procedure by name without knowing its internal code | calling math.sqrt(x)
|
| Argument | A value passed to a procedure, matched to the API's parameter | sqrt(16) |
Libraries are welcome in your Create Task program, and calling well-tested code is smart engineering. But there is a rule you must not miss: the point for a student-developed procedure requires a procedure that you wrote, with a parameter that affects the procedure's behavior. Calling a library procedure alone does not earn that point.
A point-earning example
Suppose your program uses the math library to compute a distance, then feeds it into a procedure you wrote:
In your written response, separate what the library did from what you did:
- "I used a library procedure,
squareRoot, to compute the distance, which saved time and avoided errors." - "I developed my own procedure
distanceLabelwhose parameterdchanges the returned label, so it demonstrates abstraction." - "The parameter generalizes the procedure so it works for any distance the program produces, not one fixed value."
The trap to avoid
Do not point to a library call, like squareRoot or sqrt, as your student-developed procedure. The reader is looking for a procedure you defined with a parameter that affects its behavior. Use libraries freely, but make sure your abstraction is your own. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
clamp(v, lo, hi) as follows: it returns v when lo ≤ v ≤ hi, returns lo when v < lo, and returns hi when v > hi. What does clamp(12, 0, 10) return?math library documents ceil(x) as returning the smallest integer that is not less than x. What does this segment display?5.0 but it does not run. What is the bug?pow(base, exponent) takes exactly two arguments. Which statement best explains why the call below is incorrect?- I. A library is a collection of procedures that can be reused in new programs.
- II. Using a well-tested library procedure can save development time and reduce errors.
- III. To call a library procedure correctly you must understand how it is implemented internally.
math library and print the square root of 64. Target output: 8.0
math library and x, print two lines:- first:
xrounded down withmath.floor - second:
xrounded up withmath.ceil
math.pi to compute the area of a circle with radius r (area is pi times r squared), then print it rounded to 2 decimal places with round. Target output: 78.54
x up to the next whole number, but it calls the wrong library procedure and prints 3. Fix it so it uses the documented procedure that rounds up. Target output: 4
- first: the area of the rectangle
- second: the perimeter of the rectangle
math.sqrt to compute the distance between two points (the square root of dx squared plus dy squared), then print it. The points are (0, 0) and (3, 4). Target output: 5.0
math.pi and radius r, compute and print two labeled lines, each value rounded to 2 decimals with round:-
circumferencethen its value (2 times pi times r) -
areathen its value (pi times r squared)
r is given. Target output: circumference 43.98 then area 153.94
a and b, use the math library to print three labeled lines:-
hypotenusethen its value (square root of a squared plus b squared) -
perimeterthen a plus b plus the hypotenuse -
areathen one half times a times b
Frequently Asked Questions
🔗 Continue studying
The Superpack includes an editable Topic 3.14 slide deck on libraries and APIs, a documentation-reading question bank with described procedures, a print-ready worksheet on predicting library results, and a unit quiz. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]