Introduction to Python Modules
1. What is a Module?
A module in Python is a file containing Python definitions and statements. It helps in
organizing code into reusable components. Python has many built-in modules, and we can
also create our own.
2. Importing Modules in Python
Method Syntax Usage
Importing entire import module_name Allows access to all functions and
module constants using
module_name.function()
Importing specific from module_name Directly uses function_name()
functions/constants import
function_name
without needing module_name. prefix
Importing module with import module_name Shortens module name for convenience
as alias
alias
Example: Importing Modules
import math # Importing the math module
print(math.pi) # Output: 3.141592653589793
from math import sqrt, pi # Importing specific functions
print(sqrt(16)) # Output: 4.0
print(pi) # Output: 3.141592653589793
import statistics as stat # Importing module with an alias
print(stat.mean([10, 20, 30])) # Output: 20.0
3. Commonly Used Python Modules
Python provides several built-in modules. Below are three commonly used modules and their
functions:
A. math Module
The math module provides mathematical functions.
Function/Constant Description Example
pi Value of π (3.14159...) math.pi → 3.141592653589793
e Euler's number (2.718...) math.e → 2.718281828459045
sqrt(x) Square root of x math.sqrt(25) → 5.0
ceil(x) Smallest integer ≥ x math.ceil(4.2) → 5
floor(x) Largest integer ≤ x math.floor(4.8) → 4
pow(x, y) x raised to power y math.pow(2, 3) → 8.0
fabs(x) Absolute value of x math.fabs(-7.5) → 7.5
sin(x) Sine of x (radians) math.sin(math.pi/2) → 1.0
cos(x) Cosine of x (radians) math.cos(0) → 1.0
tan(x) Tangent of x (radians) math.tan(math.pi/4) → 1.0
Example: Using math Module
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.
Function Description Example
random() Returns a random float between random.random() → 0.5678
0 and 1
randint(a, b) Returns a random integer random.randint(1, 10) →
between a and b (inclusive) 7
randrange(start, Returns a random number from random.randrange(1, 10,
stop, step) 2) → 3
range (start, stop, step)
Example: Using random Module
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.
Function Description Example
mean(data) Returns the arithmetic mean statistics.mean([10, 20, 30]) →
20.0
median(data) Returns the middle value statistics.median([1, 3, 3, 6]) →
3.0
mode(data) Returns the most common statistics.mode([1, 2, 2, 3]) →2
value
Example: Using statistics Module
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