01 / 20

Software
Libraries

Discover how prewritten code collections let programmers build powerful applications without reinventing the wheel.

AP Computer Science Principles

What Is a Library?

A library (sometimes called a module or package) is a collection of prewritten code: procedures, functions, and data structures that other programmers have already built, tested, and documented. You import a library into your own program and call its procedures as if you had written them yourself.

Why Libraries Matter

Efficiency
Avoid writing thousands of lines of code that already exist, tested and optimized.
Collaboration
Teams worldwide share libraries, letting programmers build on each other's work.
Reliability
Well-maintained libraries are battle-tested by millions of users, reducing bugs.

How Libraries Connect to Your Code

Your Program IMPORT Application Programming Interface (API) Math Library sqrt, abs, random Graphics Library drawCircle, setColor Data Library readCSV, sort, filter

The Core Principle: Procedural Abstraction

When you call a library procedure, you rely on what it does (its interface), not how it does it (its implementation). This separation is called procedural abstraction.

Interface What you see: Procedure name Parameters Return value Implementation What's hidden: Algorithm logic Data structures Optimizations

Reading an API Specification

An API specification (or documentation) tells you everything you need to use a library procedure. It typically includes:

1
Procedure name: The identifier you call, e.g., drawRect
2
Parameters: The inputs it expects, e.g., x, y, width, height
3
Return value: What (if anything) the procedure gives back
4
Description: A human-readable explanation of what it does and any side effects

Library vs. Framework vs. Module

Library Framework Module
Who calls whom? You call the library Framework calls you You call the module
Scope Solves a specific task Provides full architecture A single file or unit
Control You stay in control Framework controls flow You stay in control
Example math library Django random module

How to Use a Library

1
Identify the need: Determine what functionality you need (e.g., random numbers, image processing).
2
Find a library: Search for an existing library that provides that functionality.
3
Read the documentation: Understand the API: procedure names, parameters, and return values.
4
Import the library: Use an import statement (e.g., IMPORT(libraryName)).
5
Call procedures: Use the library's procedures with the correct arguments in your program.

Example 1: Using the Math Library

// Import the math library
IMPORT(math)

// Use library procedures
side ← 16
result ← math.sqrt(side)
DISPLAY(result)
// Output: 4

Example 2: Random Number Generation

IMPORT(random)

// Generate a random integer between 1 and 6
diceRoll ← random.randint(1, 6)
DISPLAY("You rolled: ", diceRoll)

// Pick a random item from a list
colors ← ["red", "blue", "green"]
pick ← random.choice(colors)
DISPLAY("Color: ", pick)

Example 3: Building a Weather App

Weather App HTTP Library fetch API data JSON Library parse responses Chart Library display graphs Complete Weather Dashboard Built from 3 libraries + your application logic

Libraries in Practice

Web Development
React, Bootstrap, and jQuery provide prebuilt UI components and DOM manipulation.
Data Science
NumPy and Pandas offer powerful tools for numerical computation and data analysis.
Machine Learning
TensorFlow and scikit-learn let you build AI models without coding algorithms from scratch.

These are all examples of software libraries: pre-existing code that developers import, call, and combine to create new applications.

Reading Documentation

API Documentation: random library random.randint(a, b)
Returns a random integer N such that \( a \le N \le b \)
Parameters: a (int), b (int) : Return: int random.choice(sequence) Returns a random element from a non-empty sequence Parameters: sequence (list) : Return: element random.shuffle(list) Shuffles the list in place (modifies original)

Common Mistakes

Forgetting to Import
Calling a library procedure without importing the library first will cause a runtime error.
Wrong Arguments
Passing the wrong number or type of arguments (e.g., a string where an integer is expected) breaks the call.
Ignoring Return Values
Some procedures return results. If you don't store the return value, the result is lost.

Summary: Key Concepts

Concept Definition Why It Matters
Library Collection of prewritten procedures Saves time, reduces errors
API Public interface to a library Tells you what you can call
Abstraction Hiding implementation details Simplifies complex problems
Import Statement to load a library Makes procedures available
Documentation Describes procedures and parameters Guides correct usage

Question 1 of 5

Which of the following best describes a software library?

Question 2 of 5

A programmer uses a library procedure without understanding how it works internally. This is an example of:

Question 3 of 5

What is displayed when this code runs?

Question 4 of 5

A programmer writes random.randint(1, 10) but forgets to include the import statement. What happens?

Question 5 of 5

Which of the following is NOT a benefit of using libraries in software development?