Python Standard Library Overview
The Python Standard Library is a collection of ready-to-use modules that come installed with Python. You can use these modules to perform common tasks — without installing any third-party packages.
The Standard Library includes tools for:
File I/O String handling Math and statistics Dates and times Data structures Regular expressions Networking Concurrency and much more.
Why Use the Standard Library?
Advantage | Benefit |
---|---|
Comes built-in | No need to install anything |
Well-tested and documented | Reliable and stable |
Portable | Works across platforms (Windows, macOS, Linux) |
Saves time | Avoid reinventing common solutions |
How to Access Standard Library Modules
stdlib_import.py
import module_name
Example 1: math
— Mathematical Functions
stdlib_math.py
import math
print("Pi:", math.pi)
print("Square root of 16:", math.sqrt(16))
output.txt
Pi: 3.141592653589793
Square root of 16: 4.0
Example 2: datetime
— Dates and Times
stdlib_datetime.py
import datetime
today = datetime.date.today()
print("Today's date:", today)
output.txt
Today's date: 2025-06-16 # Example date
Example 3: random
— Random Numbers
stdlib_random.py
import random
print("Random number between 1 and 10:", random.randint(1, 10))
output.txt
Random number between 1 and 10: 7 # Example output
Example 4: os
— Operating System Interface
stdlib_os.py
import os
print("Current working directory:", os.getcwd())
output.txt
Current working directory: /Users/username/projects # Example output
Example 5: sys
— System-specific Parameters
stdlib_sys.py
import sys
print("Python version:", sys.version)
output.txt
Python version: 3.12.2 (example)
Example 6: statistics
— Basic Stats
stdlib_statistics.py
import statistics
data = [10, 20, 30, 40, 50]
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
output.txt
Mean: 30
Median: 30
Other Useful Modules (Partial List)
Module | Purpose |
---|---|
json | Work with JSON data |
re | Regular expressions |
collections | Advanced data structures (deque, defaultdict) |
pathlib | Modern file system paths |
csv | Read/write CSV files |
urllib | URL handling |
itertools | Advanced iteration tools |
argparse | Command-line argument parsing |
logging | Flexible logging system |
Summary
- The Standard Library is a powerful collection of pre-installed modules.
- No installation required — just
import
and use. - Helps you write better, faster, more portable Python code.
In the next section, we will explore Writing Your Own Modules — how to structure your code into reusable modules.