Table of Contents
In Python, a library (often called a module or package) is a collection of ready-made code that you can reuse in your own programs. Instead of writing everything from scratch, you can "import" this existing code and use it directly.
Think of a library as a toolbox:
- Python itself is the workshop (the language and runtime).
- Your program is the project you're building.
- Libraries are toolboxes full of useful tools (functions, classes, constants).
When you use a library, you are saying: "Someone has already solved part of this problem. I will use their solution instead of reinventing it."
Why libraries exist
Programming problems repeat. For example:
- Working with dates and times
- Doing math beyond basic
+,-,*,/ - Generating random numbers
- Reading and writing different file formats
- Talking to websites or APIs
Writing these features correctly and safely is hard. Libraries:
- Save time: you don't write everything yourself.
- Reduce bugs: widely used libraries are tested by many people.
- Make code clearer: instead of a long custom solution, you call a well-named function from a library.
How libraries fit into your code
You use libraries by importing them. Conceptually, this is:
- Load the library into your program.
- Use the functions, classes, or variables it provides.
In code, it looks like:
import math
result = math.sqrt(25) # uses the sqrt function from the math library
print(result)Here:
mathis the library (a module).sqrtis a function inside that library.math.sqrt(25)means: "call thesqrtfunction from themathlibrary with argument25."
You will learn the import statement and its variations in a later section; here, just understand that importing connects your program to a library.
Types of Python libraries
At a high level, there are two main kinds of libraries you will use:
- Standard library
- Comes built-in with Python.
- You do not need to install anything extra.
- Always available once Python is installed.
- Examples (which you’ll explore later):
mathfor mathematical functionsrandomfor random numbersdatetimefor working with dates and times- External (third-party) libraries
- Created by other people or organizations.
- You do need to install them (usually with
pip). - Add specialized features:
- Web development
- Data analysis
- Automation
- Machine learning, and more
You will learn how to install these with pip and how to choose good ones in later sections.
What is inside a library?
A typical Python library can contain:
- Functions – reusable pieces of code that do something specific.
- Example:
math.sqrt(x)returns the square root of $x$. - Classes – blueprints for creating objects with data and behavior.
- Constants and variables – useful predefined values.
- Example:
math.pifor $\pi$. - Submodules – smaller modules grouped inside a larger package.
You access these pieces using dot notation:
import math
print(math.pi) # a constant
print(math.sqrt(9)) # a functionThe pattern is:
$$
\text{library\_name}.\text{thing\_inside}
$$
Libraries vs your own code
You can think of code in three layers:
- Python language
- The core rules: variables, loops, functions, etc.
- Libraries
- Code built on top of the language that gives extra abilities.
- Your program
- Uses the language + libraries to solve your specific problem.
Over time, you can even turn your own code into a small library that you reuse across multiple projects. That’s just code organized so others (or future you) can import and use it easily.
When should you use a library?
You usually reach for a library when:
- The task is common (dates, random numbers, math, file formats).
- The task is complex or low-level (network communication, encryption).
- Someone has already provided a well-tested solution.
- You want to focus on your problem, not the technical details.
For example, if you need random numbers, you could:
- Try to write your own random number generator (hard, error-prone), or
- Use the built-in
randomlibrary:
import random
number = random.randint(1, 10)
print(number)The second option is exactly why libraries are so valuable.
Advantages and trade-offs
Advantages:
- Faster development
- Less code to write and maintain
- More reliable features
- Access to advanced capabilities (data science, web, etc.)
Trade-offs:
- You need to learn the library’s interface (its functions and how to call them).
- For external libraries, you depend on:
- Them being installed
- Their updates and changes over time
For beginners, using the Python standard library is a gentle introduction, since it’s always available and well-documented.
How you will use libraries in this course
In this chapter and the next ones, you will:
- See how to import modules and access what’s inside them.
- Explore some important standard libraries:
math,random, anddatetime.- Learn how to install external libraries with
pip.
For now, the key idea to remember:
A library in Python is pre-written, reusable code that you import into your program so you can use its tools (functions, classes, constants) instead of writing everything from scratch.