0% found this document useful (0 votes)
54 views34 pages

Cbse Class 12 Lab PRG

The document outlines 8 Python programs covering various tasks: 1. Checking if a string is a palindrome 2. Creating a phone contact book and searching contacts 3. Calculating the area of different shapes using modules 4. Generating random numbers between 1-6 to simulate rolling a dice 5. Reading a text file and displaying words separated by # 6. Counting vowels, consonants, uppercase, lowercase in a text file 7. Removing lines containing a letter from one file and writing to another 8. Counting the occurrences of a word in a text file

Uploaded by

Kavimuhil G
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)
54 views34 pages

Cbse Class 12 Lab PRG

The document outlines 8 Python programs covering various tasks: 1. Checking if a string is a palindrome 2. Creating a phone contact book and searching contacts 3. Calculating the area of different shapes using modules 4. Generating random numbers between 1-6 to simulate rolling a dice 5. Reading a text file and displaying words separated by # 6. Counting vowels, consonants, uppercase, lowercase in a text file 7. Removing lines containing a letter from one file and writing to another 8. Counting the occurrences of a word in a text file

Uploaded by

Kavimuhil G
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/ 34

CLASS-XII- COMPUTER SCIENCE (083)

PRACTICAL PROGRAMS -2023-24

SECTIONS PAGE NO.


PART-A-PYTHON PROGRAMS 2-24
PART -B - MYSQL QUERIES 25-28
PART-C-PYTHON-MYSQL CONNECTIVITY PROGRAM 29-34
2

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:

print("Palindrome checking program")


ans='y'
while ans=='y':
s=input("Enter the string :")
s1=s[::-1]
if s==s1:
print("The given string",s,"is a palindrome")
else:
print("The given string ",s,"is not a palindrome")
ans=input("Do you want to add check for another string(y/n):")

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.

2. PROGRAM-2: Write a program to create a phone contact book and


also to do search contact.

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:

Phone contact book


1-to add contact
2-to search contact
3-to quit
Enter your choice:1
Enter name:anandh
Enter mobile number:7894561230
Contact saved successfully
Do you want to add another contact(y/n):y
Enter name:joy
Enter mobile number:9876543214
Contact saved successfully
Do you want to add another contact(y/n):n
Phone contact book
1- to add contact
2- to search contact
3-to quit
Enter your choice:2
Enter the name which you want to search:joy
Mobile number : 9876543214
Phone contact book
1-to add contact
2-to search contact
3-to quit
Enter your choice:3
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

3. PROGRAM-3:Write a program to find the area of at least four


shapes using module.

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:

Area of the shapes calculating program


1-square 2-rectangle 3-triangle 4-circle
5-cube 6-quit
Enter your choice:1
Enter the area value:7
The area of the square is : 49.0
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-quit
Enter your choice:2
Enter the length value:8
Enter the breadth value:9
The area of the rectangle is : 72.0
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-quit
Enter your choice:3
Enter the base value:5
Enter the height value:6
The area of the rectangle is : 30.0
1- square 2-rectangle 3-triangle 4-circle
5-cube 6-quit
Enter your choice:6
>>>
7

RESULT:
Thus the above program has been executed successfully and the
output is verified.

4. PROGRAM -4 : Write a random number generator that generates


random numbers between 1 and 6 (simulates a dice).

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 :

I am a computer science student.


I am learning python programming.
9
OUTPUT:

I #am #a #computer #science #student.

I #am #learning #python #programming.

RESULT:
Thus the above program has been executed successfully and the
output is verified.

6. PROGRAM-6 :Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in the file.

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 :

I am a computer science student.


I am learning python programming.
10
OUTPUT:

The number of vowels in the file is : 20


The number of consonants in the file is : 46
The number of upper case letters in the file is : 2
The number of lower case letters in the file is : 52

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:

File Content written into Sample1.txt which lines contains the


letter 'a'

SAMPLE1.TXT FILE CONTENT:

I am a computer science student.


