Programs For Term-2 Exam

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

LIST OF PRACTICAL PROGRAMS

CLASS XII 2021-22


CODE:083
Computer Science TERM-II

PART-A Python Programs

1. Write a menu driven program in Python to implement a stack using a list


data- structure.

Solution:
s = []
top=0
def push():
global top
if(top==3):
print("Stack is full")
else:
id=input("Enter elements into the stack")
s.append(id)
top=top+1
def pop():
global top
#print(len(s))
#print(top)
if top==0:
print('Stack is empty')
else:
dn = s.pop()
print(dn)
top=top-1
def disp():
if len(s)==0:
print('empty stack')
else:
for i in range(len(s)):
print(s[i])
while(1):
print('''menu
1. Push
2. Pop
3. Display
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
push()
elif (ch==2):
pop()
elif(ch==3):
disp()
elif (ch==4):
break
else:
print('invalid input')

2. Write a menu driven program in Python to implement a stack using a list


data- structure.
Each node should have
a. Book no
b. Book name
c. Book price

Solution:
s = []
def push():
b_ID=int(input('enter book no.'))
b_NAME=input('enter book name')
b_PRICE=float(input('enter price'))
data=b_ID,b_NAME,b_PRICE
s.append(data)
print("Book added to stack")
def pop():
if len(s)==0:
print('Stack is empty')
else:
dn = s.pop()
print(dn)
def disp():
if len(s)==0:
print('empty stack')
else:
for i in range(len(s)):
print("Book Id :",s[i][0])
print("Book Name :",s[i][1])
print("Book Price :",s[i][2])
while(1):
print('''menu
1. Push
2. Pop
3. Display
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
push()
elif (ch==2):
pop()
elif(ch==3):
disp()
elif (ch==4):
break
else:
print('invalid input')

3. Write a function in python, AddCustomer(item) to add a new element ,


DeleteCustomer(stk) to delete a element from a List and isEmpty(stk) to check
empty stack ,considering them to act as push and pop operations of the Stack
data structure

Solution:
def isEmpty(stk): # checks whether the stack is empty or not
if stk==[]:
return True
else:
return False

def AddCustomer(item): # Allow additions to the stack


stk.append(item)
print(stk)
top=len(stk)-1

def DeleteCustomer(stk):

if isEmpty(stk): # verifies whether the stack is empty or not


print("Underflow")
else: # Allow deletions from the stack
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
print("Pop item is “,str(item))

def Display(stk):
if isEmpty(stk):
print("stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print(str(stk[i]))

stk=[]
top=None
AddCustomer("anitha")
AddCustomer("vanitha")
DeleteCustomer(stk)
Display(stk)

PART-B: MySQL

1. STATIONARY AND CONSUMER


AIM: To create two tables for stationary and consumer and execute the given
commands using SQL.

TABLE: STATIONARY

S_ID StationaryName Company Price

DP01 Dot Pen ABC 10


PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel Pen ABC 15

TABLE: CONSUMER

C_ID ConsumerName Address S_ID


01 Good Learner Delhi PL01
06 Write Well Mumbai GP02
12 Topper Delhi DP01
15 Write & Draw Delhi PL02
16 Motivation Bangalore PL01

i) To display the details of those Consumers whose Address is Delhi

ii) To display the details of Stationary whose Price is in the range of 8 to 15(Both

values included)

iii) To display the ConsumerName , Address from table Consumer and Company and

Price from table Stationery with their corresponding matching S_ID

iv) To increase the Price of all Stationary by 2.

v) To display distinct Company from STATIONARY.

CREATE TABLE STATIONARY (S_ID char(5) NOT NULL PRIMARY KEY, StationaryName

char(25), Company char(5), Price int);

INSERT INTO STATIONARY VALUES(“DP01” , “Dot Pen”, “ABC”, 10);


INSERT INTO STATIONARY VALUES(“PL02” , “Pencil”, “XYZ”, 6);

CREATE TABLE CONSUMER (C_ID int , ConsumerName char(25), Address char(25), S_ID char(5));

