Software
Libraries
Discover how prewritten code collections let programmers build powerful applications without reinventing the wheel.
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
How Libraries Connect to Your Code
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.
Reading an API Specification
An API specification (or documentation) tells you everything you need to use a library procedure. It typically includes:
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
Example 1: Using the Math Library
IMPORT(math)
// Use library procedures
side ← 16
result ← math.sqrt(side)
DISPLAY(result)
// Output: 4
Example 2: Random Number Generation
// 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
Libraries in Practice
These are all examples of software libraries: pre-existing code that developers import, call, and combine to create new applications.
Reading Documentation
Common Mistakes
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
Consider the following code:
IMPORT(mathLib)
x ← mathLib.sqrt(49)
y ← mathLib.abs(-12)
DISPLAY(x + y)
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?