PythonStandard Library Overview

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?

AdvantageBenefit
Comes built-inNo need to install anything
Well-tested and documentedReliable and stable
PortableWorks across platforms (Windows, macOS, Linux)
Saves timeAvoid 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)

ModulePurpose
jsonWork with JSON data
reRegular expressions
collectionsAdvanced data structures (deque, defaultdict)
pathlibModern file system paths
csvRead/write CSV files
urllibURL handling
itertoolsAdvanced iteration tools
argparseCommand-line argument parsing
loggingFlexible 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.