Built-in Modules in Python
Built-in modules are modules that come pre-installed with Python. You do not need to install them separately. They provide ready-made functions and classes to perform common tasks such as mathematics, date handling, file operations, and system interaction.
You can use them in a program using the import statement.
import module_name
Example:
import math
print(math.sqrt(25))
Output:
5.0
Common Built-in Modules in Python
1. math Module
Provides mathematical functions.
Example:
import math
print(math.sqrt(16))
print(math.factorial(5))
print(math.pi)
Functions:
-
sqrt()– Square root -
factorial()– Factorial value -
pi– Value of π -
pow()– Power calculation
2. random Module
Used to generate random numbers.
Example:
import random
print(random.randint(1,10))
print(random.choice([10,20,30,40]))
Functions:
-
randint()– Random integer -
choice()– Random selection -
random()– Random float
3. datetime Module
Used for date and time operations.
Example:
from datetime import datetime
now = datetime.now()
print("Current Date and Time:", now)
Functions:
-
datetime.now()– Current date and time -
date.today()– Current date
4. time Module
Used to work with time and delays.
Example:
import time
print("Start")
time.sleep(2)
print("End")
Function:
-
sleep()– Pause execution
5. sys Module
Used for system-specific operations.
Example:
import sys
print(sys.version)
Uses:
-
Python version
-
Command line arguments
6. os Module
Used for interacting with the operating system.
Example:
import os
print(os.getcwd())
Functions:
-
getcwd()– Current directory -
mkdir()– Create folder -
remove()– Delete file
Summary Table
| Module | Purpose |
|---|---|
| math | Mathematical operations |
| random | Random number generation |
| datetime | Date and time handling |
| time | Time related functions |
| sys | System operations |
| os | Operating system interaction |
Comments
Post a Comment