Module is a file containing Python code such as variables, functions, and classes that can be used in another Python program. Modules help organize code and promote code reusability.
Advantages of Modules:
- Code reusability.
- Better organization of programs.
- Easier maintenance.
- Reduces code duplication.
- Improves readability.
Importing Modules
To use a module, it must first be imported into the program.
Syntax
import module_name
Example
import math
print(math.sqrt(25))
Output
5.0
Import Specific Functions
Syntax
from module_name import function_name
Example
from math import sqrt
print(sqrt(36))
Output
6.0
Import Multiple Functions
from math import sqrt, factorial
print(sqrt(49))
print(factorial(5))
Import Using Alias
An alias is an alternative name for a module.
Syntax
import module_name as alias_name
Example
import math as m
print(m.pi)
Output
3.141592653589793
Math Module
The math module provides mathematical functions and constants.
Importing Math Module
import math
Common Functions of Math Module
1. sqrt()
Returns the square root of a number.
import math
print(math.sqrt(64))
Output
8.0
2. pow()
Returns the power of a number.
import math
print(math.pow(2, 3))
Output
8.0
3. factorial()
Returns factorial of a number.
import math
print(math.factorial(5))
Output
120
4. ceil()
Rounds a number upward.
import math
print(math.ceil(5.2))
Output
6
5. floor()
Rounds a number downward.
import math
print(math.floor(5.8))
Output
5
Important Constants
pi
import math
print(math.pi)
Output
3.141592653589793
e
import math
print(math.e)
Output
2.718281828459045
Random Module
The random module is used to generate random numbers and random selections.
Importing Random Module
import random
1. random()
Generates a random decimal number between 0 and 1.
import random
print(random.random())
Possible Output
0.5472
2. randint()
Generates a random integer within a specified range.
import random
print(random.randint(1, 10))
Possible Output
7
3. choice()
Selects a random element from a sequence.
import random
colors = ["Red", "Blue", "Green"]
print(random.choice(colors))
Possible Output
Blue
4. shuffle()
Randomly rearranges list elements.
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
Possible Output
[4, 1, 5, 2, 3]
Creating Your Own Modules
A user can create custom modules by saving Python code in a separate .py file.
Step 1: Create a Module
Create a file named calculator.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
Step 2: Use the Module
Create another Python file.
import calculator
print(calculator.add(10, 20))
print(calculator.multiply(5, 4))
Output
30
20
Advantages of User Defined Modules
- Code reusability.
- Better organization.
- Easier testing.
- Simplifies large programs.
Concept of Packages
A Package is a collection of related modules organized in directories.
Packages help manage large projects efficiently.
Structure of a Package
mypackage/
│
├── module1.py
├── module2.py
├── module3.py
└── __init__.py
The __init__.py file indicates that the directory is treated as a package.
Importing Modules from a Package
Example Structure
mypackage/
│
├── calculator.py
└── __init__.py
calculator.py
def add(a, b):
return a + b
Main Program
from mypackage import calculator
print(calculator.add(10, 5))
Output
15
Difference Between Module and Package
| Module | Package |
|---|---|
| Single Python file | Collection of modules |
| Contains functions, variables, classes | Contains multiple modules |
| Easier for small projects | Suitable for large projects |
| Extension is .py | Directory containing modules |
| Example: math.py | Example: mypackage |
Summary Table
| Concept | Description |
|---|---|
| Module | Python file containing reusable code |
| import | Used to import modules |
| Math Module | Provides mathematical functions |
| Random Module | Generates random values |
| User Defined Module | Custom module created by programmer |
| Package | Collection of related modules |
| Alias | Alternative name for a module |