Python Modules
Introduction
Python provides many built-in features that help developers perform different tasks without writing everything from scratch. These features are organised into reusable files called modules.
A Python module is a file containing Python code, such as functions, variables, and classes, that can be imported and used in another Python program.
For example, Python provides modules such as:
mathkeywordrandomdatetime
In this article, we will learn how to import and use these modules with practical examples.
1. What Is a Module in Python?
A module is a Python file that contains reusable code. It helps organise programs and allows developers to reuse existing functionality.
For example, instead of writing a function to calculate a square root, we can use the built-in math module.
Example
import math
print(math.sqrt(196))
Output:
14.0
Here:
importis a Python keyword used to load a module.mathis the module name.sqrt()is a function provided by the module.196is the input value.
2. Math Module in Python
The math module provides mathematical functions and constants.
It can be used for operations such as square roots, factorials, powers, and trigonometric calculations.
Example 1: Square Root
import math
result = math.sqrt(196)
print(result)
Output:
14.0
Example 2: Factorial
import math
print(math.factorial(5))
Output:
120
Example 3: Power
import math
print(math.pow(2, 3))
Output:
8.0
Example 4: Using Pi
import math
print(math.pi)
Output:
3.141592653589793
3. Keyword Module in Python
The keyword module provides information about Python’s reserved keywords.
Keywords are special words that have predefined meanings in Python. They cannot be used as ordinary identifiers.
Examples include:
ifelseforwhileclassdefreturnimport
Example: Display All Python Keywords
import keyword
print(keyword.kwlist)
Output:
The program displays a list of Python’s reserved keywords. The exact list may vary between Python versions.
Example: Check Whether a Word Is a Keyword
import keyword
print(keyword.iskeyword("if"))
print(keyword.iskeyword("hello"))
Output:
True
False
Here, if is a Python keyword, whereas hello is not.
4. Random Module in Python
The random module is used to generate random values.
It is useful for games, simulations, random selections, and testing programs.
Example 1: Generate a Random Integer
import random
print(random.randint(1, 100))
Possible Output:
57
The output can be any integer between 1 and 100, including both endpoints.
Example 2: Generate a Random Number
import random
print(random.random())
Possible Output:
0.735812
The random() function returns a floating-point number greater than or equal to 0.0 and less than 1.0.
Example 3: Select a Random Item
import random
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(random.choice(fruits))
Possible Output:
Mango
The selected fruit may be different each time the program runs.
5. Datetime Module in Python
The datetime module is used to work with dates and times.
It provides functionality to retrieve the current date, current time, and perform date and time calculations.
Example 1: Display Current Date and Time
import datetime
print(datetime.datetime.now())
Possible Output:
2026-11-30 10:30:45.123456
The actual output depends on the system’s current date and time.
Example 2: Display Current Date
import datetime
today = datetime.date.today()
print(today)
Possible Output:
2026-11-30
Example 3: Display Current Year
import datetime
today = datetime.date.today()
print(today.year)
Possible Output:
2026
6. How to Import Modules in Python
Python provides different ways to import modules.
A. Import the Complete Module
import math
print(math.sqrt(25))
Output:
5.0
Use the module name followed by a dot to access its functions.
B. Import a Specific Function
from math import sqrt
print(sqrt(25))
Output:
5.0
Here, only the sqrt() function is imported.
C. Import a Module Using an Alias
import math as m
print(m.sqrt(25))
Output:
5.0
The alias m is used instead of writing math each time.
7. How to View Available Python Modules
Python provides a built-in help system that can display information about available modules.
Example:
help('modules')
This command displays modules available in the current Python environment. The output can be long and may include installed third-party modules.
8. Advantages of Python Modules
- Code Reusability: Write code once and use it in multiple programs.
- Better Organisation: Divide large programs into smaller files.
- Easy Maintenance: Update code in one module instead of changing it everywhere.
- Saves Development Time: Use existing functions instead of implementing everything from scratch.
- Improved Readability: Keep related functionality organised.
9. Summary of Modules Covered
| Module | Purpose | Example |
|---|---|---|
math | Mathematical operations | math.sqrt(196) |
keyword | Information about Python keywords | keyword.kwlist |
random | Generate random values | random.randint(1, 100) |
datetime | Work with dates and times | datetime.datetime.now() |
10. Practice Questions (MCQs)
Q1. What is a Python module?
A. A loop statement
B. A file containing reusable Python code
C. A variable
D. An operator
Correct Answer: B. A file containing reusable Python code
Q2. Which module is used for mathematical operations in Python?
A. random
B. datetime
C. math
D. keyword
Correct Answer: C. math
Q3. Which function is used to calculate the square root using the math module?
A. math.power()
B. math.sqrt()
C. math.root()
D. math.square()
Correct Answer: B. math.sqrt()
Q4. Which module is used to generate random numbers?
A. math
B. keyword
C. datetime
D. random
Correct Answer: D. random
Q5. What does random.randint(1, 100) return?
A. A random integer from 1 to 100, inclusive
B. A random float between 0 and 1
C. Always 100
D. Always 1
Correct Answer: A. A random integer from 1 to 100, inclusive
Q6. Which module provides the kwlist attribute?
A. math
B. keyword
C. random
D. datetime
Correct Answer: B. keyword
Q7. Which function returns the current date and time?
A. datetime.datetime.now()
B. math.sqrt()
C. random.randint()
D. keyword.kwlist()
Correct Answer: A. datetime.datetime.now()
Q8. Which keyword is used to load a module in Python?
A. include
B. load
C. import
D. module
Correct Answer: C. import