Python Modules 1
Definition of Module : A module in Python is simply a file containing Python code
(functions, variables, classes) that can be reused in other programs.
Example: A file named math_utils.py is a module.
Why use modules?
Code reuse
Better organization
Avoid repetition
Easier maintenance
Importing Modules Python provides different ways to import modules.
1. Using import <module> 2. Import All (*)
This imports the entire module. from math import *
Syntax: import module_name Not recommended because:
Example: import math Can cause name conflicts
print([Link](16)) Reduces code readability
print([Link])
Key Point:
You must use module name as prefix
Example: [Link](), [Link]
3. Using from ... import ...
This imports specific functions or variables from a module.
Syntax: from module_name import name1, name2
Example: from math import sqrt, pi
print(sqrt(25))
print(pi)
Key Point:
No need to use module prefix
Directly call sqrt() instead of [Link]()
Benefits of import *
Python imports all public names from the module directly into your current program’s
namespace. So you can directly write the module name without the dot (.)
from math import *
print(sqrt(16))
But there is a danger. Watch the below program.
Python Modules 2
from math import *
def sqrt(x): Output
return "This is my own sqrt function" This is my own sqrt function
print(sqrt(16))
The solution is this
import math
def sqrt(x):
return "This is my own sqrt function"
print([Link](16)) # still works
print(sqrt(16)) # your function
Math Module The math module provides mathematical functions and constants.
Important Constants
🔹 π (Pi) import math
print([Link]) # Output ≈ 3.14159
🔹 e (Euler’s Number) print(math.e) # Output ≈ 2.71828
🔹 Square Root [Link](25) # Output: 5.0
🔹 Ceiling (Round Up) [Link](4.2) # Output: 5
🔹 Floor (Round Down)[Link](4.8) # Output: 4
🔹 Power [Link](2, 3) # Output: 8.0
🔹 Absolute Value [Link](-7) # Output: 7.0
🔹 Trigonometric Functions (All use radians, not degrees)
[Link](0) # 0
[Link](0) # 1
[Link](0) # 0
Convert degrees to radians: [Link](90)
Random Module Used for generating random numbers.
Python Modules 3
Functions
🔹 random() import random
print([Link]()) #Output a float between 0.0 and 1.0
🔹 randint(a, b) [Link](1, 10)
# Output Returns integer between 1 and 10 (inclusive)
🔹 randrange(start, stop, step) [Link](1, 10, 2)
# Output Returns random number from sequence
Statistics Module Used for statistical calculations.
Functions
🔹 Mean (Average) import statistics
[Link]([10, 20, 30])
# Output: 20
🔹 Median [Link]([10, 20, 30])
# Output: 20
If even number of elements: [Link]([10, 20, 30, 40]) #Output: 25
🔹 Mode (Most Frequent) [Link]([1, 2, 2, 3]) # Output: 2
Combined Example Program
import math
Python Modules 4
import random
import statistics
# Math
print("Pi:", [Link])
print("Square Root:", [Link](16))
# Random
print("Random Float:", [Link]())
print("Random Integer:", [Link](1, 100))
# Statistics
data = [10, 20, 30, 40]
print("Mean:", [Link](data))
print("Median:", [Link](data))
[Link]
[Link]
[Link]