I am learning python programming.
Studying Python is very much useful in future.
11
SAMPLE2.TXT FILE CONTENT (Lines which contains the letter ‘a’):
I am a computer science student.
I am learning python programming.

RESULT:
Thus the above program has been executed successfully and the
output is verified.

8. PROGRAM-8:Read a text file and count the number of occurrence of


the particular word in the file content.

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:

Enter the word which you want to count:python


The given word python found 2 time(s) in the file content

Enter the word which you want to count:game


The given word game not found in the file content

RESULT:
Thus the above program has been executed successfully and the
output is verified.
12

9. PROGRAM -9 Create the binary file which should contains the


student details and to search the particular student based on roll
no and display the details.

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:

Student record search program


Add student details
Enter the roll no:1
Enter name :ramya
Enter mark:78
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :santhosh
Enter mark:89
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to search:2
Roll no: 2
Name: santhosh
Mark: 89.0

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:

Student record update program


Add student details
Enter the roll no:1
Enter name :ashwathan
Enter mark:45
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :srinithi
Enter mark:87
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to update:1
Enter new mark:80
Record updated successfully
[[1,’ashwathan’,80],[2,’srinithi’,87]]
15
RESULT:
Thus the above program has been executed successfully and the
output is verified.

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()

print("Student record delete program")


addrec()
deleterec()

OUTPUT:

Student record delete program


Add student details
Enter the roll no:1
Enter name :manas
Enter mark:85
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :ram
Enter mark:56
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to delete:2
Roll no: 2 Record record successfully

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')

print("Employee details - search record program")


addrec()
searchrec()

OUTPUT:

Employee details - search record program


Enter the emp no:1001
Enter emp name :vasanth
Enter emp salary:10000
Do you want to add another record(y/n):y
18
Enter the emp no:1002
Enter emp name :sriram
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:1003
Enter emp name :keerthivasan
Enter emp salary:25000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to search:1002
Emp no: 1002
Emp name : sriram
Emp salary: 20000

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')

print("Employee details - update record program")


addrec()
updaterec()

OUTPUT:

Employee details - update record program


Enter the emp no:1001
Enter emp name :vasanth
Enter emp salary:10000
Do you want to add another record(y/n):y
Enter the emp no:1002
Enter emp name :sriram
Enter emp salary:20000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to update:1002
Enter the revised salary:25000
record updated

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:

Employee details - delete record program


Enter the emp no:1001
Enter emp name :karthick
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:1002
Enter emp name :santhosh
Enter emp salary:30000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to delete:1002
record deleted

RESULT:
Thus the above program has been executed successfully and the
output is verified.

15. PROGRAM -15 Write a program to implement all stack operations


using list as stack.

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])

print('STACK IMPLEMENTATION PROGRAM')


stk=[]
ans='y'
while ans.lower()=='y':
print('1-PUSH\n2-POP\n3-DISPLAY\n4-EXIT')
ch=int(input('Enter your choice:'))
if ch==1:
PUSH(stk)
elif ch==2:
POP(stk)
elif ch==3:
DISPLAY(stk)
elif ch==4:
break
ans=input('Do you want to do any operation(y/n):')

OUTPUT:

STACK IMPLEMENTATION PROGRAM


1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:1
Enter a city:Banglore
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:1
Enter a city:Delhi
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
23
24
Enter your choice:1
Enter a city:Chennai
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:2
The popped city is: Chennai
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:3
Delhi <--TOP
Banglore
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:4
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.
24

PART -B - MYSQL QUERIES

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

i) Create a table empl.


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);
iv) To display all the information grouping similar jobs.
Ans. select * from empl group by job;
v) To display total employees in each job.
Ans. select count(*),job from empl group by job;
vi) Delete the information of the table
Ans. delete from empl;
vii) Select * from empl order by empno ;
26 Write SQL commands and output.
3) Consider the following table and

GCode Type Price QTY Readydate


