The Python Standard Library is a large collection of built-in modules and packages that come pre-installed with Python. You donβt need to install them separately. They provide ready-to-use functionality for many common programming tasks.
π This is one of the reasons Python is powerful: βBatteries included.β
π§ Major Categories in the Python Standard Library
1οΈβ£ Math and Numeric Modules
Used for mathematical calculations.
Examples:
mathβ advanced math functionsrandomβ generate random numbersstatisticsβ statistical calculations
Example:
import math
print(math.sqrt(25))
print(math.factorial(5))
Output:
5.0
120
2οΈβ£ Date and Time Modules
Used to work with dates, time, and timestamps.
Modules:
datetimetimecalendar
Example:
import datetime
now = datetime.datetime.now()
print(now)
3οΈβ£ File and Directory Access
Used to read files, write files, and manage directories.
Modules:
osshutilpathlib
Example:
import os
print(os.getcwd()) # Current working directory
4οΈβ£ Data Serialization
Used to convert data structures into a format that can be stored or transmitted.
Modules:
jsonpicklecsv
Example:
import json
data = {"name": "Venkat", "age": 30}
print(json.dumps(data))
Output:
{"name": "Venkat", "age": 30}
5οΈβ£ Internet and Networking
Used for web requests and internet communication.
Modules:
urllibhttpsocket
Example:
import socket
print(socket.gethostname())
6οΈβ£ Operating System Interaction
Used to interact with the system environment.
Modules:
sysosplatform
Example:
import sys
print(sys.version)
7οΈβ£ Compression and Archiving
Used for working with ZIP and compressed files.
Modules:
zipfilegziptarfile
Example:
import zipfile
Used to create or extract ZIP archives.
8οΈβ£ Debugging and Testing
Modules used for testing and debugging programs.
Modules:
unittestdoctestlogging
Example:
import logging
logging.warning("This is a warning message")
π§ Why the Standard Library Is Important
Benefits:
β No installation required
β Well tested and reliable
β Saves development time
β Covers many programming needs
π¦ Some Very Common Python Standard Modules
| Module | Purpose |
|---|---|
math | Mathematical operations |
random | Random numbers |
datetime | Date and time |
os | Operating system interaction |
sys | System functions |
json | JSON data handling |
csv | CSV file processing |
re | Regular expressions |
π§Ύ Example Combining Multiple Standard Modules
import random
import datetimenumber = random.randint(1, 100)
now = datetime.datetime.now()print("Random Number:", number)
print("Current Time:", now)
β
In simple terms:
The Python Standard Library is a toolbox full of ready-made modules that help you perform tasks like math calculations, file handling, networking, and data processing.