Professional Documents
Culture Documents
Cbse Class 12 Lab PRG
Cbse Class 12 Lab PRG
PART-A-PYTHON PROGRAMS
1. PROGRAM -1: Write the program to find the given string is
palindrome or not.
AIM:
To write the program to find the given string is palindrome or
not.
PROGRAM:
OUTPUT1:
Palindrome checking program
Enter the string : madam
The given string madam is a palindrome
Do you want to add check for another string(y/n):y
Enter the string : eleven
The given string eleven is a palindrome
Do you want to add check for another string(y/n):n
>>>
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To write a program to create a phone contact book and also to
do search contact.
3
PROGRAM:
pb={}
def addcontact():
ans='y'
while ans=='y':
na=input("Enter name:")
no=int(input("Enter mobile number:"))
nc=str(no)
if len(nc)==10:
pb[na]=no
print("Contact saved successfully")
else:
print("Invalid number.Enter correct number")
continue
ans=input("Do you want to add another contact(y/n):")
def searchcontact():
na=input("Enter the name which you want to search:")
if na in pb:
print("Mobile number :",pb[na])
else:
print("Name not in contact book")
while True:
print("Phone contact book")
print("1-to add contact\n2-to search contact\n3-to quit")
ch=int(input("Enter your choice:"))
if ch==1:
addcontact()
elif ch==2:
searchcontact()
elif ch==3:
break
else:
print("Invalid choice.Give correct choice")
continue
4
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To write a program to find the area of four shapes using module.
PROGRAM:
5
MODULE CODE:
MODULE NAME : area.PY
def square(a):
return a*a
def rectangle(l,b):
return l*b
def triangle(b,h):
return 0.5*b*h
def circle(r):
return 3.14*r*r
def cube(a):
return 6*(a**2)
MAIN PROGRAM :
import area
print("Area of the shapes calculating program")
while True:
print("1-square\t2-rectangle\t3-triangle\t4-circle\t5-cube\t6-
quit")
ch=int(input("Enter your choice:"))
if ch==1:
a=float(input("Enter the area value:"))
ar=area.square(a)
print("The area of the square is :",ar)
elif ch==2:
l=float(input("Enter the length value:"))
b=float(input("Enter the breadth value:"))
ar=area.rectangle(l,b)
print("The area of the rectangle is :",ar)
elif ch==3:
b=float(input("Enter the base value:"))
h=float(input("Enter the height value:"))
ar=area.triangle(b,h)
print("The area of the rectangle is :",ar)
elif ch==4:
a=float(input("Enter the radius value:"))
ar=area.circle(a)
print("The area of the circle is :",ar)
6
elif ch==5:
a=float(input("Enter the area value:"))
ar=area.cube(a)
print("The area of the cube is :",ar)
elif ch==6:
break
else:
print("Invalid choice")
continue
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
PROGRAM:
import random
print("Dice game ")
print("Game starts...")
ans='y'
while ans=='y':
print("Dice rolling ... ")
s=random.randint(1,6)
print("You got:",s)
ans=input("Do you want to roll again the dice (y/n):")
print("See you again.. Bye .. ")
OUTPUT:
Dice game
Game starts...
Dice rolling....
You got: 5
8
Do you want to roll again the dice (y/n):y
Dice rolling....
You got: 2
Do you want to roll again the dice (y/n):y
Dice rolling....
You got: 2
Do you want to roll again the dice (y/n):y
Dice rolling....
You got: 6
Do you want to roll again the dice (y/n):n
See you again.. Bye…
RESULT:
Thus the above program has been executed successfully and the
output is verified.
5. PROGRAM -5:Read a text file line by line and display each word
separated by a #.
AIM:
To read a text file line by line and display each word
separated by a #.
PROGRAM:
a=open('sample.txt','r')
b=a.readline()
while b:
c=b.split()
for i in range(len(c)-1):
print(c[i],'#',end='')
print(c[-1])
print()
b=a.readline()
a.close()
FILE CONTENT :
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.
PROGRAM:
a=open('sample.txt','r')
b=a.read()
v=c=uc=lc=0
for i in b:
if i.isalpha():
if i in ‘AEIOUaeiou’:
v+=1
else:
c+=1
if i.isupper():
uc+=1
elif i.islower():
lc+=1
print("The number of vowels in the file is :",v)
print("The number of consonants in the file is :",c)
print("The number of upper case letters in the file is :",uc)
print("The number of lower case letters in the file is :",lc)
a.close()
FILE CONTENT :
RESULT:
Thus the above program has been executed successfully and the
output is verified.
7. PROGRAM-7: Remove all the lines that contain the character 'a' in
a file and write it to another file.
AIM:
To remove all the lines that contain the character 'a' in a
file and write it to another file.
PROGRAM:
f=open('sample.txt', 'r')
a=f.readlines()
f.close()
f=open('sample.txt','w')
f1=open('sample1.txt','w')
for i in a:
if ('a' or 'A') in i:
f1.write(i)
else:
f.write(i)
f.close()
f1.close()
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To read a text file and count the number of occurrence of the
particular word in the file content.
PROGRAM:
a=open("sample.txt",'r')
b=a.read()
c=b.split()
d=0
w=input("Enter the word which you want to count:")
for i in c:
if i.lower()==w:
d+=1
if d==0:
print("The given word",w,"not found in the file content")
else:
print("The given word",w,"found",d,"time(s) in the file
content")
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
12
AIM:
To create the binary file which should contains the student
details and to search the particular student based on roll no and
display the details.
PROGRAM:
import pickle
def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
a.close()
print("Records stored successfully!.")
def searchrec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to search:"))
b=pickle.load(a)
for i in b:
if r==i[0]:
print("Roll no:",i[0],"\nName:",i[1],"\nMark:",i[2])
break
else:
print("Entered Roll no not found in the file")
a.close()
print("Student record search program")
addrec()
searchrec()
13
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
10. PROGRAM -10 Create the binary file which should contains the
student details and to update the particular student based on roll
no.
AIM:
To create the binary file which should contains the student
details and to update the particular student based on roll no.
PROGRAM:
import pickle
def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
14
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")
def updaterec():
a=open('student.bin','rb+')
r=int(input("Enter the roll no which you want to update:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
nm=float(input("Enter new mark:"))
i[2]=nm
break
else:
print("Entered Roll no not found in the file")
a.seek(0)
pickle.dump(l,a)
print("Record updated successfully")
print(l)
a.close()
print("Student record update program")
addrec()
updaterec()
OUTPUT:
11. PROGRAM -11 Create the binary file which should contains the
student details and to delete the particular student based on roll
no.
AIM:
To create the binary file which should contains the student
details and to delete the particular student based on roll no.
PROGRAM:
import pickle
def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")
def deleterec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to delete:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
l.remove(i)
break
else:
print("Entered Roll no not found in the file")
16
a.close()
a=open("student.bin",'wb')
pickle.dump(l,a)
print("Roll no:",r," Record deleted successfully")
a.close()
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
12. PROGRAM -12 Create the csv file which should contains the
employee details and to search the particular employee based on emp
no and display the details.
AIM:
To create the csv file which should contains the employee
details and to search the particular employee based on emp no and
display the details.
17
PROGRAM:
import csv
def addrec():
a= open('emp.csv','w',newline='')
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
def searchrec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
s=int(input('Enter the emp no which you want to search:'))
s=str(s)
for i in b:
l.append(i)
for i in l:
if i[0]==s:
print("Emp no:",i[0],'\nEmp name :',i[1],'\nEmp
salary:',i[2])
break
else:
print('no record found')
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
13. PROGRAM -13 Create the csv file which should contains the
employee details and to update their salary based on emp no .
AIM:
To create the csv file which should contains the employee
details and to update their salary based on emp no .
PROGRAM:
import csv
def addrec():
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
19
def updaterec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
for i in b:
l.append(i)
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
s=int(input('Enter the emp no which you want to update:'))
s=str(s)
for i in l:
if i[0]==s:
ns=int(input("Enter the revised salary:"))
i[2]=ns
b.writerows(l)
print("record updated")
break
else:
print('no record found')
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
20
14. PROGRAM -14 Create the csv file which should contains the
employee details and to delete the particular record based on emp
no .
AIM:
To create the csv file which should contains the employee
details and to delete the particular record based on emp no .
PROGRAM:
import csv
def addrec():
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
def deleterec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
for i in b:
l.append(i)
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
s=int(input('Enter the emp no which you want to delete:'))
s=str(s)
for i in l:
if i[0]==s:
l.remove(i)
b.writerows(l)
print("record deleted")
break
else:
print('no record found')
21
print("Employee details - delete record program")
addrec()
deleterec()
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To write a program to implement all stack operations using list
as stack.
PROGRAM:
def PUSH(stk):
a=input('Enter a city:')
stk.append(a)
def POP(stk):
if len(stk)==0:
print('STACK UNDERFLOW')
else:
c=stk.pop()
print('The popped city is:',c)
22
def DISPLAY(stk):
if len(stk)==0:
print('STACK EMPTY')
else:
print(stk[-1],'<--TOP')
for i in range(len(stk)-2,-1,-1):
print(stk[i])
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
24
1. Consider the following table and Write SQL commands and output.
Gcode Type Price QTY Readydate
10023 Pencil 1150 25 2010-12-
Skirt 19
10001 Formal 1250 15 2010-01-
Skirt 12
10012 Baby 750 20 2009-04-
Top 09
10009 Informa 1500 35 2012-12-
l Pant 20
10020 Frock 850 20 2012-01-
01
10089 Slacks 750 10 2010-10-
31
i) Create a table garment.
Ans. create table garment(Gcode int primary key, Type varchar(20), Price
int(5), QTY int(5), ReadyDate date);
ii) Display the description of the table.
Ans. desc garment;
iii) insert all the rows in the above given table.
Ans. insert into garment values(10023, ‘Pencil Skirt’,1150,25,’2010-12-
19’);
insert into garment values(10001, ‘Formal Skirt’,1250,15,’2010-
01-12’);
iv)Add column named City to the table Garment .
Ans. alter table garment add city varchar(20);
v)Display the information in the descending order of Gcode
Ans. select * from garment order by gcode desc;
vi) Delete all the information whose Gcode is 10023
Ans. delete from garment where gcode=10023;
vii) Select * from garments where price>1000;
Gcode Type Pri QTY Readydat
ce e
10023 Pencil 115 25 2010-12-
Skirt 0 19
10001 Formal 125 15 2010-01-
Skirt 0 12
10009 Informal 150 35 2012-12-
Pant 0 20
25 Write SQL commands and output.
2) Consider the following table and
4) Consider the following table and Write SQL commands and output.
i) Create a table empl. 27
Ans. create table empl(empno int(8) primary key, ename varchar(20), job
char(15), comm float(6,2), deptno int(4));
ii) Display the descriptions of employee table.
Ans. desc empl;
iii) To insert the rows given in above table.
Ans. insert into empl values(8369, ‘smith’,’clerk’,null,20);
insert into empl values(8499, ‘anya’,’salesman’,300.00,30);
5) Consider the following table and Write SQL commands and output.
AIM:
To write a python program to create the table and insert the
records into the table by getting the data from the user and to
store in the MySQL database.
PROGRAM :
import mysql.connector as my
a=my.connect(host='localhost',user='root',password=123456,database='MYSCHOOL')
if a.is_connected():
print('Database connected')
else:
print('Database not connected')
b=a.cursor()
q0='drop table if exists student'
b.execute(q0)
q="create table student(EXAMNO INT,NAME VARCHAR(20),CLASS
INT,SECTION CHAR(2),MARK INT)"
b.execute(q)
ans='y'
while ans.lower()=='y':
ex=int(input("ENTER EXAMNO:"))
na=input("ENTER NAME:")
cl=int(input("ENTER CLASS:"))
s=input("ENTER SECTION:")
m=int(input("ENTER MARK:"))
q1="insert into student values({},'{}',{},'{}',{})".format(ex,na,cl,s,m)
b.execute(q1)
print("Record successfully stored in the student table")
ans=input("Do you want to add another record(y/n):")
a.commit()
a.close()
30
OUTPUT:
Database connected
ENTER EXAMNO:101
ENTER NAME:SRINITHI
ENTER CLASS:12
ENTER SECTION:A1
ENTER MARK:77
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:102
ENTER NAME:RAMYA
ENTER CLASS:12
ENTER SECTION:A2
ENTER MARK:78
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:103
ENTER NAME:HEMA
ENTER CLASS:12
ENTER SECTION:A3
ENTER MARK:78
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:104
ENTER NAME:THARANIYA
ENTER CLASS:12
ENTER SECTION:A1
ENTER MARK:76
Record successfully stored in the student table
Do you want to add another record(y/n):N
>>>
RESULT:
Thus the above program has been executed successfully and the
output is verified.
PROGRAM :
import mysql.connector as my
a=my.connect(host='localhost',user='root',password=123456,database='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO SEARCH:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
for i in e:
print('EXAM NO:',i[0])
print('NAME :',i[1])
print('CLASS :',i[2])
print('SECTION :',i[3])
print('MARKS :',i[4])
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):')
a.close()
OUTPUT:
AIM:
To write a python program to update the student mark on table
based on the examno given by the user and if the record not found
display the appropriate message.
PROGRAM :
import mysql.connector as my
a=my.connect(host='localhost',user='root',password=13456,database='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO UPDATE:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
nm=int(input('ENTER THE NEW MARK:'))
q2='UPDATE STUDENT SET MARK={} WHERE EXAMNO={}'.format(nm,r)
b.execute(q2)
a.commit()
print('RECORD UPDATED SUCCESSFULLY WITH NEW MARK')
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):')
a.close()
33
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.
AIM:
To write a python program to delete the particular record from
the table based on the examno given by the user and if record not
found display the appropriate message.
PROGRAM :
import mysql.connector as my
a=my.connect(host='localhost',user='root',password=123456,database='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO DELETE:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
q2='DELETE FROM STUDENT WHERE EXAMNO={}'.format(r)
b.execute(q2)
a.commit()
34
print('RECORD DELETED SUCCESSFULLY')
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):')
a.close()
OUTPUT:
RESULT:
Thus the above program has been executed successfully and the
output is verified.