Introduction to Python Modules
Introduction to Python Modules
1. What is a Module?
Python provides several built-in modules. Below are three commonly used modules and their
functions:
A. math Module
The math module provides mathematical functions.
import math
print(math.sqrt(36)) # Output: 6.0
print(math.pi) # Output: 3.141592653589793
print(math.ceil(4.3)) # Output: 5
print(math.floor(4.7)) # Output: 4
B. random Module
The random module is used to generate random numbers.
import random
print(random.random()) # Output: Random float between 0 and 1
print(random.randint(1, 10)) # Output: Random integer between 1 and 10
print(random.randrange(1, 10, 2)) # Output: Random number from (1,3,5,7,9)
C. statistics Module
The statistics module provides functions to perform statistical operations.
import statistics
data = [10, 20, 20, 30, 40]
print(statistics.mean(data)) # Output: 24.0
print(statistics.median(data)) # Output: 20
print(statistics.mode(data)) # Output: 20