Practical Record Book - Python
Practical Record Book - Python
BY
This is to certify that I have completed the Python Practical Record Book for
5 How to access modules saved inside other locations into current module?
9 Create a CSV file with the name emp.csv and write data into the file.
10 Create a CSV file with the name emp.csv and read data from it.
11 Create a file with the name student.txt and write data to the file.
12 Create a file with the name student.txt and read data from the file.
1) Windows Operating System: Default PC operating system for the lab Experiments.
3) IDLE Shell 3.12.2 – Integrated Development Environment (IDE) used to write the source
code and output the compiled results of the Experiments.
Source code:
p={}
while True:
print("1. Adding")
print("2. Updating")
print("3. Finding")
print("4. Score Board")
print("5. Exit")
opt=int(input("Enter your option"))
if opt==1:
name=input("Enter Name")
score=int(input("Enter Score"))
if name in p:
print(name,"is exists")
else:
p[name]=score
elif opt==2:
name=input("Enter Name")
if name in p:
s=int(input("Enter Updated Score"))
p[name]=s
else:
print(name,"not exists")
elif opt==3:
name=input("Enter name")
if name in p:
s=p[name]
print("score ",s)
else:
print(name,"is not exists")
elif opt==4:
tot=0
for x in p.items():
name,score=x
print(name,score)
tot=tot+score
print("Total Score :",tot)
elif opt==5:
break
Output:
Finding + Exit:
Experiment-2: Program to find factorial of a number using recursion:
Source code:
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
def main():
num=int(input("enter any number"))
res=factorial(num)
print(f'factorial is {res}')
main()
Output:
Scenario1 – num=6:
Scenario2 – num=9:
Scenario3 – num=20:
Scenario4 – num=90:
Scenario5 – num=35:
Experiment-3: Program to define a module and import a specific function from that module to
another module.
Source code:
def fun1():
print("inside fun1 of module1")
def fun2():
print("inside fun2 of module1")
def main():
print(x)
print(y)
fun1()
main()
Output:
Experiment-4: Program to define a module and import the content of another module with
alias name used.
Source code:
#Module1-saved as Exp4_module1
x=100 #global
y=200 #global
def fun1():
print("inside fun1 of module1")
def fun2():
print("inside fun2 of module1")
#Module2-saved as Exp4_module2
from Exp4_module1 import x as k
from Exp4_module1 import fun1 as f1
def fun1():
print("fun1 of current module")
x=99
def main():
print(x)
fun1()
print(k)
f1()
main()
Output:
Experiment-5: How to access modules saved inside other locations into current module?
Source code:
#Module1-saved as Exp5_module1
def fun1():
print("fun1 of module1")
#Module2-saved as Exp5_module2
import sys
sys.path.append(r"C:\Users\CARE
IT\Documents\Isbat\Via_Aspiration\AIT_Isbat\SEM4\Python\Lab")
import Exp5_module1
def main():
Exp5_module1.fun1()
main()
Output:
Experiment-6: Program to demonstrate working with LIST in python.
Source code:
scores=[]
Output:
Scenario1 - n=5:
Scenario2 - n=10:
Experiment-7: Program to demonstrate working with SET in python.
Source code:
while True:
print("***Email Book***")
print("1. Add")
print("2. Search")
print("3. Remove")
print("4. List")
print("5. Exit")
Add + List:
Search:
Remove + List:
Experiment-8: Implement multilevel inheritance in python.
Source code:
# Multilevel inheritance
class Animal:
def speak(self):
print("Animal Speaking")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
Experiment-9: Create a CSV file with the name emp.csv and write data into the file.
Source code:
import csv
def main():
try:
f=open("emp.csv","w",newline='')
cw=csv.writer(f)
cw.writerow(['empno','ename','salary'])
cw.writerow([101,'naresh',50000.0])
cw.writerow([102,'suresh',65000.0])
cw.writerow([103,'kiran',75000.0])
cw.writerow([104,'ramesh',70000.0])
except OSError:
print("error in creating csv file")
finally:
f.close()
main()
Output – CSV file opened to view the contents defined in the module:
Experiment-10: Create a CSV file with the name emp.csv and read data from it.
Source code:
import csv
def main():
try:
f=open("emp.csv","r")
cr=csv.reader(f)
for row in cr:
print(row)
f.close()
f=open("emp.csv","r")
cr=csv.reader(f)
empList=list(cr)
print(empList)
salaryList=[float(empList[i][2]) for i in range(1,len(empList))]
print(salaryList)
print(f'Maximum Salary :{max(salaryList)}')
print(f'Minimum Salary :{min(salaryList)}')
print(f'Total Salary :{sum(salaryList)}')
except FileNotFoundError:
print("file not found")
main()
Output - Reference to CSV file created in Experiment-9 above. Below the contents read in IDLE.
Experiment-11: Create a file with the name student.txt, and write data to the file.
Source code:
except OSError:
print("unable to create file")
finally:
f.close()
main()
Output – Text file created under same folder as module, and opened to view the added contents:
Experiment-12: Create a file with the name student.txt, and read data from the file.
Source code:
try:
f=open("student.txt","r")
s=f.read()
print(s)
except FileNotFoundError:
print("file not found")
finally:
f.close()
main()
Output - Reference to Text file created in Experiment-11 above. Below the contents read in IDLE:
Experiment-13: Implement the working of packages.
Source code:
#Create a folder with name package1 in a given location, and add two python modules.
#Module1: saved as Exp13_module1 under folder “package1”.
def fun1():
print("inside fun1")
def fun2():
print("inside fun2")
def fun3():
print("inside fun3")
import sys
sys.path.append(r"C:\Users\CARE
IT\Documents\Isbat\Via_Aspiration\AIT_Isbat\SEM4\Python\Lab\package1")
import package1.Exp13_module1
import package1.Exp13_module2
def main():
package1.Exp13_module1.fun1()
package1.Exp13_module1.fun2()
package1.Exp13_module1.fun3()
package1.Exp13_module2.fun4()
main()
Output:
Experiment-14: Implement calculator program using functions.
Source code:
def calculator(n1,n2,opr):
def add():
return n1+n2
def sub():
return n1-n2
def multiply():
return n1*n2
def div():
return n1/n2
if opr=='+':
print(f'Result is {add()}')
elif opr=='-':
print(f'Result is {sub()}')
elif opr=='*':
print(f'Result is {multiply()}')
elif opr=='/':
print(f'Result is {div()}')
def main():
num1=int(input("Enter First Number"))
num2=int(input("Enter Second Number"))
opr=input("Enter Operator")
calculator(num1,num2,opr)
main()
Output: