Article
Libraries and APIs
You did not write the code that plays a video in your browser, or the code that tells you tomorrow's weather. Other programmers wrote it and shared it. When you use their work in your own program, you are using a library.
Software libraries
A software library is a collection of ready-made procedures that you can use in your own programs. You call a library procedure in the same way as your own procedures: its name, brackets and arguments. You do not have to write it, and you do not have to know how it works inside. That is procedural abstraction again, but this time somebody else built the procedure.
Python has many libraries. Two of them are math and random. In Python you write import to say which library you want to use. Run this:
The output is 7.0, 7 and 8. We can describe what each procedure does without looking at its code:
math.sqrt(x)gives the square root ofx.math.floor(x)roundsxdown to a whole number.math.ceil(x)roundsxup to a whole number.
Now the random library. Run this several times:
You get a different number each time, from 1 to 6. The procedure random.randint(a, b) gives a random whole number from a to b, including both ends. The AP exam has a procedure that does the same job, and you have already met it:
DISPLAY(RANDOM(1, 6))
Check your understanding
What is a software library?
APIs and documentation
How do you know that randint needs two arguments, or that it includes both ends? You read its description. The API (application programming interface) of a library is the specification of its procedures: their names, what arguments each needs, what each does and what each gives back. The written description is the documentation.
Three layers when you use a library
Your program
Calls sqrt(25) and uses the number that comes back.
The API and its documentation
sqrt(x) gives back the square root of x.
The library's code
How the square root is worked out. You do not need to read it.
Good documentation answers four questions about every procedure: What is it called? What does it need (its parameters)? What does it do? What does it give back (its return value)? Here is documentation for a made-up library called ShopLib:
| Procedure | What it does |
|---|---|
price(item) | Returns the price in dollars of item. item is a piece of text. For example, price("book") returns 50. |
discount(amount, percent) | Returns amount after taking off percent % of it. For example, discount(200, 10) returns 180. |
Using only this table, you can write programs with ShopLib. You never see the code inside.
Check your understanding
The documentation of a library says: discount(amount, percent) returns amount after taking off percent % of it. What does discount(80, 25) return?
Why use libraries?
- They save time. You do not write and test everything yourself.
- They are reliable. Many people have already used and checked them.
- They let you do hard things, such as drawing graphs or playing sound, with one call.
- They need documentation. Without it, you would have to guess what a procedure does and which arguments it needs.
Worked example
Use the ShopLib documentation above, and suppose price("pen") returns 20. What does this program display?
cost ← price("book")
DISPLAY(discount(cost, 10))
DISPLAY(discount(price("pen"), 50))price("book")returns 50, as the documentation says. Socostis 50.discount(cost, 10)takes 10% off 50, which leaves 45. The program displays45.price("pen")returns 20. Thendiscount(20, 50)takes half off, which leaves 10. The program displays10.
The output is 45 and 10. You found it by reading the documentation, not the code.
Common mistakes
- Trying to read the library's code to use it. The documentation tells you everything you need.
- Guessing the order of the arguments. In
discount(amount, percent), the amount comes first. Check the documentation. - Mixing up API and library. The library is the code. The API is the description of how to use it.
- Forgetting
importin Python. Without it, Python does not know the namemathorrandom. - Expecting random procedures to give the same answer each time. They do not.
On the exam
Questions often show a short piece of documentation and ask what a call returns, or which call gives a certain result. Read each procedure's description carefully, and trust it: do not worry how it works. Other questions ask why a programmer would use a library, or why documentation is needed.
Key words
Software library: a collection of procedures that programmers can use in their own programs.
API (application programming interface): the specification of how the procedures in a library behave and how to use them.
Documentation: the written description of an API, which says what each procedure needs, does and returns.
Import: the Python word for bringing a library into a program.