Standard Library

The Python Standard Library is a collection of built-in modules that come with every Python installation. These modules provide ready-made functions and tools for performing common tasks such as working with dates, generating random numbers, handling files, interacting with the operating system, and performing mathematical operations. Instead of writing complex logic from scratch, developers can rely on the standard library to build applications faster and more reliably.

Python’s standard library is one of its biggest advantages because it provides extensive functionality without requiring extra downloads or third-party packages.

1. What Is the Standard Library

The standard library is a set of modules included with Python by default. Each module contains functions, classes, and constants that help automate tasks and simplify development. These modules are tested, optimized, and maintained by Python itself, ensuring high reliability.

The standard library covers areas such as:

  • Mathematics
  • File and directory handling
  • Dates and time
  • Random number generation
  • Regular expressions
  • Serialization and data formats
  • Internet protocols
  • System utilities

To use any standard library module, it must be imported first.

Example

import math
print(math.sqrt(25))

Output:

5.0

2. math Module

The math module provides mathematical functions beyond the basic arithmetic operators in Python. It includes square roots, logarithms, trigonometry functions, constants like pi, and more. These functions are implemented in C and are therefore highly efficient.

Example: Using math functions

import math

print(math.sqrt(16))
print(math.pi)
print(math.sin(math.radians(30)))

Output:

4.0
3.141592653589793
0.5

This module is frequently used in scientific computing, games, graphics, and simulations.

3. random Module

The random module is used to generate random numbers, shuffle items, or pick a random element from a list. It is commonly used in games, simulations, password generators, and testing environments.

Example: Random numbers

import random

print(random.randint(1, 10))
print(random.choice(["apple", "banana", "orange"]))

Output:

7
banana

Each execution may give different results because the values are randomly generated.

4. datetime Module

The datetime module gives tools to work with dates, times, and timestamps. It supports formatting, arithmetic operations like adding days, and extracting components such as year, month, and day.

Example: Using datetime

from datetime import datetime

now = datetime.now()
print(now)
print(now.year, now.month, now.day)

Output:

2025-01-14 10:30:55.123456
2025 1 14

This module is widely used in scheduling systems, logging, automation, and analytics.

5. os Module

The os module allows Python to interact with the operating system. It provides functions to manage files, directories, and environment variables. This makes it essential for tasks like file automation, backup scripts, and application deployment.

Example: Directory Operations

import os

print(os.getcwd())
os.mkdir("new_folder")

Output:

/home/user/projects

A new folder is created inside the current working directory.

6. sys Module

The sys module provides access to Python interpreter settings, runtime environment, and command-line arguments. It is commonly used in automation, debugging, and command-line applications.

Example: sys module usage

import sys

print(sys.version)
print(sys.argv)

Output:

3.11.4 (main, Jun  7 2024, 10:10:20)
['script.py']

7. statistics Module

The statistics module provides functions for calculating mathematical statistics such as mean, median, mode, and standard deviation. This makes it useful for data analysis and scientific computing.

Example: Calculating Statistics

import statistics

data = [10, 20, 30, 40, 50]
print(statistics.mean(data))
print(statistics.median(data))

Output:

30
30

8. time Module

The time module allows Python to work with time-based operations, including delays, timestamps, and performance measurement.

Example: Using time.sleep()

import time

print("Start")
time.sleep(2)
print("End")

Output:

Start
End

A 2-second delay occurs between the two prints.

9. json Module

The json module is used to work with JSON data. It can convert Python objects into JSON strings and vice versa. JSON is commonly used in web applications, APIs, configuration files, and data exchange.

Example: JSON Conversion

import json

data = {"name": "Alice", "age": 25}
json_string = json.dumps(data)
print(json_string)

Output:

{"name": "Alice", "age": 25}

10. re Module

The re module enables pattern matching using regular expressions. It is useful for validating inputs, searching text, splitting strings, and extracting patterns.

Example: Searching Text

import re

text = "My number is 9876543210"
match = re.search(r"\d{10}", text)
print(match.group())

Output:

9876543210

11. shutil Module

The shutil module provides high-level operations for copying, moving, and removing files and directories. It is commonly used in automation scripts and backup systems.

Example: Copying Files

import shutil

shutil.copy("sample.txt", "backup.txt")

This creates a copy of sample.txt under the name backup.txt.

12. collections Module

The collections module contains powerful data structures like Counter, defaultdict, deque, and OrderedDict. These structures are more efficient and flexible than Python’s built-in lists and dictionaries for certain tasks.

Example: Counter

from collections import Counter

count = Counter("banana")
print(count)

Output:

Counter({'a': 3, 'n': 2, 'b': 1})