INSERT INTO CONSUMER VALUES(01, “Good Learner”, “Delhi”, “PL01”);


INSERT INTO CONSUMER VALUES(06,”Write Well”,”Mumbai”,”GP02”);

OUTPUT:

i) Select * from CONSUMER where address=”delhi”;

c_id consumernam address S_id


e
1 good learner delhi PL01
12 topper delhi DP02
15 write & draw delhi PL02

ii) select * from stationary where price between 8 and 15;

S_id stationary company Price


Dp01 dot pen ABC 10
GP02 gel pen ABC 15

iii) select consumername, address, company, price from stationary, consumer where
stationary.s_id=consumer.s_id;

consumername address company Price


good learner delhi CAM 5
write well mumbai ABC 15
topper delhi ABC 10
write&draw delhi XYZ 6
motivation bangalore CAM 5

iv) update stationary set price=price+2;


select * from stationery;

S_id stationary company Price


DP01 Dot pen ABC 12
PL02 Pencil XYZ 8
ER05 Eraser XYZ 9
PL01 Pencil CAM 7
GP02 Gel pen ABC 17
v) select distinct(company) from stationary;

Company
ABC
XYZ
CAM

2. ITEM AND TRADERS

To create two tables for item and traders and execute the given commands using SQL.
TABLE:ITEM
Code IName Qty Price Company TCode
1001 DIGITAL PAD 121 120 11000 XENTIA T01
1006 LED SCREEN 40 70 38000 SANTORA T02
1004 CAR GPS SYSTEM 50 2150 GEOKNOW T01
1003 DIGITAL CAMERA 12X 160 8000 DIGICLICK T02
1005 PEN DRIVE 32GB 600 1200 STOREHOME T03

TABLE:TRADERS

TCode TName City

T01 ELECTRONICS MUMBAI


SALES
T03 BUSY STORE CORP DELHI

T02 DISP HOUSE INC CHENNAI

i) To display the details of all the items in ascending order of item names (i.e
IName)
ii) To display item name and price of all those items, whose price is in
the range of 10000 and 22000 (both values inclusive)
iii) To display the number of items , which are traded by each trader.
The expected output of this query should be
T01 2
T02 2
T03 1
iv) To display the Price , item name(i.e IName) and quantity(i.e Qty) of
those items which have quantity more than 150.
V) To display the names of those traders, who are either from DELHI or from MUMBAI.

CREATE TABLE ITEM (Code int , IName char(25) , Qty int , Price int , Company char(25),
TCode char(5));

INSERT INTO ITEM VALUES (1001,” DIGITAL PAD 121”,120, 11000,” XENTIA”, “T01”);
INSERT INTO ITEM VALUES (1006,” LED SCREEN 40”,70, 38000,” SANTORA”, “T02”);

CREATE TABLE TRADERS (TCode char(5) , TName char(25), City char(20));

INSERT INTO TRADERS VALUES (“T01”,” ELECTRONICS SALES”,” MUMBAI”);

INSERT INTO TRADERS VALUES (“T03”,” BUSY STORE CORP”,” DELHI”);

OUTPUT:

i) select * from ITEM order by IName;

Code IName Qty Price Company TCode


1004 CAR GPS SYSTEM 50 2150 GEOKNOW T01
1003 DIGITAL CAMERA 12X 160 8000 DIGICLICK T02
1001 DIGITAL PAD 121 120 11000 XENTIA T01
1006 LED SCREEN 70 38000 SANTORA T02
1005 PEN DRIVE 32GB 600 1200 STORE HOME T03

ii) select IName , Price from ITEM where Price between 10000 and 22000;

IName Price
DIGITAL PAD 121 11000

iii) select TCode , count(*) from ITEM group by TCode;

Tcode Count(*)
T01 2
T02 2
T03 1

iv) select Price , IName , Qty from ITEM where Qty>150;

Price IName Qty


8000 DIGITAL CAMERA 12X 160
1200 PEN DRIVE 32GB 600
v) select TName from TRADERS where City in (“DELHI”,”MUMBAI”);