10023 Pencil 1150 25 2010-12-19
Skirt
10001 Formal 1250 15 2010-01-12
Skirt
10012 Baby Top 750 20 2009-04-09
10009 Informal 1500 35 2012-12-20
Pant
10020 Frock 850 20 2012-01-01
10089 Slacks 750 10 2010-10-31
i) Create a 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) Increase the price by 500 rupees.
Ans. update garment set price=price+500;
v) Increase the price of garment by 500 where readydate is 2012/01/01
Ans. Update garment set price=price+500 where readydate=’2012-01-01’;
vi) Delete all the information whose Type is NULL
Ans. delete from garment where type is null;
vii) select * from garment order by type asc;
GCode Type Price QTY Readydate
10012 Baby Top 750 20 2009-04-09
10001 Formal 1250 15 2010-01-12
Skirt
10020 Frock 850 20 2012-01-01
10009 Informal 1500 35 2012-12-20
Pant
10023 Pencil 1150 25 2010-12-19
Skirt
10089 Slacks 750 10 2010-10-31

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);

iv) Drop a column Deptno from the table.


Ans. alter table empl drop deptno;
v) To display total employees in each job.
Ans. select count(*),job from empl group by job;
vi) Increase the commission by 5%.
Ans. update empl set comm=comm+comm*0.5;
vii) Select count(*),job from empl group by job;
Count(*) job
1 clerk
3 salesman
1 manager
1 presiden
t
28

5) Consider the following table and Write SQL commands and output.

GCode Type Price QTY Readydate


10023 Pencil 1150 25 2010-12-19
Skirt
10001 Formal 1250 15 2010-01-12
Skirt
10012 Baby Top 750 20 2009-04-09
10009 Informal 1500 35 2012-12-20
Pant
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) Update Garment table by changing the Type to T-Shirt where Type is
Formal Skirt.
Ans. update garment set type=’T-shirt’ where type=’Formal Skirt’;
v) Display the price in descending orders.
Ans. select price from garment order by price desc;
vi) Remove the information whose skirt type ends with ‘t’.
Ans. delete from garment where type like ‘%t’;
vii) select * from garment where type like ‘%t’;
GCode Type Price QTY Readydate
10023 Pencil 1150 25 2010-12-19
Skirt
10001 Formal 1250 15 2010-01-12
Skirt
10009 Informal 1500 35 2012-12-20
Pant
29

PART-C - PYTHON - MYSQL CONNECTIVITY PROGRAMS

1. PROGRAM 1-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.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
ATTRIBUTES : EXAMNO,NAME,CLASS,SECTION,MARK

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.

2. PROGRAM 2-Write a python program to search the records from the


table based on the examno and display the details of the student.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
31
AIM:
To write a python program to search the records from the table
based on the examno and display the details of the student.

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:

ENTER THE EXAMNO YOU WANT TO SEARCH:102


EXAM NO: 102
NAME : RAMYA
CLASS : 12
SECTION : A2
MARKS : 78
DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):y
ENTER THE EXAMNO YOU WANT TO SEARCH:105
RECORD NOT FOUND
DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):n
>>>
32
RESULT:
Thus the above program has been executed successfully and the
output is verified.

3. PROGRAM 3-Write a python program to update the student mark on


table based on the examno given by the user . If record not found
display the appropriate message.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT

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:

ENTER THE EXAMNO YOU WANT TO UPDATE:102


ENTER THE NEW MARK:100
RECORD UPDATED SUCCESSFULLY WITH NEW MARK
DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):Y
ENTER THE EXAMNO YOU WANT TO UPDATE:105
RECORD NOT FOUND
DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):n
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

4. PROGRAM 4- Write a python program to delete the particular record


from the table based on the examno given by the user . If record
not found display the appropriate message.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT

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:

ENTER THE EXAMNO YOU WANT TO DELETE:103


RECORD DELETED SUCCESSFULLY
DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):Y
ENTER THE EXAMNO YOU WANT TO DELETE:103
RECORD NOT FOUND
DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):N
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

You might also like