⭐ Standard Modules in Python
Standard Modules are the modules that come pre-installed with Python.
These modules are part of Python’s Standard Library and provide ready-made functions to perform common tasks such as:
✔ Mathematics
✔ File handling
✔ Date and time processing
✔ System operations
✔ String processing
✔ Random number generation
✔ Network communication
You do not need to install them — you simply import and use them.
⭐ What is Python Standard Library?
The Python Standard Library is a collection of hundreds of built-in modules, written in Python and C, that provide functionality for almost every programming need.
It acts as a toolbox for programmers.
⭐ Why Standard Modules Are Important?
✔ Saves time
✔ Reduces need to write code from scratch
✔ Provides reliable, tested functionality
✔ Supports various domains (math, networking, OS, file I/O, web data, etc.)
⭐ Commonly Used Standard Modules (Important for Exams)
Below are the most important standard modules with explanations and examples.
1. math Module
Provides mathematical operations such as:
sqrt()→ square rootpow()→ powerfactorial()sin(),cos(),tan()pi,econstants
Example:
import math
print(math.sqrt(25))
print(math.factorial(5))
2. random Module
Used for generating random numbers.
Functions:
random()→ random number between 0 and 1randint(a, b)→ integer between a and bchoice()→ pick random element from list
Example:
import random
print(random.randint(1, 10))
3. datetime Module
Used for working with dates and time.
Example:
from datetime import datetime
print(datetime.now())
4. os Module
Interacts with the operating system.
Functions:
mkdir()→ create folderlistdir()→ list directory filesremove()→ delete filegetcwd()→ current working directory
Example:
import os
print(os.getcwd())
5. sys Module
Provides access to system-specific parameters and functions.
Example:
import sys
print(sys.version)
print(sys.path)
6. statistics Module
Mathematical statistics functions:
mean()median()mode()
Example:
import statistics
print(statistics.mean([10, 20, 30]))
7. time Module
For handling time-related tasks.
Example:
import time
print(time.time())
time.sleep(2) # pauses program for 2 seconds
8. json Module
Used for working with JSON data.
import json
data = '{"name":"Amit","age":25}'
print(json.loads(data))
9. re Module
Used for regular expressions.
Example:
import re
print(re.findall(r"\d+", "Age is 25"))
10. urllib Module
Used for fetching data from the web.
Example:
from urllib.request import urlopen
response = urlopen("https://example.com")
print(response.read())
⭐ Other Useful Standard Modules
| Module | Usage |
|---|---|
math | Mathematics |
calendar | Working with calendars |
hashlib | Hashing |
sqlite3 | Database SQLite |
email | Email handling |
socket | Networking |
shutil | File operations |
functools | Higher-order functions |
collections | Specialized data structures |
⭐ Advantages of Standard Modules
✔ Ready-made functionality
✔ Saves time and effort
✔ Increases productivity
✔ Provides robust, error-free tools
✔ Widely used in professional applications
⭐ Exam-Ready Short Answer
Standard modules are built-in modules included in Python’s Standard Library. They offer predefined functions for tasks like mathematical computation, file handling, system management, networking, date and time processing, and random number generation. Examples include math, random, datetime, os, sys, and json.
