0% found this document useful (0 votes)
8 views14 pages

Rec 9 To 19

Uploaded by

9310 GAUTAM V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Rec 9 To 19

Uploaded by

9310 GAUTAM V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

9.

def separate(filename):
obj1=open("sample.txt","r")
a=obj1.read()
b=a.replace(" ","#")
print(b)
obj1.close()
separate("sample.txt")

python#is#a#programming#language

10.
def count(lines):
u,l,v,c,d,s=0,0,0,0,0,0
for i in lines:
for j in i:
if j.isupper():
u+=1
elif j.islower():
l+=1
if j.lower() in 'aeiou':
v+=1
elif j in 'bcdfghjklmnpqrstvwxyz':
c+=1
elif j.isdigit():
d+=1
else:
s+=1
return(u,l,v,c,d,s)
def count_lines(s):
return len(s)
o=open("sample.txt",'r')
r=o.readlines()
(u,l,v,c,d,s)=count(r)
lines=count_lines(r)
print("No. of Upper Case: ",u)
print("No. of Lower Case: ",l)
print("No. of Vowels: ",v)
print("No. of Consonants: ",c)
print("No. of Digits: ",d)
print("No. of Special Characters: ",s)
print("No. of Lines: ",lines)
o.close()

No. of Upper Case: 7


No. of Lower Case: 20
No. of Vowels: 10
No. of Consonants: 12
No. of Digits: 0
No. of Special Characters: 13
No. of Lines: 3
11.
def write(filename):
with open("sample.txt","r") as obj1:
s=obj1.readlines()
with open("sample2.txt","w") as obj2:
for i in s:
if "a" not in i and "A" not in i:
obj2.write(i)
write("sample.txt")
12.
def capitalize_sentence(input_filename, output_filename):
with open("test_report.txt", "r") as obj1:
a=obj1.read()
b=a.split('.')
l=[]
for i in b:
stripped_sentence=i.strip()
if stripped_sentence:
l.append(stripped_sentence[0].upper() + stripped_sentence[1:])
else:
l.append('')

result = ".".join(l).strip()
if result:
result += '.'
with open("file.txt", "w") as obj2:
obj2.write(result)
capitalize_sentence("test_report.txt", "file.txt")
13.
import pickle
d={}
ch="y"
while ch=="y":
print("1.insert,2.update,3.search,4.delete")
choice=int(input("enter choice"))
if choice==1:
n=int(input("no of students:"))
for i in range(n):
usn=int(input("student usn:"))
name=input("enter student name:")
sclass=int(input("enter student class:"))
sec=input("enter student sec:")
mark=int(input("enter student mark:"))
d[usn]=[name,sclass,sec,mark]
with open("sample.dat","wb")as obj1:
pickle.dump(d,obj1)
elif choice==2:
with open("sample.dat","rb")as obj1:
a=pickle.load(obj1)
name=input("enter the name of the student who's mark need to be updated")
newmrk=int(input("enter mark:"))
for i in a:
if a[i][0]==name:
a[i][3]=newmrk
with open("sample.dat","wb") as obj1:
pickle.dump(a,obj1)
with open("sample.dat","rb") as obj1:
a=pickle.load(obj1)
print(a)
elif choice==3:
with open("sample.dat","rb")as obj1:
a=pickle.load(obj1)
searchusn=int(input("student usn:"))
for i in a:
if i==searchusn:
print(i,d[i])
elif choice==4:
with open("sample.dat","rb")as obj1:
a=pickle.load(obj1)
keys_to_delete = [i for i in a if a[i][3] < 13]
for i in keys_to_delete:
del a[i]
with open("sample.dat","wb")as obj1:
pickle.dump(a,obj1)
with open("sample.dat","rb") as obj1:
a=pickle.load(obj1)
print(a)
print("do u want to continue?,y=yes,n=no")
ch=input("choice:")

1.insert,2.update,3.search,4.delete
enter choice1
no of students:2
student usn:100900
enter student name:nan
enter student class:12
enter student sec:c
enter student mark:54
student usn:100901
enter student name:koi
enter student class:12
enter student sec:d
enter student mark:12
do u want to continue?,y=yes,n=no
choice:y
1.insert,2.update,3.search,4.delete
enter choice2
enter the name of the student who's mark need to be updatednan
enter mark:50
{100900: ['nan', 12, 'c', 50], 100901: ['koi', 12, 'd', 12]}
do u want to continue?,y=yes,n=no
choice:y
1.insert,2.update,3.search,4.delete
enter choice3
student usn:100901
100901 ['koi', 12, 'd', 12]
do u want to continue?,y=yes,n=no
choice:y
1.insert,2.update,3.search,4.delete
enter choice4
{100900: ['nan', 12, 'c', 50]}
do u want to continue?,y=yes,n=no
choice:n

14.
import pickle
def compute_total(r):
for i in r:
print('Total Salary of employee',i[0],'=',i[2]+i[3])
with open("empfile.dat","rb") as o:
a=pickle.load(o)
compute_total(a)
print('File size is: ',o.tell())

Total Salary of employee 101 = 55000


