Python Modules
For CSEC
[Link] Kiran
1 Introduction to Modules
A module in Python is a file that contains Python definitions, functions, variables, and
classes. Modules help organize code, improve readability, and promote reusability.
Why use Modules?
Avoids rewriting code
Helps in maintaining and organizing large programs
n
Makes code reusable
ira
Encourages modular programming
2 Creating and Using a Module
iK
Example: Creating a module [Link]
# File : mymath . py
h
def add (a , b ) :
return a + b
nt
def mul (a , b ) :
ra
return a * b
K
Importing Module
P.
import mymath
print ( mymath . add (10 , 20) )
print ( mymath . mul (3 , 5) )
Importing Specific Items
from mymath import add
print ( add (5 , 6) )
3 Module Built-in Functions
Python provides several built-in functions useful when working with modules.
1
Function Description
dir() Lists all names (functions, variables, classes) in a module
help() Displays help/documentation for module contents
name Contains name of module. Used to differentiate script
vs module execution
file Returns file path of the module
Example: Using dir() and help()
import math
print ( dir ( math ) ) # list contents
help ( math . sqrt ) # documentation
n
4 The name Attribute
ira
When a Python file is run directly, the value of name becomes “ main ”. Useful for
testing modules.
iK
Example
# File : mymodule . py
h
def greet () :
print ( " Hello ␣ from ␣ module " )
nt
if __name__ == " __main__ " :
print ( " Module ␣ executed ␣ directly " )
ra
else :
print ( " Module ␣ imported " )
K
5 Features of a Module
P.
Encapsulation: Code grouped logically in one file
Reusability: Functions reused across programs
Namespace Separation: Avoids variable/function name conflicts
Large Libraries Support: Python provides many built-in modules
Easy Maintenance: Modules make updating code easier
6 Types of Modules in Python
1. Built-in Modules
Examples:
2
math
random
os
sys
datetime
Example
import random
print ( random . randint (1 , 10) )
n
2. User-defined Modules
ra
Created by the programmer.
3. Third-party Modules
Installed using pip (e.g., numpy, pandas, flask).
Ki
7 Commonly Used Built-in Modules
i
th
7.1 1. math Module
import math
an
print ( math . sqrt (25) )
print ( math . pow (2 , 3) )
Kr
print ( math . pi )
P.
7.2 2. random Module
import random
print ( random . random () )
print ( random . choice ([1 , 2 , 3 , 4]) )
7.3 3. os Module
import os
print ( os . getcwd () )
print ( os . listdir () )
3
7.4 4. datetime Module
from datetime import datetime
print ( datetime . now () )
8 Flowchart: Importing a Module
Start
import module
n
ira
iK
Module Found?
Yes No
h
Load Module Error: Module Not Found
nt
ra
Execute Program
K
9 Best Practices for Using Modules
P.
Use import module instead of from module import *
Group all imports at the top of the file
Give meaningful module names
Create reusable modules for repetitive tasks
Use name guard for testing
10 Practice Questions
Short Answer Questions
1. What is a module in Python?
2. List any three features of modules.
4
3. What is the purpose of dir() function?
4. What is the difference between built-in and user-defined modules?
Long Answer Questions
1. Explain the types of modules with examples.
2. Describe the features of Python modules.
3. Write short notes on math, random, and datetime modules.
Coding Exercises
1. Create a module “[Link]” with functions and test it.
2. List all names inside math module using dir().
n
3. Use random module to simulate a dice roll.
ira
h iK
nt
ra
K
P.