Cbse Xii - Lab Practical Programs 2023
Cbse Xii - Lab Practical Programs 2023
Cbse Xii - Lab Practical Programs 2023
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.
3
PROGRAM:
OUTPUT1:
OUTPUT2:
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,update and delete contact.
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:
4
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")
def updatecontact():
na=input("Enter the name which you want to update:")
if na in pb :
no=int(input("Enter the new number"))
nc=str(no)
if len(nc)==10:
pb[na]=no
print("Contact number updated")
else:
print("Invalid number.Contact not updated.")
else:
print("Name not in contact book")
def deletecontact():
na=input("Enter the name which you want to delete the contact:")
if na in pb:
del pb[na]
print("Contact deleted")
else:
print("Name not in contact book")
while True:
print("Phone contact book")
print("1-to add contact\n2-to search contact\n3-to update
contact\n4-to delete contact\n5-to quit")
ch=int(input("Enter your choice:"))
if ch==1:
addcontact()
elif ch==2:
searchcontact()
elif ch==3:
updatecontact()
5
elif ch==4:
deletecontact()
elif ch==5:
break
else:
print("Invalid choice.Give correct choice")
continue
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 the shapes using module.
PROGRAM:
7
MODULE CODE:MODULE NAME : AREA.PY
def square(a):
return a*a
def rectangle(l,b):
return l*b
def triangle(b,h):
return b*h
def circle(r):
return 3.14*r*r
def cube(a):
return 6*a**2
def cylinder(r,h):
return 2*3.14*r*(r+h)
def cone(r,h):
return 3.14*r*r+h
def sphere(r):
return 4*3.14*r**2
MAIN PROGRAM :
import area
print("Area of the shapes calculating program")
while True:
print("1-square\t2-rectangle\t3-triangle\t4-circle\n5-cube\t6-
cylinder\t7-cone\t8-sphere\t9-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)
8
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:
r=float(input("Enter the radius value:"))
h=float(input("Enter the height value:"))
ar=area.cylinder(r,h)
print("The area of the cylinder is :",ar)
elif ch==7:
r=float(input("Enter the radius value:"))
h=float(input("Enter the height value:"))
ar=area.cone(r,h)
print("The area of the cone is :",ar)
elif ch==8:
a=float(input("Enter the radius value:"))
ar=area.sphere(a)
print("The area of the sphere is :",ar)
elif ch==9:
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
1
Do you want to roll again the dice0 (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
vowels=['a','e','i','o','u']
for i in b:
if i.lower() in vowels:
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:
a=open('sample1.txt','r')
b=open('sample2.txt','w')
c=a.readlines()
for i in c:
if 'a' not in i:
b.write(i)
print()
a.close()
b.close()
print("File Content written into Sample2.txt which lines not
contains the letter 'a'")
OUTPUT:
File Content written into Sample2.txt which lines not contains the
letter 'a'
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.
14
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)
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")
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:"))
16
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=open("student.bin",'wb')
pickle.dump(l,a)
print("Record updated successfully")
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")
18
a.close()
a=open("student.bin",'wb')
pickle.dump(l,a)
print("Roll no:",r," Record record 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.
19
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 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")
21
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.
22
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')
23
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)
24
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.
Q1.Write the query to create the above table and set the
constraints for the attributes.
EXAMNO-PRIMARY KEY,NAME- NOT NULL ,MARK CHECK-MARK SHOULD NOT
EXCEED 500.
QUERY:
Q2.Write the query to insert the mentioned records into the table.
QUERY:
QUERY:
QUERY:
Q5.Write the query to display whose marks in the range 400 to 450
(both values are inclusive).
QUERY:
SELECT * FROM STUDENTS WHERE MARK BETWEEN 400 AND 450;
26
SET-2:
QUERY:
Q2.Write the query to display the details whose name is starts with
‘P’ or ‘B’.
QUERY:
SELECT * FROM STUDENTS WHERE NAME LIKE 'P%' OR NAME LIKE 'B%';
Q3. Write the query to display the student name and section whose
name contain ‘priya’.
QUERY:
Q4. Write the query to display all the details sort by name in
descending order.
QUERY:
QUERY:
SET - 3:
Q1.Write the query to add a new column named as CITY with the data
type VARCHAR(30) and apply the default constraint ‘NOT MENTIONED’
in the students table.
27
QUERY:
QUERY:
Q3.Write the query to redefine the NAME field size into VARCHAR(40)
in the students table.
QUERY:
Q4.Write the query to update the mark as 350 whose marks is null
and update the section is A3 whose section is null.
QUERY:
Q5. Write the query to update the city of all records with the
following cities
[CHENNAI,BENGALURU]
QUERY:
SET-4:
Q1.Write the query to create the patient table and keep DOCID to be
the foreign key with update and delete cascade.
QUERY:
Q2.Write the query to display the count of male and female patients
in the age between 40 and 50.
QUERY:
Q3.Write the query to display the patient id,name and age who fixed
the appointment on September month.
29
QUERY:
QUERY:
Q5.write the query to display the sum of the salary of the doctors
department wise.
QUERY:
SET-5
QUERY:
Q2.Write the query to display the doctor id,doctor name and number
of patients need to visit.
QUERY:
Q3.Write the query to display the average salary of each dept from
doctor table.
QUERY:
QUERY:
QUERY:
OUTPUT:
ii)
31
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='password',
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()
32
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='password',datab
ase='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='password',datab
ase='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()
35
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='password',datab
ase='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()
36
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.