Total Salary of employee 102 = 67000
Total Salary of employee 103 = 61000
Total Salary of employee 104 = 73000
Total Salary of employee 105 = 69500
File size is: 96
15.
import csv
def read():
with open("info.csv","r")as obj2:
robj=csv.reader(obj2)
sum=0
for i in robj:
sum+=1
print("Number of records are=",sum)

def search():
with open("info.csv","r")as obj3:
robj=csv.reader(obj3)
s=int(input("enter the user_id="))
for i in robj:
if i[0]==s:
print("the required record is",i)
read()
search()

enter user id=1


enter user password=123
enter user id=2
enter user password=345
enter user id=3
enter user password=456
Number of records are= 3
enter the user_id=3
[3,456]

16.
import csv
l=[]
header=["item code","item name","item price","item quantity"]
obj1=open("groceries.csv","w",newline="")
wobj=csv.writer(obj1)
n=int(input("enter number of groceries"))
for i in range (n):
ic=int(input("item code="))
n=input("item name=")
p=int(input("item price="))
q=int(input("item quantity"))
l.append([ic,n,p,q])
wobj.writerow(header)
wobj.writerows(l)
obj1.close()
enter number of groceries3
item code=1
item name=apple
item price=50
item quantity2
item code=2
item name=pen
item price=30
item quantity3
item code=3
item name=pencil
item price=20
item quantity4

17.
stk=[]
def push_ele(x):
stk.append(x)
top=len(stk)-1
def pop_ele():
if len(stk)==0:
print('Underflow')
else:
stk.pop()
top=len(stk)-1
def display_stk():
if len(stk)==0:
print('Stack is empty')
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
d=eval(input('Enter the dictionary:'))
ch='yes'
while ch=='yes':
print('1.Push 2.Pop 3.Display')
x=int(input('Enter the respective number:'))
if x==1:
for i in d:
if d[i]>100:
push_ele([i,d[i]])
if x==2:
pop_ele()
if x==3:
display_stk()
ch=input('Do you want to continue? yes or no')

Enter the dictionary:{'Abhishek':100,'Kamal':200,'Pranav':150}


1.Push 2.Pop 3.Display
Enter the respective number:1
Do you want to continue? yes or noyes
1.Push 2.Pop 3.Display
Enter the respective number:2
Do you want to continue? yes or noyes
1.Push 2.Pop 3.Display
Enter the respective number:3
['Kamal', 200]
Do you want to continue? yes or no

18.
stk=[]
L=[]
def add_ele(x):
for i in x:
stk.append(i)
top=len(stk)-1
def del_ele():
if len(stk)==0:
print('Underflow')
else:
stk.pop()
top=len(stk)-1
def display_stk():
if len(stk)==0:
print('Stack is empty')
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
n=int(input('Enter the number of records:'))
for i in range(n):
hostelnum=int(input('Enter the hostel number:'))
stu=int(input('Enter the number of students:'))
hostelroom=int(input('Enter the number of rooms:'))
l=[int(hostelnum),int(stu),int(hostelroom)]
L.append(l)
ch='yes'
while ch=='yes':
print('1.Add record 2.Delete record 3.Display records')
x=int(input('Enter the respective number:'))
if x==1:
add_ele(L)
if x==2:
del_ele()
if x==3:
display_stk()
ch=input('Do you want to continue? yes or no')

Enter the number of records:3


Enter the hostel number:25
Enter the number of students:65
Enter the number of rooms:20
Enter the hostel number:35
Enter the number of students:65
Enter the number of rooms:22
Enter the hostel number:26
Enter the number of students:65
Enter the number of rooms:28
1.Add record 2.Delete record 3.Display records
Enter the respective number:1
Do you want to continue? yes or noyes
1.Add record 2.Delete record 3.Display records
Enter the respective number:2
Do you want to continue? yes or noyes
1.Add record 2.Delete record 3.Display records
Enter the respective number:3
[35, 65, 22]
[25, 65, 20]
Do you want to continue? yes or nono

19.
stk=[]
L=[]
def add_ele(x):
for i in x:
stk.append(i)
top=len(stk)-1
def del_ele():
if len(stk)==0:
print('Underflow')
else:
stk.pop()
top=len(stk)-1
def display_stk():
if len(stk)==0:
print('Stack is empty')
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
n=int(input('Enter the number of records:'))
for i in range(n):
pincode=int(input('Enter the pincode:'))
name=input('Enter the name of the city:')
l=[int(pincode),name]
L.append(l)
ch='yes'
while ch=='yes':
print('1.Add record 2.Delete record 3.Display records')
x=int(input('Enter the respective number:'))
if x==1:
add_ele(L)
if x==2:
del_ele()
if x==3:
display_stk()
ch=input('Do you want to continue? yes or no')

Enter the number of records:3


Enter the pincode:600125
Enter the name of the city:Rizzy Town
Enter the pincode:890678
Enter the name of the city:Downtown
Enter the pincode:389047
Enter the name of the city:Skibidi City
1.Add record 2.Delete record 3.Display records
Enter the respective number:1
Do you want to continue? yes or noyes
1.Add record 2.Delete record 3.Display records
Enter the respective number:2
Do you want to continue? yes or noyes
1.Add record 2.Delete record 3.Display records
Enter the respective number:3
[890678, 'Downtown ']
[600125, 'Rizzy Town']
Do you want to continue? yes or nono

You might also like