EX.
NO: 9
71762305009 MODULES AND PACKAGES
19.03.2025
AIM:
To study and implement python programs on modules and packages
PROGRAM:
1. Create and Import a Custom Module
Create a Python module named math_operations.py that contains the
following functions:
add(a, b): Returns the sum of a and b.
subtract(a, b): Returns the difference between a and b.
multiply(a, b): Returns the product of a and b.
divide(a, b): Returns the quotient (handle division by zero).
Write another script (main.py) that imports the module and allows the user to:
Input two numbers.
Choose an operation.
Display the result using functions from math_operations.py.
CODE:
OUTPUT:
1. Importing and Using Built-in Modules
Write a Python script that:
o Asks the user to enter a number.
o Uses the math module to calculate:
Square root (sqrt)
Factorial (factorial)
Natural logarithm (log)
o Uses the random module to generate a random number between 1
and 100.
o Uses the datetime module to display the current date and time.
CODE:
import math as m
import random as r
import datetime
y = datetime.datetime.now()
x=int(input("enter a number:"))
print(f"Factorial: {m.factorial(x)}")
print(f"Square root:{m.sqrt(x)}")
print(f"Natural logirithm:{m.log(x)}")
print("")
print(f"The random number generated between 0 and 100 is
{r.randrange(1,100,4)}")
print("")
print(f"Current date and time:{y}")
OUTPUT:
2. Create a Package for Student Management System
Create a package named student_management containing two modules:
o student.py (Handles student details: name, ID, marks).
o grade.py (Contains a function calculate_grade(marks), which
returns grades:
90-100: A
80-89 : B
70-79 : C
60-69 : D
< 60 : F
Write a script (main.py) that:
o Imports the package.
o Accepts a student's name, ID, and marks.
o Uses calculate_grade() to determine the grade.
o Displays the student’s information and grade.
CODE:
from student_management import student as s,grade as g n=int(input("enter
no of students:"))
for i in range(0,n):
id=int(input(f"enter id of student {i+1}:"))
names=input(f"enter name of student {i+1}:")
marks=int(input(f"enter marks scored:"))
s.details(id,names,marks)
print("Students Marks Details:") for
keys,values in s.name.items():
x=s.name[keys]
y=keys
z=s.mark[keys]
gr=g.grade(z)
print(f"Id:{y} Name:{x} Marks:{z} Grade:{gr}")
OUTPUT:
3. Create a Package for Banking System
Develop a package called banking that contains:
o account.py (Handles account details: account number, holder
name, balance).
o transactions.py (Handles deposits, withdrawals, and balance
inquiries).
Implement a script (bank.py) that:
o Creates a bank account.
o Allows the user to deposit, withdraw, and check balance.
CODE:
from banking import accounts as a,transactions as t
x=1
while(x==1):
print("1.Create account")
print("2.Sign in")
print("3.exit")
a1=int(input("enter choice:"))
if a1==1:
str=input("enter name:")
ax=int(input("enter initial amount:"))
a.account(str,ax)
elif a1==2:
x1=int(input("enter account no:"))
y2=0
while(y2==0):
print("1.deposit \n 2.withdraw \n 3.Enquire Balance\n 4.exit")
y=int(input("enter choice:"))
if y==1:
t.deposit(x1)
elif y==2:
t.withdraw(x1)
elif y==3:
t.balance(x1)
elif y==4:
y2=1
elif a1==3:
x=0
OUTPUT:
RESULT:
The python programs using modules and packages have been implemented and
verified sucessfully.