TName
ELECTRONICS SALES
BUSY STORE CORP

3. DOCTOR AND SALARY

AIM: To create two tables for doctor and salary and execute the given
commands using SQL

TABLE:DOCTOR

ID NAME DEPT SEX EXPERIENCE


101 John ENT M 12
104 Smith ORTHOPEDIC M 5
107 George CARDIOLOGY M 10
114 Lara SKIN F 3
109 K George MEDICINE F 9
105 Johnson ORTHOPEDIC M 10
117 Lucy ENT F 3
111 Bill MEDICINE F 12
130 Morphy ORTHOPEDIC M 15

TABLE: SALARY

ID BASIC ALLOWANCE CONSULTATION


101 12000 1000 300
104 23000 2300 500
107 32000 4000 500
114 12000 5200 100
109 42000 1700 200
105 18900 1690 300
130 21700 2600 300

i) Display NAME of all doctors who are in “MEDICINE” having more than 10

years experience from table DOCTOR

ii) Display the average salary of all doctors working in “ENT” department using the

tables DOCTOR and SALARY. (Salary=BASIC+ALLOWANCE)


iii) Display minimum ALLOWANCE of female doctors.

iv) Display DOCTOR.ID , NAME from the table DOCTOR and BASIC , ALLOWANCE

from the table SALARY with their corresponding matching ID.

v) To display distinct department from the table doctor.

CREATE TABLE DOCTOR(ID int NOT NULL PRIMARY KEY, NAME char(25) , DEPT
char(25) , SEX char , EXPERIENCE int);

INSERT INTO DOCTOR VALUES(101,"John","ENT",'M',12);

INSERT INTO DOCTOR VALUES(104,”Smith”, “ORTHOPEDIC”, 'M',5);

CREATE TABLE SALARY(ID int, BASIC int, ALLOWANCE int, CONSULTATION int);

INSERT INTO SALARY VALUES(101, 2000,1000,300);

INSERT INTO SALARY VALUES(104, 23000,2300,500);

OUTPUT:

i) select NAME from DOCTOR where DEPT=”MEDICINE” and EXPERIENCE >10;

NAME
Bill

ii)select avg(BASIC+ALLOWANCE) “avg salary” from DOCTOR , SALARY where


DOCTOR.ID=SALARY.ID and DEPT=”ENT”;
Avg salary
13000.00

iii) select min(ALLOWANCE) from SALARY, DOCTOR where SEX=’F’ and


DOCTOR.ID=SALARY.ID;

min(ALLOWANCE)
1700

iv)select DOCTOR.ID, NAME, BASIC, ALLOWANCE from DOCTOR, SALARY where


DOCTOR.ID=SALARY.ID;
ID NAME BASIC ALLOWANCE
101 John 12000 1000
104 Smith 23000 2300
107 George 32000 4000
109 K George 42000 1700
114 Lara 12000 5200
130 Morphy 21700 2600

v) select distinct(DEPT) from DOCTOR;

DEPT
ENT
ORTHOPEDIC
CARDIOLOGY
SKIN
MEDICINE

4. COMPANY AND CUSTOMER


To create two tables for company and customer and execute the given commands using
SQL.

TABLE : COMPANY

TABLE : CUSTOMER

1. To display those company name which are having price less than 30000.
2. To display the name of the companies in reverse alphabetical order.

3. To increase the price by 1000 for those customer whose name starts with ‘S’

4. To add one more column totalprice with decimal (10,2) to the table customer

5. To display the details of company where productname as mobile.

CREATE TABLE COMPANY(cid int(3) , name varchar(15) , city varchar(10), productname


varchar(15));

INSERT INTO COMPANY VALUES(111,'SONY','DELHI','TV');

INSERT INTO COMPANY VALUES(222,'NOKIA','MUMBAI','MOBILE');

CREATE TABLE CUSTOMER(custid int(3), name varchar(15), price int(10), qty int(3) , cid

int(3));

INSERT INTO CUSTOMER VALUES(101, 'ROHAN SHARMA', 70000, 20,222);


