Professional Documents
Culture Documents
CLASS : 12-A
SESSION : 2022-2023
Q1. WAP to search an element in a list and display the frequency of element
present in list and
PROGRAM:
def function():
dictionary = {}
b = dictionary.keys()
if word in dictionary:
dictionary[word]+=1
else:
dictionary[word] = 1
print(dictionary)
elif choice == 2:
for i in range(len(l1)):
if l1[i] == a:
function()
OUTPUT:
Choice: 1
Q2. WAP to take input for 3 numbers, check and print the largest number using
function.
PROGRAM:
def maximum_value(b,c,d):
print(max(b,c,d))
maximum_value(6,7,8)
OUTPUT:
8
4
Q3.WAP to pass list to a function and double the odd values and half even
values
PROGRAM:
def change(A):
for i in range(len(A)):
if A[i]%2 == 0:
A[i] =
int(A[i]/2)
else:
A[i] = A[i]*2
Print()
change([1,6,8,5])
OUTPUT:
[2, 3, 4, 10]
5
PROGRAM:
def count(P):
even = 0
odd = 0
for i in range(len(P)):
if P[i]%2 == 0:
even+=1
else:
odd +=1
count((2,4,5,7,10))
OUTPUT:
PROGRAM:
def algebra(B,C):
if FA== 1:
print("Sum = ",B+C)
elif FA== 2:
print("Product = ",B*c)
elif FA== 3:
print("Product = ",B-C)
algebra(7,4)
OUTPUT:
1. Sum
2. Product
3. Difference
Choice: 1
Sum = 11
7
Q6. WAP to pass a string to a function and count how many vowels
PROGRAM:
def count_vowels(str):
count = 0
for i in str:
if i in ['a','e','i','o','u','A','E','I','O','U']:
count+=1
print(count)
16
PROGRAM:
def generator():
import random
generator()
OUTPUT:
PROGRAM:
def analytics(str):
if D== 1:
count = 0
for i in range(len(str)):
if str[i].isdigit():
count+=1
count = 0
for i in range(len(str)):
if str[i].isupper():
count+=1
elif D== 3:
count= 0
for i in range(len(str)):
if str[i].islower():
count+=1
elif D== 4:
count= 0
for i in range(len(str)):
if str[i].isspace():
count+=1
elif D== 5:
count= 0
for i in range(len(str)):
if str[i].isalpha():
continue
elif str[i].isdigit():
10
continue
elif str[i].isspace():
continue
else:
count+=1
elif D== 6:
count_uc= 0
count_lc= 0
count_digit= 0
count_space= 0
count_sp= 0
for i in range(len(str)):
if str[i].isupper():
count_uc+=1
elif str[i].islower():
count_lc+=1
elif str[i].isdigit():
count_digi+=1
elif str[i].isspace():
count_space+=1
else:
count_sp+=1
OUTPUT:
1. No. of digits
2. No. of uppercase characters
4. No. of spaces
6. Calculate everything
Choice: 2
11
PROGRAM:
def function(str):
M = str.split()
for i in range(len(M)):
M[i] = M[i].capitalize()
for i in M:
fo_str+=i
print(' '.join(M))
OUTPUT:
PROGRAM:
fw=open("story.txt",'w')
fw.close()
fr=open("story.txt", 'r')
count= 0
while str!='':
str = fr.readline()
P= str.split()
for i in P:
count+=1
print(i)
OUTPUT:
AM
and
also
13
PROGRAM:
fw=open("killer.txt",'w')
fw.close()
str= fr.read()
OUTPUT:
Occurance of this: 1
Occurance of that: 1
14
PROGRAM:
count= 0
str= fo.readlines()
for i in str:
if i[0] in ['a','e','i','o','u','A','E','I','O','U']:
print(i)
count+=1
print(count)
OUTPUT:
15
Q13. WAP to read characters from keyboard one by one, all lower case
letters
gets stored inside a file “LOWER”, all uppercase letters gets stored
inside a
file “UPPER” ,and all other characters get stored inside “OTHERS”.
PROGRAM:
A= open("upper.txt", 'w')
B= open("lower.txt", 'w')
C= open("other.txt", 'w')
while True:
if K=="quit" or K=="Quit":
break
elif K.isupper():
elif K.islower():
else:
A.close()
B.close()
C.close()
OUTPUT:
Q14. WAP to create a binary file with name and roll number. Search for
a given
PROGRAM:
import pickle
student=[]
f=open("student.dat",'wb')
P='Y'
while P.lower()=='y':
name=input("Enter Name:")
student.append([roll,name])
P=input("Add More?(Y):")
pickle.dump(student,f)
f.close()
fo=open("student.dat",'rb')
student=[]
while True:
try:
student=pickle.load(fo)
except EOFError:
break
p='Y'
while p.lower()=='y':
found=False
for s in student:
if s[0]==r:
print("Name is:",s[1])
found=True
break
17
if not found:
P=input("Search more?(Y):")
break
fo.close()
OUTPUT:
Enter Name:KK
Add More?(Y):Y
Enter Name:MM
Add More?(Y):N
Name is: KK
Search more?(Y):N
18
Q15. WAP to create a binary file with roll number, name and marks,
input
PROGRAM:
import pickle
student=[]
fa=open("student.dat",'wb')
D='Y'
while D.lower()=='y':
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
student.append([roll,name,marks])
D=input("Add More?(Y):")
pickle.dump(student,fa)
fa.close()
fm=open("student.dat",'rb+')
student=[]
while True:
try:
student=pickle.load(fm)
except EOFError:
break
D='Y'
while D.lower()=='y':
found=False
for s in student:
if s[0]==o:
print("Name is:",s[1])
print("current marks is:",s[2])
s[2]=g
19
print("Record Upadated")
found=True
break
if not found:
D=input("Update more?(Y):")
break
fm.close()
OUTPUT:
Enter Name:AA
Enter Marks:90
Add More?(Y):Y
Enter Name:QQ
Enter Marks:99
Add More?(Y):N
Name is: QQ
Record Upadated
Update more?(Y):N
20
PROGRAM:
import csv
def write():
f=open("file.csv",'w',newline='')
wo=csv.writer(f)
wo.writerow(['empid','name','mobileno.'])
while true:
eid=int(input("enter empid:"))
mobno=int(input("enter mobileno.:"))
data=[eid,name,mobno]
wo.writerow(data)
if ch in 'Nn':
break
f.close()
def read():
f=open("file.csv",'r')
ro=csv.reder(fr)
for i in ro:
print(i)
def search():
f=open("file.csv",'r',newline='')
found=0
for rec in r:
if rec[0]==eid:
print(".....Record found......")
print(rec)
found=1
break
if found==0:
print(".......Record not
found......")
f.close()
def update():
f=open("file.csv",'r',newline='')
21
found=0
r=csv.reader(f)
nrec=[]
for rec in r:
if rec[0]==eid:
print("....current record....")
print(".....updated record.....",rec)
found=1
nrec.append(rec)
if found==0:
f.close()
else:
f=open('file.csv','w',newline=='')
w=csv.writer(f)
w.writerows(nrec)
f.close()
write()
read()
search()
update()
OUTPUT:
enter empid:AABB@SDV
enter mobileno.:9911754784
enter empid:BBAA@SDV
enter mobileno.:9814754581
['empid','name','mobileno.']
....current record....
enter name to updateDD
22
operation), removing element (POP operation) and displaying the stack elements
(Traversal operation)
using lists.
PROGRAM:
s = []
top = None
def s_push(stk):
while True:
stk.append(item)
top = len(stk)-1
if redo in 'nN':
break
def s_pop(stk):
if len(stk) == 0:
print("Underflow")
else:
i = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk)
top = top-1
print(i)
def display(stk):
if len(stk) == 0:
print('Stack is empty')
else:
top = len(stk) - 1
print(stk[i])
while True:
if choices == 1:
s_push(s)
elif choices == 2:
23
s_pop(s)
elif choices == 3:
display(s)
elif choices == 4:
break
else:
print("Invalid Input!!")
OUTPUT:
1. Push
2. Pop
4. Exit
Choice: 1
1. Push
2. Pop
4. Exit
Choice: 3
2 #Top Element
1. Push
2. Pop
4. Exit
Choice: 4
24
PROGRAM:
data = []
top = None
def s_push(stk):
while True:
stk.append([bookcode, booktitle,price])
top = len(stk)-1
if redo in 'nN':
break
def display(stk):
if len(stk) == 0:
print('Stack is empty')
else:
top = len(stk) - 1
print(stk[top])
print(stk[i])
def search(stk):
for i in range(len(stk)):
if stk[i][0] == code:
def update(stk):
code = int(input("Enter the book code: "))
if a == 1:
for i in range(len(stk)):
25
if stk[i][0] == code:
stk[i][0] = code_new
print("Code Updated!!")
elif a == 2:
for i in range(len(stk)):
if stk[i][0] == code:
stk[i][1] = name_new
print("Name Updated!!")
elif a == 3:
for i in range(len(stk)):
if stk[i][0] == code:
stk[i][2] = price_new
print("Price Updated!!")
while True:
\n\t3. Update data \n\t4. View the library \n\t5. Exit\nChoice: "))
if choices == 1:
s_push(data)
elif choices == 2:
search(data)
elif choices == 3:
update(data)
elif choices == 4:
display(data)
elif choices == 5:
break
else:
print("Invalid Input!!")
OUTPUT:
2. Search
3. Update data
5. Exit
26
Choice: 1
2. Search
3. Update data
5. Exit
Choice: 2
2. Search
3. Update data
5. Exit
Choice: 5
27
PROGRAM:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="web
@1234",database="school")
mycursor=mydb.cursor()
int(8));"")
for x in mycursor:
print(x)
OUTPUT:
('food',)
('information_schema',)
('mysql',)
('order1',)
('performance_schema',)
('piyush14',)
('sakila',)
('school',)
('sys',)
28
('world',)
Q20. Write a python code to insert data into table emp using python-mysql
connectivity
PROGRAM:
import mysql.connector
cur = mycon.cursor()
while True:
cur.execute(sql, vals)
mycon.commit()
print('Data added!!')
if c in 'nN':
break
OUTPUT:
29
Data added!!
Data added!!
Q21. Write a code to fetch record from table emp using python-mysql
connectivity.
PROGRAM:
import mysql.connector
mycon = mysql.connector.connect(host =
'localhost', user
cur = mycon.cursor()
cur.execute(r1)
d = cur.fetchall()
for k in d:
print(k)
OUTPUT:
31
(a) To show book name, Author name and price of books of First Pub. Publisher .
(c) To Display the names and price from books in ascending order of their prices.
(e) To display the Book_Id, Book_name and quantity issued for all books which have
been issued.
32
(iv) To display the maximum salary among managers and clerks from the
table Empsalary.