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.
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:
To use any standard library module, it must be imported first.
import math
print(math.sqrt(25))
Output:
5.0
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.
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.
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.
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.
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.
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.
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.
import os
print(os.getcwd())
os.mkdir("new_folder")
Output:
/home/user/projects
A new folder is created inside the current working directory.
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.
import sys
print(sys.version)
print(sys.argv)
Output:
3.11.4 (main, Jun 7 2024, 10:10:20)
['script.py']
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.
import statistics
data = [10, 20, 30, 40, 50]
print(statistics.mean(data))
print(statistics.median(data))
Output:
30
30
The time module allows Python to work with time-based operations, including delays, timestamps, and performance measurement.
import time
print("Start")
time.sleep(2)
print("End")
Output:
Start
End
A 2-second delay occurs between the two prints.
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.
import json
data = {"name": "Alice", "age": 25}
json_string = json.dumps(data)
print(json_string)
Output:
{"name": "Alice", "age": 25}
The re module enables pattern matching using regular expressions. It is useful for validating inputs, searching text, splitting strings, and extracting patterns.
import re
text = "My number is 9876543210"
match = re.search(r"\d{10}", text)
print(match.group())
Output:
9876543210
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.
import shutil
shutil.copy("sample.txt", "backup.txt")
This creates a copy of sample.txt under the name backup.txt.
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.
from collections import Counter
count = Counter("banana")
print(count)
Output:
Counter({'a': 3, 'n': 2, 'b': 1})