INSERT INTO CUSTOMER VALUES(102, 'DEEPAK KUMAR', 50000, 10,666);

OUTPUT :

1. select company.name from company,customer where company.cid=customer. cid and


price < 30000;

NAME
NEHA SONI
2. select name from company order by name desc;

NAME
SONY
ONIDA
NOKIA
DELL
BLACKBERRY

update customer set price = price + 1000 where name like 's%';

3. select * from customer;


CUSTID NAME PRICE QTY CID
101 ROHAN SHARMA 70000 20 222
102 DEEPAK KUMAR 50000 10 666
103 MOHAN KUMAR 30000 5 111
104 SAHIL BANSAL 36000 3 333
105 NEHA SONI 25000 7 444
106 SONA AGARWAL 21000 5 333
107 ARUN SINGH 50000 15 666

4. alter table customer add totalprice decimal(10,2);

Select * from customer;

CUSTID NAME PRICE QTY CID TOTALPRICE


101 ROHAN SHARMA 70000 20 222 NULL
102 DEEPAK KUMAR 50000 10 666 NULL
103 MOHAN KUMAR 30000 5 111 NULL
104 SAHIL BANSAL 36000 3 333 NULL
105 NEHA SONI 25000 7 444 NULL
106 SONA AGARWAL 21000 5 333 NULL
107 ARUN SINGH 50000 15 666 NULL

5. select * from company where productname='mobile';

CID NAME CITY PRODUCTNAME


222 NOKIA MUMBAI MOBILE
444 SONY MUMBAI MOBILE
555 BLACKBERRY MADRAS MOBILE

5. TEACHER (RELATION)

To create table for teacher and execute the given commands using SQL.

TABLE : TEACHER

No Name Age Department DateofJoin Salary Sex


1 Jishnu 34 Computer 1997/10/01 12000 M
2 Sharmila 31 History 1998/24/03 20000 F
3 Santhosh 32 Maths 1996/12/12 30000 M
4 Shanmathi 35 History 1999/01/07 40000 F
5 Ragu 42 Maths 1997/05/09 25000 M
6 Shiva 50 History 1997/02/27 30000 M
7 Shakthi 44 Computer 1997/02/25 21000 M
8 Shriya 33 Maths 1997/02/27 20000 F

1. To show all information about the teacher of history department.

2. To list the names of female teacher who are in Maths department.

3. To list names of all teachers with their date of joining in ascending order.

4. To count the number of teachers with age>35.

5. To count the number of teachers department wise.

CREATE TABLE TEACHER(No int(2), Name varchar(15), Age int(3) , Department


varchar(15), Dateofjoin date , Salary int(7) , Sex char(1));

INSERT INTO TEACHER VALUES(1,'Jishnu',34,'Computer','1997/10/01',12000,'M');

OUTPUT:

1. select * from teacher where Department=’History’;

No Name Age Department DateofJoin Salary Sex


2 Sharmila 31 History 1998-03-24 20000 F
4 Shanmathi 35 History 1999-01-07 40000 F
6 Shiva 50 History 1997-27-02 30000 M

2. select Name from teacher where Department= 'Maths' and Sex='F'

Name
Shriya

2. select Name, Dateofjoin from teacher order by Dateofjoin asc;

Name Dateofjoin
Santhosh 1996-12-12
Jishnu 1997-10-01
Shakthi 1997-02-25
Shiva 1997-02-27
Shriya 1997-02-27
Ragu 1997-05-09
Sharmila 1998-03-24
Shanmathi 1999-01-07

3. select count(*) from teacher where Age>35;

count(*)
3

4. select Department, count(*) from teacher group by department;

Department count(*)
Computer 2
History 3
Maths 3

5. Write a menu driven program in Python to establish connection between Python


and MySQL. Perform following operations using the connection

Create the tables with the following description and insert at least 5 rows with
appropriate data in them.

Name of table: Store

S.No Column Name Data Type Size Constraint


.
1 Storeid Char 5 Primary Key
2 Name Varchar 20
3 Location Varchar 20
4 City Varchar 15
5 NoofEmp Int 2
6 Dateofopen Date
7 Sales int

