SlideShare a Scribd company logo
COMPUTER
SCIENCE
TERM-1
PRACTICAL FILE
NAME-YASHRAJ PAHUJA
CLASS-12th
/B
ROLL NO-25
INDEX
S.
No.
Practical Date Signature
1 Any two patterns using nested for loop up to n
times where n is input by the user.
24 April 2023
2 Implement bubble sort using UDF on a list input by
the user.
24 April 2023
3 Implement insertion sort using UDF on a list input
by the user.
24 April 2023
4 Write python code which uses a UDF
‘check_perfect’ that takes ‘n’ as a parameter and
checks whether ‘n’ is a perfect number or not.
24 April 2023
5 Write python code which uses UDF
‘check_armstrong’ that takes a parameter ‘n’ and
checks whether ‘n’ is an armstrong number or not.
24 April 2023
6 Write python code which creates a menu for the
following options :
1. Check whether number is perfect or not
2. Check whether number is armstrong or not
3. Display prime numbers from the list
4. Check whether list is sparse or not
5. Exit
11 May 2023
7 30 May 2023
8 30 May 2023
9 30 May 2023
10 30 May 2023
11 30 May 2023
12 Write python code which uses UDF 'add_recirds()'
to add the records of employees where each
employee contains employee number, name,
salary and department. Ask the data from the user
till the user wishes and store all the records in
'employee.dat' file. Write function 'display_record()'
which reads the record of binary file employee.dat
and displays it.
14 July 2023
13 Create a menu driven program to input the details
of a voter including voter ID, voter name, age,
address.
1. Create a binary file 'Voter.dat'
2. Read the records of the binary file and
display them.
3. Search for the voters whose age is above
60 and display them.
Use UDF to perform the above functions.
14 July 2023
14 Write a menu driven program which performs a
specific operation on CSV file 'employee.csv' which
contains employee number, name, salary and
designation.
1. Create a file
2. Display the records
3. Search for employee number
4. Search for salary
5. Exit
31 July 2023
15 Write python code which create a 'voter.csv' file
and using UDF, perform the following operations
1. Create 'voter.csv' file. (Voter ID, name, age,
address)
2. Display the records of voters with age
above 60.
3. Display records of voters who stay in
Rajouri garden with age above 21.
31 July 2023
16 Write python code which uses UDF 'count()' that
takes filename as parameter and returns the count
of lines in the file beginning with letter 'T'.
7 August
2023
17 Write python code which uses function
'count_words()' that read the contents of the file
'story.txt' and display the number of words from the
file.( If the file is not created then the program
creates the file.)
7 August
2023
18 A binary file 'grade.dat' is required to be created by
inputting records from the user. Perform the
following operations using UDF. (Admission
number, Name, marks, grade)
1. Create the binary file 'great.dat' by inputting
records from the user till user wishes
2. Display the records of the students who
have secured more than 90 marks
7 August
2023
3. Update the grade of the student whose
name is input by the user from B grade to A
grade.
4. Display all the records
19 Write python code which create a CSV file
'inventory.csv' containing item quote, item name,
company, price and quantity. Use UDF to perform
the following operations.
1. Create the CSV file (Till the user wish)
2. Display all the records of the CSV file.
3. Display the details of those items whose
price is more than 40,000.
7 August
2023
20 Write python code which implements stack as a list
containing details of a customer having customer
name, phone no, and city. The menu driven
program should provide option 1. Push, 2. Pop, 3.
Display. The program should continue till the user
wishes, taking care of all the validations.
23 August
2023
21 Write python code which creates a menu
implementing user defined functions on a CSV file.
1. Create a CSV file by writing 1 record at a
time.
2. Write multiple records in one go.
3. Display the records of the CSV file.
4. Display the records of employee with salary
input by the user.
5. Exit
The CSV file contains employee no, name,
designation, salary and age.
23 August
2023
22 Write python code that uses UDF ‘create_file()’ that
creates a text file with inputs from the user. The
program/function displays the number of uppercase
vowels, lowercase vowels, number of words
starting with ‘A’ and number of lines starting with
‘A’.
1 September
2023
Q1. Any two patterns using nested for
loop upto n times where n is input by the
user?
Ans1.
n=int(input("enter the number"))
for i in range (1,n):
for j in range (1,6):
print ("*",end=" ")
print()
Ans2.
n=int(input("enter the number"))
for i in range (1,n):
for j in range (1,8):
print (j,end=" ")
print ()
Q2. Implement bubble sort using UDF on
a list input by the user.
Ans2.
def bubble_sort(l):
n = len(l)
for i in range (0,n):
for j in range(n-i-1):
if l[j]> l[j+1]:
l[j],l[j+1] = l[j+1], l[j]
return l
l = []
k = int (input("enter the limit"))
for i in range (0,k):
val = int(input("enter numbers "))
l.append(val)
f = bubble_sort(l)
print(f)
Q3. Implement insertion sort using UDF
on a list input by the user.
Ans.
Q4. Write python code which uses a UDF
‘check_perfect’ that takes ‘n’ as a
parameter and checks whether ‘n’ is a
perfect number or not.
Ans.
def check(n):
sum=0
for i in range(1,n):
if n%i==0:
sum=sum+i
if sum==n:
print("it is a perfect number")
else:
print("it is not a perfect number")
n=int(input("enter the number"))
check(n)
Q5. Write python code which uses UDF
‘check_armstrong’ that takes a parameter
‘n’ and checks whether ‘n’ is an armstrong
number or not.
Ans.
Output-
: Q6. Write python code which creates a
menu for the following options
1.Check whether number is perfect or not
2.Check whether number is armstrong or not
3.Display prime numbers from the list
4.Check whether list is sparse or not
5.Exit
def check_perfect():
x=int(input("enter the number:"))
sum=0
for i in range(1,x):
if x%i==0:
sum+=i
if sum==x:
print(x,"perfect number")
else:
print(x,"not a perfect number")
def check_amstrong():
n=int(input("enter the number"))
f=n
r=n
while f>n:
digit=f%10
r=r+(digit**3)
f=f//10
if r==n:
print("number is amstrong")
else:
print("number is not amstrong")
def prime():
m=list()
k=int(input("enter the size of list"))
c=list()
for i in range(0,k):
val=int(input("enter the value"))
m.append(val)
for j in m:
ctr=0
for s in range(1,j+1):
if j%s==0:
ctr=+1
if ctr==2:
c.append(j)
print(c)
def sparse():
l=list()
n=int(input("enter the size of list"))
for i in range(0,n):
val=int(input("enter the valeu"))
l.append(V)
ctrA=0
ctrB=0
for v in i:
if v==0:
ctrA+=1
elif v!=0:
ctrB+=1
if ctrA>ctrB:
print("list is sparse")
else:
print("list is not sparse")
def exit():
print("exit")
choice='y'
while choice in 'y':
print("the options are:")
print("#1.check number is perfect or not")
print("#2.check whether number us amstrong or
not")
print("#3.display prime number from the list")
print("#4.check whether no is sparse or not")
print("#5.exit")
ch=int(input("enter your choice"))
if ch==1:
check_perfect()
elif ch==2:
check_amstrong()
elif ch==3:
prime()
elif ch==4:
sparse()
elif ch==5:
exit()
else:
print("INVALID CHOICE")
choice=input("dp you want to continue(y,n)")
Q)7write python code which reads from the
text file school.txt and display the contents
line wise.
Ans.
OUTPUT-
Q8)write python code which read from the
text file YASHRAJ HERE WANNA SEE ME
.HAHAHAHAHA.txt and display those lines
which start from ‘H’
Ans.
OUTPUT-
Q9)write python code which reads lines
from the text file story.txt and count the
number of lines beginning with an upper
case vowel.
ANS.
f1=open("story.txt","r")
lines=f1.readlines()
ctr=0
for i in lines:
if i[0]in'AEIOU':
ctr+ctr+1
print(ctr)
Q10)write python code which read lines
from the text file story .txt and display those
lines whose length is more than 10
characters
Ans.
OUTPUT-
Q11)write python code which creates a file
paragraph.txt and count the number of
words having length exactly five.
Ans.
f1=open("paragraph.txt","w")
ch="y"
while ch in "yY":
string=input("enter lines of data")
f1.write(string+"/n")
ch=input("do you want to add more")
f1.close()
f2=open("paragraph.txt","r")
ctr=0
data=f2.read()
words=data.split()
for w in words:
if len(w)==5:
ctr=ctr+1
print(ctr)
Q12. Write python code which uses UDF
'add_recirds()' to add the records of employees
where each employee contains employee number,
name, salary and department. Ask the data from
the user till the user wishes and store all the
records in 'employee.dat' file. Write function
'display_record()' which reads the record of binary
file employee.dat and displays it.
Ans.
OUTput
Q13. Create a menu driven program to
input the details of a voter including voter
ID, voter name, age, address.
1. Create a binary file 'Voter.dat'
2. Read the records of the binary file
and display them.
3. Search for the voters whose age is
above 60 and display them.
Ans.
import pickle
def add_records():
f1=open("voter.dat",'wb')
rec=[]
choice='y'
while choice in 'Yy':
vid=int(input("Enter voter ID : "))
name=input("Enter name : ")
age=float(input("Enter age : "))
add=input("Enter address : ")
L=[vid, name, age, add]
rec.append(L)
choice=input("Do you want to add more records
?(y/n)")
pickle.dump(rec,f1)
f1.close()
def display_record():
f2=open("voter.dat",'rb')
data=pickle.load(f2)
print(data)
def search_record():
L=[]
f3=open('voter.dat','rb')
L=pickle.load(f3)
for a in L:
if a[2]>60:
print(a)
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records.")
print("3. Search for voters' above age 60.")
print("4. Exit")
k=int(input("Enter your choice: "))
if k==1:
add_records()
elif k==2:
display_record()
elif k==3:
search_record()
elif k==4:
break
else:
print("INVALID CHOICE")
choice=input("Do you want to continue? (y/n)")
Output
Q14. Write a menu driven program which
performs a specific operation on CSV file
'employee.csv' which contains employee
number, name, salary and designation.
1. Create a file
2. Display the records
3. Search for employee number
4. Search for salary
5. Exit
Ans.
import csv
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records.")
print("3. Search for employee number.")
print("4. Search for salary")
print("5. Exit")
k=int(input("Enter your choice: "))
if k==1:
f1=open("employee.csv",'w')
c1=csv.writer(f1)
L=['Employee no.', 'Name', 'Salary',
'Designation']
c1.writerow(L)
choice='y'
while choice in 'Yy':
eno=int(input("Enter employee
number : "))
name=input("Enter name : ")
sal=float(input("Enter salary : "))
des=input("Enter designation : ")
L=[eno, name, sal, des]
c1.writerow(L)
choice=input("Do you want to add
more records ?(y/n)")
f1.close()
elif k==2:
f2=open("employee.csv",'r')
c1=csv.reader(f2)
for rec in c1:
if rec!=[]:
print(rec)
elif k==3:
s=int(input("Enter employee number :
"))
f3=open('employee.csv','r')
c2=csv.reader(f3)
next(c2)
for row in c2:
if row!=[] and int(row[0])==s:
print(row)
elif k==4:
t=int(input("Enter salary : "))
L2=[]
f4=open('employee.csv','r')
c3=csv.reader(f4)
next(c3)
for rec in c3:
if rec!=[] and float(rec[2])==t:
print(rec)
elif k==5:
break
choice=input("Do you want to continue?
(y/n)")
Q15. Write python code which create a
'voter.csv' file and using UDF, perform the
following operations
1. Create 'voter.csv' file. (Voter ID,
name, age, address)
2. Display the records of voters with
age above 60.
3. Display records of voters who stay
in Rajouri garden with age above 21.
Ans.
import csv
def add():
f1=open("voter.csv",'w')
c1=csv.writer(f1)
L=['Voter ID', 'Name', 'Age', 'Address']
c1.writerow(L)
choice='y'
while choice in 'Yy':
vid=int(input("Enter Voter ID : "))
name=input("Enter name : ")
add=input("Enter address : ")
age=int(input("Enter age : "))
L=[vid, name, age, add]
c1.writerow(L)
choice=input("Do you want to add more records ?(y/n)")
f1.close()
def search1():
f2=open("voter.csv",'r')
c2=csv.reader(f2)
next(c2)
for rec in c2:
if rec!=[] and int(rec[2])>60:
print(rec)
def search2():
f3=open('voter.csv','r')
c3=csv.reader(f3)
next(c3)
for rec in c3:
if rec!=[] and rec[3]=="Rajouri Garden" and int(rec[2])<21:
print(rec)
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records of voters with age above 60.")
print("3. Display records of voters who stay in Rajouri garden with age above 21.")
print("4. Exit")
k=int(input("Enter your choice: "))
if k==1:
add()
elif k==2:
search1()
elif k==3:
search2()
elif k==4:
break
choice=input("Do you want to continue? (y/n)")
Output
Q.16 write python code which uses UDF
"count()" that takes one parameters
filename and returns the count of lines in
the file beginning with letter "T"
Ans.
fv=open('letsgo.txt','w')
l=int(input("How many lines do you want in the file? "))
for i in range(0, l):
t= input("Write the line you want to enter in the file: ")
fv.write(t + "n")
fv.close()
def count(filename):
with open(filename, 'r') as file:
lines = file.readlines()
count = 0
for line in lines:
count += 1
return count
filename = 'letsgo.txt'
line_count = count(filename)
print("The number of lines starting with 'T' in {filename}
is:", line_count)
Q.17 Write python code which uses
function “count words( )” that reads the
contents of the file "string.txt" and display
the number of words from the file . If file is
not created then the program creates the
file
Ans.
def count_words():
ctr=0
f1=open("Story.txt",'r')
data=f1.read()
w=data.split()
for i in w :
ctr=ctr+1
print("No. of words are : ", ctr)
count_words()
Q18. A binary file 'grade.dat' is
required to be created by inputting
records from the user. Perform the
following operations using UDF.
(Admission number, Name, marks,
grade)
1. Create the binary file 'great.dat'
by inputting records from the user
till user wishes
2. Display the records of the
students who have secured more
than 90 marks
3. Update the grade of the student
whose name is input by the user
from B grade to A grade.
Display all the records.
Ans.
import pickle
def add():
f1=open("grade.dat",'ab')
rec=[]
choice='y'
while choice in 'Yy':
add=int(input("Enter admission no. : "))
name=input("Enter name : ")
mark=float(input("Enter marks : "))
gra=input("Enter grade : ")
L=[add, name, mark, gra]
rec.append(L)
choice=input("Do you want to add more records
?(y/n)")
pickle.dump(rec,f1)
f1.close()
def search():
f2=open("grade.dat",'rb')
data=pickle.load(f2)
for d in data:
if d[2]>90:
print(d)
def update():
n=input("Enter the name of student : ")
f3=open("grade.dat",'rb+')
data=pickle.load(f3)
for d in data:
if d[1]==n:
d[3]='A'
f3.seek(0)
pickle.dump(data,f3)
f3.close()
def display():
with open("grade.dat",'rb')as f4:
data=pickle.load(f4)
for rec in data:
print(rec)
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records of students who have scored
above 90 marks.")
print("3. Update grade of students from B to A.")
print("4. Display all records")
k=int(input("Enter your choice: "))
if k==1:
add()
elif k==2:
search()
elif k==3:
update()
elif k==4:
display()
else:
print("INVALID CHOICE")
choice=input("Do you want to continue? (y/n)")
Q19. Write python code which create a
CSV file 'inventory.csv' containing item
quote, item name, company, price and
quantity. Use UDF to perform the
following operations.
1. Create the CSV file (Till the user
wish)
2. Display all the records of the CSV
file.
3. Display the details of those items
whose price is more than 40,000.
Ans.
import csv
def add():
f1=open("inventory.csv",'a')
c1=csv.writer(f1)
L=['Item quote', 'Item name', 'Company', 'Price', 'Quantity']
c1.writerow(L)
choice='y'
while choice in 'Yy':
iq=float(input("Enter Item quote: "))
name=input("Enter Item name : ")
com=input("Enter company : ")
pr=float(input("Enter price : "))
qu=int(input("Enter quantity : "))
L=[iq, name, com, pr, qu]
c1.writerow(L)
choice=input("Do you want to add more records ?(y/n)")
f1.close()
def display():
f2=open("inventory.csv",'r')
c2=csv.reader(f2)
next(c2)
for rec in c2:
if rec!=[]:
print(rec)
def search():
f3=open('inventory.csv','r')
c3=csv.reader(f3)
next(c3)
for rec in c3:
if rec!=[] and float(rec[3])>40000:
print(rec)
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records.")
print("3. Display details of those items whose price is above 40000.")
k=int(input("Enter your choice: "))
if k==1:
add()
elif k==2:
display()
elif k==3:
search()
choice=input("Do you want to continue? (y/n)")
Q20. Write python code which implement
stack as a list containing details of a
customer having customer name, phone
no, and city. The menu driven program
should provide the option 1. Push, 2. Pop,
3. Display. The program should continue
till the user wishes, taking care of all the
validations.
Ans.
stack=[]
choice='y'
while choice in 'Yy':
print("1.Push")
print("2.Pop")
print("3.Display")
print("4.Exit")
ch=int(input("Enter you choice : "))
if ch==1:
name=input("Enter you name : ")
no=int(input("Enter phone no. : "))
city=input("Enter city : ")
det=[name, no, city]
stack.append(det)
elif ch==2:
if stack==[]:
print("Stack underflow")
else:
stack.pop()
elif ch==3:
if len(stack)==0:
print("Stack empty")
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i])
elif ch==4:
break
choice=input("Do you want to continue?(y/n)")
OUTPUT.
Q21. Write python code which creates a
menu implementing user defined functions
on CSV file.
1. Create a CSV file by writing 1
record at a time.
2. Write multiple records in one go.
3. Display the records of the CSV file.
4. Display the records of employ with
salary input by the user.
5. Exit
The CSV file contains employee no,
name, designation, salary
Ans.
import csv
def add():
f1=open("employees.csv",'w')
c1=csv.writer(f1)
L=['Employee no.', 'Name', 'Designation', 'Salary',
'Age']
c1.writerow(L)
choice='y'
while choice in 'Yy':
eno=int(input("Enter employee number : "))
name=input("Enter name : ")
sal=float(input("Enter salary : "))
des=input("Enter designation : ")
age=int(input("Enter age : "))
L=[eno, name, des, sal, age]
c1.writerow(L)
choice=input("Do you want to add more records
?(y/n)")
f1.close()
def display():
f2=open("employees.csv",'r')
c2=csv.reader(f2)
for rec in c2:
if rec!=[]:
print(rec)
def search():
t=int(input("Enter salary : "))
f3=open('employees.csv','r')
c3=csv.reader(f3)
next(c3)
for rec in c3:
if rec!=[] and float(rec[3])==t:
print(rec)
choice='y'
while choice in 'Yy':
print("The options are: ")
print("1. Enter new records.")
print("2. Display records.")
print("3. Search for salary")
print("4. Exit")
k=int(input("Enter your choice: "))
if k==1:
add()
elif k==2:
display()
elif k==3:
search()
elif k==4:
break
choice=input("Do you want to continue? (y/n):")
OUTput
Print(“exit”)
Print(“project completed successfully by yashraj ”)
THANK YOU

More Related Content

Similar to computer science practical.pdf

ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptx
jeyel85227
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
EliasPetros
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
vikram mahendra
 
2012 bcsl-021 solve assihnment
2012 bcsl-021 solve assihnment2012 bcsl-021 solve assihnment
2012 bcsl-021 solve assihnment
Indira Gnadhi National Open University (IGNOU)
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
Animesh Chaturvedi
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
Ch 4
Ch 4Ch 4
Ch 4
AMIT JAIN
 
complex c programming
complex c programming complex c programming
complex c programming
CenturyLink India
 
F# Console class
F# Console classF# Console class
F# Console class
DrRajeshreeKhande
 
Computer Science Sample Paper 2015
Computer Science Sample Paper 2015Computer Science Sample Paper 2015
Computer Science Sample Paper 2015
Poonam Chopra
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
PRACTICAL_FILE_XII_,_ARCHANA[1].pptx
PRACTICAL_FILE_XII_,_ARCHANA[1].pptxPRACTICAL_FILE_XII_,_ARCHANA[1].pptx
PRACTICAL_FILE_XII_,_ARCHANA[1].pptx
GarvNanda
 
Master Python in 15 Days.pdf
Master Python in 15 Days.pdfMaster Python in 15 Days.pdf
Master Python in 15 Days.pdf
JorgeTorres560505
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
KimVeeL
 
PostThis
PostThisPostThis
PostThis
testingphase
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
chinthala Vijaya Kumar
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
janakim15
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
rajatxyz
 
paython practical
paython practical paython practical
paython practical
Upadhyayjanki
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
Last7693
 

Similar to computer science practical.pdf (20)

ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptx
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
2012 bcsl-021 solve assihnment
2012 bcsl-021 solve assihnment2012 bcsl-021 solve assihnment
2012 bcsl-021 solve assihnment
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Ch 4
Ch 4Ch 4
Ch 4
 
complex c programming
complex c programming complex c programming
complex c programming
 
F# Console class
F# Console classF# Console class
F# Console class
 
Computer Science Sample Paper 2015
Computer Science Sample Paper 2015Computer Science Sample Paper 2015
Computer Science Sample Paper 2015
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
PRACTICAL_FILE_XII_,_ARCHANA[1].pptx
PRACTICAL_FILE_XII_,_ARCHANA[1].pptxPRACTICAL_FILE_XII_,_ARCHANA[1].pptx
PRACTICAL_FILE_XII_,_ARCHANA[1].pptx
 
Master Python in 15 Days.pdf
Master Python in 15 Days.pdfMaster Python in 15 Days.pdf
Master Python in 15 Days.pdf
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
PostThis
PostThisPostThis
PostThis
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
paython practical
paython practical paython practical
paython practical
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 

Recently uploaded

一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
peuce
 
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDARLORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
lorraineandreiamcidl
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
aozcue
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
aozcue
 
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
andreassenrolf537
 
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdfSchematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
nikoloco007
 
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
arcosarturo900
 
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Peter Gallagher
 

Recently uploaded (8)

一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
 
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDARLORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
LORRAINE ANDREI_LEQUIGAN_GOOGLE CALENDAR
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
 
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
 
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdfSchematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
Schematic Diagram MSI MS-7309 - REV 1.0 PDF .pdf
 
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
天博体育下载-可靠的网络天博体育下载-网络天博体育下载|【​网址​🎉ac123.net🎉​】
 
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
 

computer science practical.pdf

  • 2. INDEX S. No. Practical Date Signature 1 Any two patterns using nested for loop up to n times where n is input by the user. 24 April 2023 2 Implement bubble sort using UDF on a list input by the user. 24 April 2023 3 Implement insertion sort using UDF on a list input by the user. 24 April 2023 4 Write python code which uses a UDF ‘check_perfect’ that takes ‘n’ as a parameter and checks whether ‘n’ is a perfect number or not. 24 April 2023 5 Write python code which uses UDF ‘check_armstrong’ that takes a parameter ‘n’ and checks whether ‘n’ is an armstrong number or not. 24 April 2023 6 Write python code which creates a menu for the following options : 1. Check whether number is perfect or not 2. Check whether number is armstrong or not 3. Display prime numbers from the list 4. Check whether list is sparse or not 5. Exit 11 May 2023 7 30 May 2023 8 30 May 2023 9 30 May 2023 10 30 May 2023 11 30 May 2023 12 Write python code which uses UDF 'add_recirds()' to add the records of employees where each employee contains employee number, name, salary and department. Ask the data from the user till the user wishes and store all the records in 'employee.dat' file. Write function 'display_record()' which reads the record of binary file employee.dat and displays it. 14 July 2023
  • 3. 13 Create a menu driven program to input the details of a voter including voter ID, voter name, age, address. 1. Create a binary file 'Voter.dat' 2. Read the records of the binary file and display them. 3. Search for the voters whose age is above 60 and display them. Use UDF to perform the above functions. 14 July 2023 14 Write a menu driven program which performs a specific operation on CSV file 'employee.csv' which contains employee number, name, salary and designation. 1. Create a file 2. Display the records 3. Search for employee number 4. Search for salary 5. Exit 31 July 2023 15 Write python code which create a 'voter.csv' file and using UDF, perform the following operations 1. Create 'voter.csv' file. (Voter ID, name, age, address) 2. Display the records of voters with age above 60. 3. Display records of voters who stay in Rajouri garden with age above 21. 31 July 2023 16 Write python code which uses UDF 'count()' that takes filename as parameter and returns the count of lines in the file beginning with letter 'T'. 7 August 2023 17 Write python code which uses function 'count_words()' that read the contents of the file 'story.txt' and display the number of words from the file.( If the file is not created then the program creates the file.) 7 August 2023 18 A binary file 'grade.dat' is required to be created by inputting records from the user. Perform the following operations using UDF. (Admission number, Name, marks, grade) 1. Create the binary file 'great.dat' by inputting records from the user till user wishes 2. Display the records of the students who have secured more than 90 marks 7 August 2023
  • 4. 3. Update the grade of the student whose name is input by the user from B grade to A grade. 4. Display all the records 19 Write python code which create a CSV file 'inventory.csv' containing item quote, item name, company, price and quantity. Use UDF to perform the following operations. 1. Create the CSV file (Till the user wish) 2. Display all the records of the CSV file. 3. Display the details of those items whose price is more than 40,000. 7 August 2023 20 Write python code which implements stack as a list containing details of a customer having customer name, phone no, and city. The menu driven program should provide option 1. Push, 2. Pop, 3. Display. The program should continue till the user wishes, taking care of all the validations. 23 August 2023 21 Write python code which creates a menu implementing user defined functions on a CSV file. 1. Create a CSV file by writing 1 record at a time. 2. Write multiple records in one go. 3. Display the records of the CSV file. 4. Display the records of employee with salary input by the user. 5. Exit The CSV file contains employee no, name, designation, salary and age. 23 August 2023 22 Write python code that uses UDF ‘create_file()’ that creates a text file with inputs from the user. The program/function displays the number of uppercase vowels, lowercase vowels, number of words starting with ‘A’ and number of lines starting with ‘A’. 1 September 2023
  • 5. Q1. Any two patterns using nested for loop upto n times where n is input by the user? Ans1. n=int(input("enter the number")) for i in range (1,n): for j in range (1,6): print ("*",end=" ") print() Ans2. n=int(input("enter the number")) for i in range (1,n): for j in range (1,8): print (j,end=" ") print ()
  • 6. Q2. Implement bubble sort using UDF on a list input by the user. Ans2. def bubble_sort(l): n = len(l) for i in range (0,n): for j in range(n-i-1): if l[j]> l[j+1]: l[j],l[j+1] = l[j+1], l[j] return l l = [] k = int (input("enter the limit")) for i in range (0,k): val = int(input("enter numbers ")) l.append(val) f = bubble_sort(l) print(f)
  • 7. Q3. Implement insertion sort using UDF on a list input by the user. Ans.
  • 8. Q4. Write python code which uses a UDF ‘check_perfect’ that takes ‘n’ as a parameter and checks whether ‘n’ is a perfect number or not. Ans. def check(n): sum=0 for i in range(1,n): if n%i==0: sum=sum+i if sum==n: print("it is a perfect number") else: print("it is not a perfect number") n=int(input("enter the number")) check(n)
  • 9. Q5. Write python code which uses UDF ‘check_armstrong’ that takes a parameter ‘n’ and checks whether ‘n’ is an armstrong number or not. Ans. Output-
  • 10. : Q6. Write python code which creates a menu for the following options 1.Check whether number is perfect or not 2.Check whether number is armstrong or not 3.Display prime numbers from the list 4.Check whether list is sparse or not 5.Exit def check_perfect(): x=int(input("enter the number:")) sum=0 for i in range(1,x): if x%i==0: sum+=i if sum==x: print(x,"perfect number") else: print(x,"not a perfect number") def check_amstrong(): n=int(input("enter the number")) f=n r=n while f>n: digit=f%10 r=r+(digit**3) f=f//10 if r==n:
  • 11. print("number is amstrong") else: print("number is not amstrong") def prime(): m=list() k=int(input("enter the size of list")) c=list() for i in range(0,k): val=int(input("enter the value")) m.append(val) for j in m: ctr=0 for s in range(1,j+1): if j%s==0: ctr=+1 if ctr==2: c.append(j) print(c) def sparse(): l=list() n=int(input("enter the size of list")) for i in range(0,n): val=int(input("enter the valeu")) l.append(V) ctrA=0 ctrB=0 for v in i: if v==0: ctrA+=1
  • 12. elif v!=0: ctrB+=1 if ctrA>ctrB: print("list is sparse") else: print("list is not sparse") def exit(): print("exit") choice='y' while choice in 'y': print("the options are:") print("#1.check number is perfect or not") print("#2.check whether number us amstrong or not") print("#3.display prime number from the list") print("#4.check whether no is sparse or not") print("#5.exit") ch=int(input("enter your choice")) if ch==1: check_perfect() elif ch==2: check_amstrong() elif ch==3: prime() elif ch==4: sparse() elif ch==5: exit() else:
  • 14. Q)7write python code which reads from the text file school.txt and display the contents line wise. Ans. OUTPUT-
  • 15. Q8)write python code which read from the text file YASHRAJ HERE WANNA SEE ME .HAHAHAHAHA.txt and display those lines which start from ‘H’ Ans. OUTPUT-
  • 16. Q9)write python code which reads lines from the text file story.txt and count the number of lines beginning with an upper case vowel. ANS. f1=open("story.txt","r") lines=f1.readlines() ctr=0 for i in lines: if i[0]in'AEIOU': ctr+ctr+1 print(ctr)
  • 17. Q10)write python code which read lines from the text file story .txt and display those lines whose length is more than 10 characters Ans. OUTPUT-
  • 18. Q11)write python code which creates a file paragraph.txt and count the number of words having length exactly five. Ans. f1=open("paragraph.txt","w") ch="y" while ch in "yY": string=input("enter lines of data") f1.write(string+"/n") ch=input("do you want to add more") f1.close() f2=open("paragraph.txt","r") ctr=0 data=f2.read() words=data.split() for w in words: if len(w)==5: ctr=ctr+1 print(ctr)
  • 19. Q12. Write python code which uses UDF 'add_recirds()' to add the records of employees where each employee contains employee number, name, salary and department. Ask the data from the user till the user wishes and store all the records in 'employee.dat' file. Write function 'display_record()' which reads the record of binary file employee.dat and displays it. Ans.
  • 21. Q13. Create a menu driven program to input the details of a voter including voter ID, voter name, age, address. 1. Create a binary file 'Voter.dat' 2. Read the records of the binary file and display them. 3. Search for the voters whose age is above 60 and display them. Ans. import pickle def add_records(): f1=open("voter.dat",'wb') rec=[] choice='y' while choice in 'Yy': vid=int(input("Enter voter ID : ")) name=input("Enter name : ") age=float(input("Enter age : ")) add=input("Enter address : ") L=[vid, name, age, add] rec.append(L) choice=input("Do you want to add more records ?(y/n)") pickle.dump(rec,f1) f1.close() def display_record():
  • 22. f2=open("voter.dat",'rb') data=pickle.load(f2) print(data) def search_record(): L=[] f3=open('voter.dat','rb') L=pickle.load(f3) for a in L: if a[2]>60: print(a) choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records.") print("3. Search for voters' above age 60.") print("4. Exit") k=int(input("Enter your choice: ")) if k==1: add_records() elif k==2: display_record() elif k==3: search_record() elif k==4: break else: print("INVALID CHOICE")
  • 23. choice=input("Do you want to continue? (y/n)") Output
  • 24. Q14. Write a menu driven program which performs a specific operation on CSV file 'employee.csv' which contains employee number, name, salary and designation. 1. Create a file 2. Display the records 3. Search for employee number 4. Search for salary 5. Exit Ans. import csv choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records.") print("3. Search for employee number.") print("4. Search for salary") print("5. Exit") k=int(input("Enter your choice: "))
  • 25. if k==1: f1=open("employee.csv",'w') c1=csv.writer(f1) L=['Employee no.', 'Name', 'Salary', 'Designation'] c1.writerow(L) choice='y' while choice in 'Yy': eno=int(input("Enter employee number : ")) name=input("Enter name : ") sal=float(input("Enter salary : ")) des=input("Enter designation : ") L=[eno, name, sal, des] c1.writerow(L) choice=input("Do you want to add more records ?(y/n)") f1.close() elif k==2: f2=open("employee.csv",'r') c1=csv.reader(f2) for rec in c1:
  • 26. if rec!=[]: print(rec) elif k==3: s=int(input("Enter employee number : ")) f3=open('employee.csv','r') c2=csv.reader(f3) next(c2) for row in c2: if row!=[] and int(row[0])==s: print(row) elif k==4: t=int(input("Enter salary : ")) L2=[] f4=open('employee.csv','r') c3=csv.reader(f4) next(c3) for rec in c3: if rec!=[] and float(rec[2])==t: print(rec) elif k==5: break
  • 27. choice=input("Do you want to continue? (y/n)")
  • 28. Q15. Write python code which create a 'voter.csv' file and using UDF, perform the following operations 1. Create 'voter.csv' file. (Voter ID, name, age, address) 2. Display the records of voters with age above 60. 3. Display records of voters who stay in Rajouri garden with age above 21. Ans. import csv def add(): f1=open("voter.csv",'w') c1=csv.writer(f1) L=['Voter ID', 'Name', 'Age', 'Address'] c1.writerow(L) choice='y' while choice in 'Yy': vid=int(input("Enter Voter ID : ")) name=input("Enter name : ") add=input("Enter address : ") age=int(input("Enter age : ")) L=[vid, name, age, add] c1.writerow(L) choice=input("Do you want to add more records ?(y/n)") f1.close() def search1(): f2=open("voter.csv",'r') c2=csv.reader(f2) next(c2) for rec in c2: if rec!=[] and int(rec[2])>60: print(rec) def search2(): f3=open('voter.csv','r') c3=csv.reader(f3)
  • 29. next(c3) for rec in c3: if rec!=[] and rec[3]=="Rajouri Garden" and int(rec[2])<21: print(rec) choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records of voters with age above 60.") print("3. Display records of voters who stay in Rajouri garden with age above 21.") print("4. Exit") k=int(input("Enter your choice: ")) if k==1: add() elif k==2: search1() elif k==3: search2() elif k==4: break choice=input("Do you want to continue? (y/n)") Output
  • 30.
  • 31. Q.16 write python code which uses UDF "count()" that takes one parameters filename and returns the count of lines in the file beginning with letter "T" Ans. fv=open('letsgo.txt','w') l=int(input("How many lines do you want in the file? ")) for i in range(0, l): t= input("Write the line you want to enter in the file: ") fv.write(t + "n") fv.close() def count(filename): with open(filename, 'r') as file: lines = file.readlines() count = 0 for line in lines: count += 1 return count filename = 'letsgo.txt' line_count = count(filename) print("The number of lines starting with 'T' in {filename} is:", line_count)
  • 32. Q.17 Write python code which uses function “count words( )” that reads the contents of the file "string.txt" and display the number of words from the file . If file is not created then the program creates the file Ans. def count_words(): ctr=0 f1=open("Story.txt",'r') data=f1.read() w=data.split() for i in w : ctr=ctr+1 print("No. of words are : ", ctr) count_words()
  • 33. Q18. A binary file 'grade.dat' is required to be created by inputting records from the user. Perform the following operations using UDF. (Admission number, Name, marks, grade) 1. Create the binary file 'great.dat' by inputting records from the user till user wishes 2. Display the records of the students who have secured more than 90 marks 3. Update the grade of the student whose name is input by the user from B grade to A grade. Display all the records. Ans. import pickle def add(): f1=open("grade.dat",'ab') rec=[] choice='y'
  • 34. while choice in 'Yy': add=int(input("Enter admission no. : ")) name=input("Enter name : ") mark=float(input("Enter marks : ")) gra=input("Enter grade : ") L=[add, name, mark, gra] rec.append(L) choice=input("Do you want to add more records ?(y/n)") pickle.dump(rec,f1) f1.close() def search(): f2=open("grade.dat",'rb') data=pickle.load(f2) for d in data: if d[2]>90: print(d) def update(): n=input("Enter the name of student : ") f3=open("grade.dat",'rb+') data=pickle.load(f3) for d in data: if d[1]==n: d[3]='A' f3.seek(0) pickle.dump(data,f3) f3.close() def display(): with open("grade.dat",'rb')as f4:
  • 35. data=pickle.load(f4) for rec in data: print(rec) choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records of students who have scored above 90 marks.") print("3. Update grade of students from B to A.") print("4. Display all records") k=int(input("Enter your choice: ")) if k==1: add() elif k==2: search() elif k==3: update() elif k==4: display() else: print("INVALID CHOICE") choice=input("Do you want to continue? (y/n)")
  • 36.
  • 37. Q19. Write python code which create a CSV file 'inventory.csv' containing item quote, item name, company, price and quantity. Use UDF to perform the following operations. 1. Create the CSV file (Till the user wish) 2. Display all the records of the CSV file. 3. Display the details of those items whose price is more than 40,000. Ans. import csv def add(): f1=open("inventory.csv",'a') c1=csv.writer(f1) L=['Item quote', 'Item name', 'Company', 'Price', 'Quantity'] c1.writerow(L) choice='y' while choice in 'Yy': iq=float(input("Enter Item quote: ")) name=input("Enter Item name : ") com=input("Enter company : ") pr=float(input("Enter price : ")) qu=int(input("Enter quantity : ")) L=[iq, name, com, pr, qu] c1.writerow(L) choice=input("Do you want to add more records ?(y/n)") f1.close() def display(): f2=open("inventory.csv",'r')
  • 38. c2=csv.reader(f2) next(c2) for rec in c2: if rec!=[]: print(rec) def search(): f3=open('inventory.csv','r') c3=csv.reader(f3) next(c3) for rec in c3: if rec!=[] and float(rec[3])>40000: print(rec) choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records.") print("3. Display details of those items whose price is above 40000.") k=int(input("Enter your choice: ")) if k==1: add() elif k==2: display() elif k==3: search() choice=input("Do you want to continue? (y/n)")
  • 39.
  • 40. Q20. Write python code which implement stack as a list containing details of a customer having customer name, phone no, and city. The menu driven program should provide the option 1. Push, 2. Pop, 3. Display. The program should continue till the user wishes, taking care of all the validations. Ans. stack=[] choice='y' while choice in 'Yy': print("1.Push") print("2.Pop") print("3.Display") print("4.Exit") ch=int(input("Enter you choice : ")) if ch==1: name=input("Enter you name : ") no=int(input("Enter phone no. : ")) city=input("Enter city : ") det=[name, no, city] stack.append(det) elif ch==2: if stack==[]: print("Stack underflow")
  • 41. else: stack.pop() elif ch==3: if len(stack)==0: print("Stack empty") else: for i in range(len(stack)-1,-1,-1): print(stack[i]) elif ch==4: break choice=input("Do you want to continue?(y/n)") OUTPUT.
  • 42. Q21. Write python code which creates a menu implementing user defined functions on CSV file. 1. Create a CSV file by writing 1 record at a time. 2. Write multiple records in one go. 3. Display the records of the CSV file. 4. Display the records of employ with salary input by the user. 5. Exit The CSV file contains employee no, name, designation, salary Ans. import csv def add(): f1=open("employees.csv",'w') c1=csv.writer(f1) L=['Employee no.', 'Name', 'Designation', 'Salary', 'Age'] c1.writerow(L) choice='y' while choice in 'Yy': eno=int(input("Enter employee number : ")) name=input("Enter name : ") sal=float(input("Enter salary : "))
  • 43. des=input("Enter designation : ") age=int(input("Enter age : ")) L=[eno, name, des, sal, age] c1.writerow(L) choice=input("Do you want to add more records ?(y/n)") f1.close() def display(): f2=open("employees.csv",'r') c2=csv.reader(f2) for rec in c2: if rec!=[]: print(rec) def search(): t=int(input("Enter salary : ")) f3=open('employees.csv','r') c3=csv.reader(f3) next(c3) for rec in c3: if rec!=[] and float(rec[3])==t: print(rec) choice='y' while choice in 'Yy': print("The options are: ") print("1. Enter new records.") print("2. Display records.") print("3. Search for salary") print("4. Exit") k=int(input("Enter your choice: "))
  • 44. if k==1: add() elif k==2: display() elif k==3: search() elif k==4: break choice=input("Do you want to continue? (y/n):") OUTput