12 Cs Practical
12 Cs Practical
08-08-2022
1. All students should write all the following programs in the 200 pages one
sided ruled practical copy.
2. Program should be written in the ruled paper
3. Output should be written in the white paper
4. Index along with the chapter name and little description about the program
should be mentioned in the index . Date should be written in index .
5. First write complete question followed by the program.
6. Every program should have 1 or 2 comment statements
7. Proper indentation should be maintained.
8. Date and question should be written for the every program.
9. Every program should start in the new page.
10. Main function is mandatory for every program
11. Copy should be maintained properly. Handwriting should be neat and clean.
12. After term 1 10 more programs you have to write in the same copy. So keep
the copy safely
13. Copy should be covered with brown sheet.
st
14. The last date for the submission of the practical copy is 1 September, 2022.
1. write a program to define the functions find fact () to find out the factorial of an
accept the number using the concept of no return no parameter
fact()
OUTPUT
2 R.PADMA LAKSHMI
elif c==0:
print("not armstrong number")
OUTPUT
armstrong with parameter with return type
Find armstrong number
enter the no:153
in __main__
yes Armstrong number
3. write a program to define the function find_magic(no). To find out whether
accepted number is magic number or not. If the accepted number is magic
number than the function should return 1 else 0. In the main program if 1 is
returned the programmer has to print “yes magic no” else you have to print “not
magic number”.
3 R.PADMA LAKSHMI
magic no
4. Write a program to show the implementation of positional, keyword and default
arguments.
def find_si(p1,r,t1=2):
print("principle",p1)
print("rate",r)
print("time",t1)
return (p1*r*t1)/100
print("in",__name__)
p=int(input("enter principal amount"))
r=int(input("enter rate"))
t=int(input("enter time"))
si=find_si(p,r,t)#work
si=find_si(p,t1=5,r=4)#work
si=find_si(p,r=5)#work
si=find_si(p1=p,r=3)#work
si=find_si(p1=p,r=3,t1=3)#work
si=find_si(5000,3)#work
#si=find_si(r=2,p,t)#illegal key word before positional
#si=find_si(2000,p1=3000,2,4)#illegal
#si=find_si(2000,p1=3000,4)#illegal
#si=find_si(2000,4,p1=3000)#illegal
#si=find_si(r=4,t1=5)#wont work positional argument is missing
print("simple interest",si)
Output
in __main__
enter principal amount4000
enter rate2
enter time4
principle 4000
rate 2
time 4
principle 4000
rate 4
time 5
principle 4000
rate 5
4 R.PADMA LAKSHMI
time 2
principle 4000
rate 3
time 2
principle 4000
rate 3
time 3
principle 5000
rate 3
time 2
simple interest 300.0
5. Write a program to define the function manipulate(st) to manipulate the string
and find the biggest word of the string st and return the biggest word to the
function.
st=input("accept the string")
def manipulate(s):
k=0
t=""
st1=t
l=0
for i in s:
if i!=" ":
l=l+1
t=t+i
elif l>=k:
k=l
l=0
st1=t
t=""
else:
l=0
t=""
if l>=k:
print("print the biggest word along with the length",l,t)
else:
print("print the biggest word along with the length",k,st1)
5 R.PADMA LAKSHMI
print("in",__name__)
manipulate(st)
Output
accept the stringthis excellent work should be appreciated.
in __main__
print the biggest word along with the length 12 appreciated.
6. Write a program to implement the concept of local and global variable. Both
local and global should carry the same name. Declare one more global variable
and attempt to change it and record your observations. If any error is there try
to avoid that error using the concept of global key word
a=50
l=550
a=a+l
def check(k,b):
l=500
global a
if b>=1000:
b-=80
k+=l
l-=20
print(a,l,b,k,sep=" ")
print(l)
else:
b+=90
k-=l
l+=120
print(b,a,l,k,sep=" ")
a+=100
a+=l
print(a)
print(a)
a+=700
check(a,l)
print(a,l,sep=" ")# global not suggested to use
6 R.PADMA LAKSHMI
check(l,a)
print(l,a,sep=" ")
Output
600
640 1300 620 800
2020
2020 550
2020 480 1940 1050
480
2600
550 2600
7. Write a menu driven program with the following options for reading the file.
The menu is as follows:
print("1read_n ")
print("2 read_whole_file")
print("3use_ readline")
print("4 use_ readlines")
print(“5 exit”)
def read_bytes():
f=open("series1.txt","r")
n=int(input("how many bytes you want to read"))
data=f.read(n)
print(data)
for i in data:
print(i,end=" ")
f.close()
def read_wholefile():
f=open("series1.txt","r")
data=f.read()
print(data,end=" ")
7 R.PADMA LAKSHMI
f.close()
def read_line():
f=open("series1.txt","r")
data=f.readline()
print(data)
data=f.readline()
print(data)
data=f.readline(3)
print(data)
data=f.readline(5)
print(data)
f.close()
def read_lines():
f=open("series1.txt")
data=f.readlines()
print(data)
8 R.PADMA LAKSHMI
if choice==1:
read_bytes()
if choice==2:
read_wholefile()
if choice==3:
read_line()
if choice==4:
read_lines()
if choice==5:
print("no more operations")
print("thankyou")
break
OUTPUT
menu for creation of file and doing operations
do you wish to carry on with file manipulation
menu
1 read_n
2 read_whole_file
3 readline
4 readlines
5 EXIT
enter the choice1
how many bytes you want to read5
Softw
S o f t w do you wish to carry on with file manipulation
menu
1 read_n
2 read_whole_file
3 readline
4 readlines
5 EXIT
enter the choice4
['Software is a collection of instructions and data that tell a computer how to work. \n',
'This is in contrast to hardware, from which the system is built and actually performs
the work.\n', ' software is all information processed by computer systems, including
9 R.PADMA LAKSHMI
programs and data.\n']
Software is a collection of instructions and data that tell a computer how to work.
This is in contrast to hardware, from which the system is built and actually performs
the work.
software is all information processed by computer systems, including programs and
data.
do you wish to carry on with file manipulation
menu
1 read_n
2 read_whole_file
3 readline
4 readlines
5 EXIT
8. Write a program to open the file “series1.txt”
And do the following operations using menu driven program. The menu is as
follows:
10 R.PADMA LAKSHMI
def four_letterword():
f=open("series1.txt","r")
st=f.readlines()
print(st)
count=0
for data in st:
data=data.strip()
data=data.split()
for i in data:
if len(i)==4:
print(i)
count=count+1
print("number of 4 letter words",count)
f.close()
def count_is():
f=open("series1.txt","r")
st=f.readlines()
print(st)
count=0
for data in st:
data=data.strip()
print(data)
data=data.split()
print(data)
for i in data:
if i=="is":
print(i)
count=count+1
print("number of is words are ",count)
f.close()
def move_pointer():
print("showing the working of seek() functions ")
f=open("series1.txt","rb")
f.seek(0,0)
11 R.PADMA LAKSHMI
s=f.read(1)
print(s)
print("reading entire file and printing the length")
st=f.readline()
st=f.readline()
st=f.readlines()
print(st)
print(st)
c=len(st)
print("lenght of the string",c)
f.seek(2)
c=f.read(3)
print(c)
tn=f.tell()
print("The position of the pointer",tn)
print("moving the pointer from the current position")
print(f.seek(5,1))
print("Trapping seek")
print(c)
c=f.read(2)
print(c)
tn=f.tell()
print("The position of the pointer",tn)
12 R.PADMA LAKSHMI
choice=0
print("menu for creation of file and doing operations")
while choice<=5:
print("do you wish to carry on with file manipulation ")
print("menu")
print("1 words_i ")
print("2 count_is")
print("3 four letterword")
print("4 move_pointer")
print("5 EXIT")
choice=int(input("enter the choice"))
if choice==1:
start_i()
if choice==2:
four_letterword()
if choice==3:
count_is()
if choice==4:
move_pointer()
if choice==5:
print("no more operations")
print("thankyou")
break
Output
13 R.PADMA LAKSHMI
4 move_pointer
5 EXIT
enter the choice1
['Software is a collection of instructions and data that tell a computer how to work. \n',
'This is in contrast to hardware, from which the system is built and actually performs
the work.\n', ' software is all information processed by computer systems, including
programs and data.\n']
is
instructions
is
in
is
is
information
including
number of words which start with i are 8
do you wish to carry on with file manipulation
menu
1 words_i
2 count_is
3 four letterword
4 move_pointer
5 EXIT
enter the choice4
showing the working of seek() functions
b'S'
reading entire file and printing the length
[b' software is all information processed by computer systems, including programs
and data.\r\n']
[b' software is all information processed by computer systems, including programs
and data.\r\n']
lenght of the string 1
b'ftw'
The position of the pointer 5
moving the pointer from the current position
10
Trapping seek
14 R.PADMA LAKSHMI
b'ftw'
b's '
The position of the pointer 12
moving the pointer from the end
b'ata.'
The position of the pointer 271
do you wish to carry on with file manipulation
menu
1 words_i
2 count_is
3 four letterword
4 move_pointer
5 EXIT
enter the choice5
no more operations
thankyou
9. Write a menu program to create the binary file cloth.dat with cloth id, cloth
name, cloth price, cloth quantity, and write the list object into to the file and
do the following operations.
1. Writefile()
2. Readfile()
3. Searchrecord()
4. Countrecords()
5. Exit
import pickle
import sys
import os
def writefile_list():
fw=open("cloth.dat","wb")
n=int(input("how many records"))
15 R.PADMA LAKSHMI
cid=int(input("enter the cloth id"))
cnm=(input("enter fabric"))
cprice=float(input("accept price"))
l.extend([cid,cnm,cprice])
pickle.dump(l,fw)
fw.close()
def readfile_list():
f=open("cloth.dat","rb")
while True:
try:
r=pickle.load(f)
print(r)
except EOFError:
sys.stderr.write("end of the file reached")
break
f.close()
def countrecord():
count=0
f=open("cloth.dat","rb")
while True:
try:
r=pickle.load(f)
count=count+1
except EOFError:
break
f.close()
print("no of records",count)
def searchrecord():
id=int(input("enter the cloth id to be searched"))
count=0
16 R.PADMA LAKSHMI
with open("cloth.dat","rb")as f:
while True:
try:
r=pickle.load(f)
for a in r:
if a==id:
print(a)
count=count+1
print(r)
except EOFError:
break
if count==0:
print("not found")
reply='y'
while reply=='Y' or reply=='y':
print("menu")
print("1.write binaryfile")
print("2 read binaryfile")
if ch==1:
writefile_list()
elif ch==2:
readfile_list()
elif ch==3:
searchrecord()
elif ch==4:
countrecord()
17 R.PADMA LAKSHMI
reply=input("wish to continue y-for yes n for no")
OUTPUT
menu
1.write binaryfile
2 read binaryfile
3 search record
4 count record
enter the choice2
[100, 'wool', 189.0]
[200, 'uvw', 800.85]
[400, 'silk', 800.0]
[600, 'pattu', 625.5]
end of the file reachedmenu
1.write binaryfile
2 read binaryfile
3 search record
4 count record
enter the choice3
enter the cloth id to be searched300
not found
menu
1.write binaryfile
2 read binaryfile
3 search record
4 count record
enter the choice3
enter the cloth id to be searched400
400
[400, 'silk', 800.0]
menu
1.write binaryfile
2 read binaryfile
3 search record
4 count record
enter the choice4
no of records 4
18 R.PADMA LAKSHMI
menu
1.write binaryfile
2 read binaryfile
3 search record
4 count record
10. Write a menu program to create the binary file book.dat with dictionary object
which contains bid,bnm,bprice,bquantity and do the following operations.
1. Writefile
2. Readfile
3. Searchrecord()
4. Delete record()
5. updaterecord()
6. Countrecord()
7. Exit
import pickle
import os,sys
bd={}
def writefile_dict():
ans='y'
fw=open("book.dat","wb")
while ans=='Y' or ans=='y':
bd={}
bno=int(input("enter Bid no:"))
bnm=input("accept book name")
bquan=int(input("accept quantity"))
bprice=float(input("accept book price"))
bd['no']=bno
bd['name']=bnm
bd["quantity"]=bquan
bd['price']=bprice
pickle.dump(bd,fw)
ans=input("do you wish to continue if you wish accept small or capital y else
accept anykey")
fw.close()
19 R.PADMA LAKSHMI
def readfile_dict():
with open("book.dat","rb")as f:
while True:
try:
r=pickle.load(f)
print(r)
print(type(r))
except Exception as e:
print(e)
break
def searchrecord():
id=int(input("whose bid to be searched and printed"))
count=0
with open("book.dat","rb")as f:
while True:
try:
r=pickle.load(f)
#for a in r.values():
for a in r.values():
print(type(a))
if a==id:
print(a)
print(r)
count=count+1
except EOFError:
break
if count==0:
print("not found")
20 R.PADMA LAKSHMI
def deleterecord():
id=int(input("whose bid to be searched and deleted"))
# bnm=input("enter the bookname to be searched")
f1=open("temp.dat","wb")
count=0
with open("book.dat","rb")as f:
while True:
count=0
try:
r=pickle.load(f)
for a in r.values():
if a==id:
print(a)
print("record deleted")
count=count+1
if count==0:
pickle.dump(r,f1)
except EOFError:
break
f1.close()
os.remove("book.dat")
os.rename ("temp.dat","book.dat")
def countrecords():
nor=0
with open("book.dat","rb")as f:
while True:
try:
r=pickle.load(f)
print(r)
nor=nor+1
print(type(r))
except EOFError:
break
print("no of records are",nor)
f.close()
21 R.PADMA LAKSHMI
def updaterecord():
no=int(input("enter which book id's details to be updated"))
count=0
with open("book.dat",'rb+') as f:
while True:
try:
p=f.tell()
print(p)
r=pickle.load(f)
for a in r.values():
if a==no:
count=count+1
nm=input("enter book name")
price=float(input("enter price"))
r["name"]=nm
r["price"]=price
f.seek(p)
pickle.dump(r,f)
except EOFError:
break
if count==0:
print("no record")
else:
print("record updated")
f.close()
reply='y'
22 R.PADMA LAKSHMI
print("6 delete record")
ch=int(input("enter the choice"))
if ch==1:
writefile_dict()
elif ch==2:
readfile_dict()
elif ch==3:
searchrecord()
elif ch==4:
countrecords()
elif ch==5:
updaterecord()
elif ch==6:
deleterecord()
reply=input("wish to continue y-for yes n for no")
OUTPUT
menu
1.write binaryfile
23 R.PADMA LAKSHMI
2 read binaryfile
3 searchrecord
4 count records
5 update record
6 delete record
enter the choice2
{'no': 1005, 'name': 'wings of fire', 'quantity': 20, 'price': 200.95}
<class 'dict'>
{'no': 1001, 'name': 'school', 'quantity': 39, 'price': 135.98}
<class 'dict'>
Ran out of input
menu
1.write binaryfile
2 read binaryfile
3 searchrecord
4 count records
5 update record
6 delete record
enter the choice3
whose bid to be searched and printed1001
<class 'int'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'int'>
1001
{'no': 1001, 'name': 'school', 'quantity': 39, 'price': 135.98}
<class 'str'>
<class 'int'>
<class 'float'>
menu
1.write binaryfile
2 read binaryfile
3 searchrecord
4 count records
5 update record
6 delete record
24 R.PADMA LAKSHMI
enter the choice4
{'no': 1005, 'name': 'wings of fire', 'quantity': 20, 'price': 200.95}
<class 'dict'>
{'no': 1001, 'name': 'school', 'quantity': 39, 'price': 135.98}
<class 'dict'>
no of records are 2
menu
1.write binaryfile
2 read binaryfile
3 searchrecord
4 count records
5 update record
6 delete record
enter the choice
11. Write a menu driven to print the following the menu and do the following
operationsm on csv file
1 write csvfile
2 read csvfile
3 count csvfile
4 search record
5 delete record
6 update record
7 sort record
8 exit the screen
import csv
import os
fields = ['id','item name','item price','item quantity']
print("this is to create csv file")
fn="ITEM.csv"
def writefile_csv():
item=[]
csvfilew = open(fn,'w')
25 R.PADMA LAKSHMI
csvw=csv.writer(csvfilew,lineterminator='\n')
#csvw=csv.writer(csvfilew)
csvw.writerow(fields)
ans='yes'
while ans=='yes' or ans=='YES':
id=input("enter item id")
iname=input("enter item name")
iprice=float(input("enter price"))
iquantity=int(input("enter quantity"))
item.extend([id,iname,iprice,iquantity])
#print(item)
csvw.writerow(item)
item=[]
ans=input("do you wish to continue type yes-for yes n - for no")
csvfilew.close()
def readfile_csv():
print("fetch the data and print")
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
for r in csvr:
print(r)
#csvfiler.close()
def count_csv():
c=-1
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
for r in csvr:
print(r)
c=c+1
print("number of records",c)
def delete_record():
csvfilew = open("newitem.csv",'w',newline="")
26 R.PADMA LAKSHMI
csvw=csv.writer(csvfilew)
#csvw.writerow(fields)
id=input("enter the id")
k=0
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
if k==0:
print("no matching record found could not delete anything")
csvfilew.close()
csvfiler.close()
os.remove("ITEM.csv")
os.rename("newitem.csv","ITEM.csv")
def sort():
item=[]
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
i=0
for row in csvr:
if i>0:
item.append(row)
i=i+1
print("fetch and print")
print(item)
for i in range(0,len(item)):
for j in range(0,len(item)-1-i):
if (int)(item [j][0])>(int)(item[j+1][0]):
27 R.PADMA LAKSHMI
item[j],item[j+1]=item[j+1],item[j]
print("after sorting")
print(item)
#fields = ['id','item name','item price','item quantity']
csvfilew1 = open("newit.csv",'w')
csvw=csv.writer(csvfilew1,lineterminator='\n')
csvw.writerow(fields)
csvw.writerows(item)
csvfilew1.close()
csvfiler.close()
os.remove("ITEM.csv")
os.rename("newit.csv","ITEM.csv")
def update_record():
#fields = ['id','item name','item price','item quantity']
item=[]
id=input("accept id to be updated")
csvfilew = open("newu.csv",'w',newline="")
csvw=csv.writer(csvfilew)
28 R.PADMA LAKSHMI
csvfilew.close()
os.remove("ITEM.csv")
os.rename("newu.csv","ITEM.csv")
def search_record():
id=input("enter the id which you want search")
k=0
with open(fn,'r',newline='\r\n')as csvfiler:
csvr=csv.reader(csvfiler)
print(csvr)
for row in csvr:
if id==row[0]:
k=k+1
print("yes found")
print(row)
break
if k==0:
print("no matching record")
#csvfiler.close()
reply='yes'
while reply=="YES" or reply=="yes":
print("menu")
print("1 write csvfile")
print("2 read csvfile")
print("3 count csvfile")
print("4 search record")
print("5 delete record")
print("6 update record")
print("7 sort record")
print("8 exit the screen")
29 R.PADMA LAKSHMI
ch=int(input("accept the choice"))
if ch==1:
writefile_csv()
elif ch==2:
readfile_csv()
elif ch==3:
count_csv()
elif ch==4:
search_record()
elif ch==5:
delete_record()
elif ch==6:
update_record()
elif ch==7:
sort()
elif ch==8:
print("thank you")
break
else:
continue;
OUTPUT
30 R.PADMA LAKSHMI
3 count csvfile
4 search record
5 delete record
6 update record
7 sort record
8 exit the screen
accept the choice2
fetch the data and print
['1001', 'nuts', '20.0', '20']
['1000', 'bolts', '30.0', '30']
do you wish to continue type yes for continueyes
menu
1 write csvfile
2 read csvfile
3 count csvfile
4 search record
5 delete record
6 update record
7 sort record
8 exit the screen
menu
1 write csvfile
2 read csvfile
3 count csvfile
4 search record
5 delete record
6 update record
7 sort record
8 exit the screen
accept the choice4
enter the id which you want search1000
<_csv.reader object at 0x00000199E1005520>
yes found
['1000', 'bolts', '30.0', '30']
do you wish to continue type yes for continue
12. Write a menu driven program to show how text file can be opened in different
31 R.PADMA LAKSHMI
modes
def writemode():
f=open(r"f:\..\..\modes1.txt","w")
n=int(input("how many strings"))
for i in range(n):
st=input("accept")
f.write(st+'\n')
f.close()
def readmode():
f=open("modes.txt","r")
st=f.readlines()
print(st)
for i in st:
i=i.strip()
print(i)
f.close()
def appendmode():
f=open("modes.txt","a")
st="all modes need to understood thoroughly"
f.write(st)
print("appended successfully")
f.close()
def writeplusmode():
f=open("modes.txt","w+")
n=int(input("how many strings"))
for i in range(n):
st=input("accept")
f.write(st+'\n')
f.seek(0)
32 R.PADMA LAKSHMI
st=f.read()
print(st)
f.close()
def readplusmode():
f=open("modes.txt","r+")
st=f.read()
print(st)
f.write("is it possible")
f.seek(0)
st=f.read()
print(st)
f.close()
def appendplusmode():
f=open("modes.txt","a+")
st="this is about all 6 modes of text file"
f.write(st+'\n')
f.seek(0)
st=f.read()
print(st)
f.close()
def thankyou():
print("thank you")
ans=True
while (ans):
print(“1.writemode”)
print(“2. Readmode”)
print(“3 appendmode”)
33 R.PADMA LAKSHMI
print(“4. writeplusmode”)
print(“5. readplusmode”)
print(“6. Appemdplusmode”)
print(“7 exit mode”)
choice=int(input("Accept choice"))
if choice==1:
writemode()
if choice==2:
readmode()
if choice==3:
appendmode()
if choice==4:
writeplusmode()
if choice==5:
readplusmode()
if choice==6:
appendplusmode()
if choice==7:
thankyou()
ans=False
OUTPUT:-
1 write mode-w
2 read mode()
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
this is about all 6 modes of text file
this is about all 6 modes of text file
this is about all 6 modes of text file
34 R.PADMA LAKSHMI
1 write mode-w
2 read mode()
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice
1 write mode-w
2 read mode()
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice2
['slow and steady wins the race\n', 'better late than never\n']
slow and steady wins the race
better late than never
1 write mode-w
2 read mode()
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
1 write mode-w
2 read mode()
35 R.PADMA LAKSHMI
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
this is about all 6 modes of text file
1 write mode-w
2 read mode()
3 append mode-a
4 write mode-w+)
5 read mode-r+
6 append mode a+
7 exit mode
Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
this is about all 6 modes of text file
this is about all 6 modes of text file
36 R.PADMA LAKSHMI