• Create a store table given above (in SQL questions)


• Insert rows in the table using user given data
• Drop the attribute ‘city’ and create a new attribute of ‘pincode’.
• Update the pincodes for each record.
• Display the records in ascending / descending order of given column
Solution:

import mysql.connector

db=mysql.connector.connect(host="localhost",user="root",passwd="purab@123",database="sc
hool")
cursor=db.cursor()
def create():
cursor.execute('''CREATE TABLE IF NOT EXISTS STORE2
(Storeid char(5) Primary Key,
Name varchar(20),
Location varchar(20),
City varchar(15),
NoofEmp int,
Dateofopen date,
Sales int)''')

def dispstruct():
cursor.execute('''describe STORE2''')
for x in cursor:
print(x)

def insert1():
a=input('Enter storeid')
b=input('Enter name')
c=input('Enter Location')
d=input('Enter City')
e=int(input('Enter NoofEmp'))
f= input("Enter the date of opening")
g = int(input('Enter sales'))
cursor.execute("insert into store2 \
values('"+a+"','"+b+"','"+c+"','"+d+"','"+str(e)+"','"+f+"','"+str(g)+"')")
db.commit()

def showdata():
cursor.execute('''select * from STORE2''')
row = cursor.fetchall()
for r in row:
print(r)

def altertab():
try:
cursor.execute('''alter table STORE2 drop city''')
cursor.execute('''alter table STORE2 add pincode int(6)''')
except:
print("Column already deleted")
return
while(1):
print('''menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
6. showrecords
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
create()
elif (ch==2):
dispstruct()
elif (ch==3):
insert1()
elif (ch==6):
showdata()
elif (ch==4):
altertab()
elif (ch==5):
break
else:
print('invalid input')

7. Write a menu driven program in Python to establish connection between Python and
MySQL. Perform following operations using the connection using the ‘store’
table created above. (create and insert is common for 6th and 7th Program)

oCreate a store table given above (in 6th SQL question)


o Insert rows in the table using user given data
o Display total number of stores in each pincode
o Display the maximum sales of stores having more than 10 employees
o Display total number of employees in each pincode.

import mysql.connector

db=mysql.connector.connect(host="localhost",user="root",passwd="purab@123",database="sc
hool")
cursor=db.cursor()

def create():
cursor.execute('''CREATE TABLE IF NOT EXISTS STORE2
(Storeid char(5) Primary Key,
Name varchar(20),
Location varchar(20),
City varchar(15),
NoofEmp int,
Dateofopen date,
Sales int)''')

def insert1():
a=input('Enter storeid')
b=input('Enter name')
c=input('Enter Location')
d=input('Enter City')
e=int(input('Enter NoofEmp'))
f= input("Enter the date of opening")
g = int(input('Enter sales'))
cursor.execute("insert into store2 \
values('"+a+"','"+b+"','"+c+"','"+d+"','"+str(e)+"','"+f+"','"+str(g)+"')")
db.commit()

def nstore():
cursor.execute('''select city,count(*) from STORE group by city''')
row = cursor.fetchall()
for r in row:
print(r)

def mxsales():
cursor.execute('''select name,max(Sales) from STORE where Noofemp>10''')
row = cursor.fetchall()
for r in row:
print(r)

def nemp():

cursor.execute("select city,sum(Noofemp) from STORE group by city")


#row = cursor.fetchall()
for r in cursor:
print(r)

def showdata():
cursor.execute('''select * from STORE2''')
row = cursor.fetchall()
for r in row:
print(r)

while(1):
print('''menu
1. Create table
2. Insert data
3. Show data
4. City wise total number of stores
5. Maximum sales (more than 10 employees)
6. City wise total number of employees
7. Exit''')
ch=int(input('enter choice'))
if (ch==1):
create()
elif (ch==2):
insert1():
elif (ch==3):
showdata():
elif (ch==4):
nstore()
elif (ch==5):
mxsales()
elif (ch==6):
nemp()
elif (ch==4):
break
else:
print('invalid input')

You might also like