Class XII CS Practical List 2022

advertisement
KENDRIYA VIDYALAYA MALANJKHAND
List of Practical for Class XII Computer Science Term - 2
Session 2021-22
S.No.
Practical
1.
Write a program in Python to implement the following operation on Stack
containing employee code and name.
1. PUSH
2. POP
3. DISPLAY
Consider a dictionary with keys as course name and fee as value. Write a program
to push course name in stack where fee is more than 10000. Pop and display contents
of stack on the screen.
2.
3.
4.
5.
6.
Create a list ‘BOOK’ implemented as stack and write code to perform the Push and
Pop operation on stack. Define separate functions for each operation.
Perform the following operations in MySQL using SQL command
a. Create a table emp with attributes empno, ename, salary
b. Add following columns using alter table command
i.
Age
ii.
City
iii.
MobileNo
c. Add primary key to the above table.
d. Change the data type size of salary column.
e. Remove the column 'City' and ‘MobileNo’ from the above table.
f. Show the structure of table emp.
Consider the following table 'EMP' and write commands for given queries:
a) Show empname, age, gender of those employee who are MALE and age is
less than 50.
b) Display empname, salary, city of those whose salary lies between 4000 and
8000.
c) Retrieve those records where employee city is BHOPAL or JABALPUR
d) Print empcode and name of those employees where name contains second
letter as 'a'.
e) Show average age of MALE and FEMALE employees.
Create a table candidate in the database 'EXAM' with the following attributes
rollno
name
marks
Write insert command to enter records in the above table and write sql queries to
display
1. Max marks scored by a candidate
-1-
Date
7.
8.
9.
10.
2. Min marks scored by a candidate
3. Average marks scored by a candidate
4. Total number of candidates
5. Show the name and marks of candidates in alphabetical order.
Write SQL commands and output for the given queries
a) Display ename, job, dname of each employee.
b) Show ename, sal, dname of those employee whose salary is more than 2000.
c) Retrieve ename, job,deptno,dname of those employee whose job is
‘MANAGER’
d) Show ename, job, dname of all ‘SALESMAN’
e) Show the ename and dname of all those employee whose name contains
letter ‘E’.
Write SQL commands for the following queries
a. Show ename, sal of those employees whose salary is more than 1500.
b. Show ename, sal of those employees whose salary is less than 2000.
c. Show ename, sal and hiredate in ascending order
d. Show ename , sal+comm for each employee
e. Show ename, sal of those employee who are not getting commission.
Write a program of database connectivity of Python with MySQL. Program will
store the data entered by the user in a table named as 'library'.library table has
following attributes
bookid
title
author
price
Write a database connectivity program python with MySQL to update the records
in a table ‘Library’
-2-
S.No.
1.
Code
Output
Practical
Write a program in Python to implement the following operation on Stack
containing employee code and name.
1. PUSH
2. POP
3. DISPLAY
employee = []
choice = 'y'
while (choice=='y' or choice=='Y'):
print("1. PUSH")
print("2. POP")
print("3. DISPLAY STACK")
choice1 = int(input("Enter any choice: "))
if(choice1==1):
e_id= input('Enter employee code ')
ename=input('Enter name ')
emp = (e_id, ename)
employee.append(emp)
elif(choice1==2):
if len(employee)==0:
print('Stack empty')
else:
e_id, ename = employee.pop()
print('Deleted element is ',e_id, ename)
elif(choice1==3):
top = len(employee)
while top>0:
print(employee[top-1])
top = top -1
else:
print('Wrong Input')
choice = input('Do you want to continue? y or n: ')
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 1
Enter employee code 1201
Enter name Raman
Do you want to continue? y or n:y
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 1
Enter employee code 1202
Enter name Prem
Do you want to continue? y or n:y
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 1
-3-
Date
Enter employee code 1203
Enter name Meenu
Do you want to continue? y or n:y
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 3
('1203', 'Meenu')
('1202', 'Prem')
('1201', 'Raman')
Do you want to continue? y or n: y
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 2
Deleted element is 1203 Meenu
Do you want to continue? y or n: y
1. PUSH
2. POP
3. DISPLAY STACK
Enter any choice: 3
('1202', 'Prem')
('1201', 'Raman')
Do you want to continue? y or n: n
2.
Code
Consider a dictionary with keys as course name and fee as value. Write a
program to push course name in stack where fee is more than 10000. Pop and
display contents of stack on the screen.
course_dict = {'BSC':8000,
'MCA':30000, 'MBA':50000 }
'MSC':12000,
'BCA':15000,
STACK = [ ]
def NEWDATA():
course = input('Enter course name :')
fee = int(input('Enter fee '))
course_dict[course] = fee
print('Data saved successfully ')
print(course_dict)
def PUSH():
for key in course_dict:
if course_dict[key] > 10000:
STACK.append(key)
def POP():
if len(STACK)==0:
print('STACK EMPTY, No course with fee>10000')
else:
print('course fee more than 10000')
for top in range(len(STACK), 0, -1):
-4-
'PGDCA':9000,
print(STACK.pop())
Output
3.
NEWDATA()
PUSH()
POP()
Enter course name :BA
Enter fee 6500
Data saved successfully
{'BSC': 8000, 'MSC': 12000, 'BCA': 15000, 'PGDCA': 9000, 'MCA': 30000, 'MBA':
50000, 'BA': 6500}
course fee more than 10000
MBA
MCA
BCA
MSC
Create a list ‘BOOK’ implemented as stack and write code to perform the
Push and Pop operation on stack. Define separate functions for each
operation.
BOOKS = [ ]
def PUSH(item):
BOOKS.append(item)
print('Stack now\n', BOOKS)
def POP(STACK):
print(STACK.pop(),' REMOVED')
n = int(input('How many items you want to push in stack '))
for i in range(n):
bname = input('Enter book name ')
PUSH(bname)
Solution
n2 = int(input('How many items you want to pop from stack '))
if n2<=len(BOOKS):
for i in range(n2):
POP(BOOKS)
print('Now contents of stack \n',BOOKS)
else:
pass
How many items you want to push in stack 5
Enter book name physics
Stack now
['physics']
Enter book name chemistry
Stack now
-5-
4.
Solution
['physics', 'chemistry']
Enter book name math
Stack now
['physics', 'chemistry', 'math']
Enter book name english
Stack now
['physics', 'chemistry', 'math', 'english']
Enter book name computer
Stack now
['physics', 'chemistry', 'math', 'english', 'computer']
How many items you want to pop from stack 3
computer REMOVED
english REMOVED
math REMOVED
Now contents of stack
['physics', 'chemistry']
Perform the following operations in MySQL using SQL command
g. Create a table emp with attributes empno, ename, salary
h. Add following columns using alter table command
iv.
Age
v.
City
vi.
MobileNo
i. Add primary key to the above table.
j. Change the data type size of salary column.
k. Remove the column 'City' and ‘MobileNo’ from the above table.
l. Show the structure of table emp.
(a)
create table emp
(
empno int,
ename varchar(30),
salary int
);
(b)
ALTER TABLE EMP ADD
(
AGE
INT(2),
CITY
VARCHAR(30),
MOBILENO CHAR(10)
);
(c) ALTER TABLE EMP ADD PRIMARY KEY(EMPNO);
(d) ALTER TABLE EMP MODIFY COLUMN SALARY FLOAT(7,2);
(e) ALTER TABLE EMP DROP COLUMN CITY, DROP COLUMN MOBILENO;
(d) DESCRIBE EMP;
-6-
5.
Consider the following table 'EMP' and write commands for given queries:
f) Show empname, age, gender of those employee who are MALE and
age is less than 50.
g) Display empname, salary, city of those whose salary lies between
4000 and 8000.
h) Retrieve those records where employee city is BHOPAL or
JABALPUR
i) Print empcode and name of those employees where name contains
second letter as 'a'.
j) Show average age of MALE and FEMALE employees.
a) SELECT EMPNAME, AGE, GENDER
FROM EMP
WHERE GENDER='MALE' AND AGE<50;
b)
SELECT EMPNAME, SALARY, CITY
FROM EMP
WHERE SALARY BETWEEN 4000 AND 8000;
-7-
c)
SELECT * FROM EMP
WHERE CITY='BHOPAL' OR CITY='JABALPUR';
d)
SELECT EMPCODE, EMPNAME
FROM EMP
WHERE EMPNAME LIKE '_a%';
e)
SELECT GENDER, AVG(AGE)
FROM EMP
GROUP BY GENDER;
6.
Create a table candidate in the database 'EXAM' with the following
attributes
rollno
name
marks
Write insert command to enter records in the above table and write sql
queries to display
1. Max marks scored by a candidate
2. Min marks scored by a candidate
3. Average marks scored by a candidate
4. Total number of candidates
5. Show the name and marks of candidates in alphabetical order.
SOLUTION CREATE DATABASE EXAM;
USE EXAM;
Table creation
CREATE TABLE CANDIDATE
(
rollno
int
primary key,
name
varchar(30) not null,
marks
int
);
-8-
Record Insertion
insert into candidate(rollno, name, marks)
values(101,'Mohan',75);
insert into candidate(rollno, name, marks)
values(102,'Rohit',69);
insert into candidate(rollno, name, marks)
values(103,'Ramesh',84);
insert into candidate(rollno, name, marks)
values(104,'Seema',92);
insert into candidate(rollno, name, marks)
values(105,'Reeta',44);
SELECT * FROM CANDIDATE;
To show maximum marks scored by a candidate
SELECT max(marks)
FROM CANDIDATE;
To show minimum marks obtained by a student
SELECT min(marks) FROM CANDIDATE;
To find the average marks of candidates
SELECT AVG(MARKS) FROM CANDIDATE;
-9-
Show total number of candidates
SELECT COUNT(*) FROM CANDIDATE;
To display name and marks of all students with name in alphabetical order
SELECT NAME,MARKS
FROM CANDIDATE
ORDER BY NAME ASC;
7.
Write SQL commands and output for the given queries
DROP TABLE IF EXISTS emp;
CREATE TABLE emp (
empno decimal(4,0) NOT NULL,
ename varchar(10) default NULL,
job varchar(9) default NULL,
mgr decimal(4,0) default NULL,
hiredate date default NULL,
sal decimal(7,2) default NULL,
comm decimal(7,2) default NULL,
deptno decimal(2,0) default NULL
);
DROP TABLE IF EXISTS dept;
CREATE TABLE dept (
deptno decimal(2,0) default NULL,
dname varchar(14) default NULL,
loc varchar(13) default NULL
);
INSERT INTO emp VALUES ('7369','SMITH','CLERK','7902','1980-12-17','800.00',NULL,'20');
INSERT INTO emp VALUES ('7499','ALLEN','SALESMAN','7698','1981-02-20','1600.00','300.00','30');
INSERT INTO emp VALUES ('7521','WARD','SALESMAN','7698','1981-02-22','1250.00','500.00','30');
INSERT INTO emp VALUES ('7566','JONES','MANAGER','7839','1981-04-02','2975.00',NULL,'20');
INSERT INTO emp VALUES ('7654','MARTIN','SALESMAN','7698','1981-09-28','1250.00','1400.00','30');
INSERT INTO emp VALUES ('7698','BLAKE','MANAGER','7839','1981-05-01','2850.00',NULL,'30');
INSERT INTO emp VALUES ('7782','CLARK','MANAGER','7839','1981-06-09','2450.00',NULL,'10');
INSERT INTO emp VALUES ('7788','SCOTT','ANALYST','7566','1982-12-09','3000.00',NULL,'20');
INSERT INTO emp VALUES ('7839','KING','PRESIDENT',NULL,'1981-11-17','5000.00',NULL,'10');
INSERT INTO emp VALUES ('7844','TURNER','SALESMAN','7698','1981-09-08','1500.00','0.00','30');
INSERT INTO emp VALUES ('7876','ADAMS','CLERK','7788','1983-01-12','1100.00',NULL,'20');
INSERT INTO emp VALUES ('7900','JAMES','CLERK','7698','1981-12-03','950.00',NULL,'30');
INSERT INTO emp VALUES ('7902','FORD','ANALYST','7566','1981-12-03','3000.00',NULL,'20');
INSERT INTO emp VALUES ('7934','MILLER','CLERK','7782','1982-01-23','1300.00',NULL,'10');
-10-
INSERT INTO dept VALUES ('10','ACCOUNTING','NEW YORK');
INSERT INTO dept VALUES ('20','RESEARCH','DALLAS');
INSERT INTO dept VALUES ('30','SALES','CHICAGO');
INSERT INTO dept VALUES ('40','OPERATIONS','BOSTON');
f) Display ename, job, dname of each employee.
g) Show ename, sal, dname of those employee whose salary is more
than 2000.
h) Retrieve ename, job,deptno,dname of those employee whose job is
‘MANAGER’
i) Show ename, job, dname of all ‘SALESMAN’
j) Show the ename and dname of all those employee whose name
contains letter ‘E’.
a)
select ename,job,dname
from emp, dept
where emp.deptno=dept.deptno;
b)
SELECT ENAME, SAL, DNAME
FROM EMP, DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO AND SAL>2000;
c)
SELECT ENAME,JOB,EMP.DEPTNO,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND JOB='MANAGER';
d)
-11-
SELECT ENAME,JOB,DNAME FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND JOB='SALESMAN';
e)
select ename, dname
from emp, dept
where emp.deptno=dept.deptno
and ename like '%E%';
8.
Sol
Write SQL commands for the following queries
f. Show ename, sal of those employees whose salary is more than 1500.
g. Show ename, sal of those employees whose salary is less than 2000.
h. Show ename, sal and hiredate in ascending order
i. Show ename , sal+comm for each employee
j. Show ename, sal of those employee who are not getting commission.
a.
select ename,sal
from emp
where sal>1500;
b.
select ename,sal
from emp
where sal<2000;
-12-
c.
SELECT ENAME,SAL,HIREDATE
FROM EMP
ORDER BY HIREDATE ASC;
d.
SELECT ENAME, SAL+COMM
FROM EMP;
e.
SELECT ENAME,SAL
FROM EMP
WHERE COMM IS NULL;
-13-
9.
Write a program of database connectivity of Python with MySQL. Program
will store the data entered by the user in a table named as 'library'.library
table has following attributes
bookid
title
author
price
import mysql.connector
mycon = mysql.connector.connect(host='localhost', user='root',
passwd='kvm@1234', database='mylibrary');
mycur = mycon.cursor()
sql1='''
create table if not exists library(
bookid
int
primary key,
title
varchar(30),
author
varchar(30),
price
int
)'''
mycur.execute(sql1)
n = int(input('How many records you want to enter? '))
for x in range(n):
bno
= int(input('Enter book id '))
btitle
= input('Enter title of the book ')
bauthor = input('Enter author of the book ')
bprice
= int(input('Enter price of book '))
sql2 = "insert into library values({},'{}','{}',{})".format(bno, btitle,
bauthor, bprice )
mycur.execute(sql2)
mycon.commit()
print('Record inserted successfully ')
sql3 = 'SELECT * FROM LIBRARY'
mycur.execute(sql3)
-14-
data = mycur.fetchall()
for y in data:
print(y)
10.
Code
mycon.close()
How many records you want to enter? 3
Enter book id 111
Enter title of the book Java
Enter author of the book Deepak
Enter price of book 540
Record inserted successfully
Enter book id 222
Enter title of the book VB
Enter author of the book Yogesh
Enter price of book 340
Record inserted successfully
Enter book id 333
Enter title of the book Python
Enter author of the book Sunil
Enter price of book 680
Record inserted successfully
(111, 'Java', 'Deepak', 540)
(222, 'VB', 'Yogesh', 340)
(333, 'Python', 'Sunil', 680)
Write a database connectivity program python with MySQL to update the
records in a table ‘Library’
import mysql.connector
mycon = mysql.connector.connect(host='localhost', user='root',
passwd='kvm@1234', database='mylibrary');
mycur = mycon.cursor()
bnolist =[]
print('Existing records ')
sql1 = 'SELECT * FROM LIBRARY'
mycur.execute(sql1)
data = mycur.fetchall()
for y in data:
print(y)
bnolist.append(y[0])
bno = int(input('Enter book number to update '))
if bno not in bnolist:
print('book no not found!')
else:
t = input('Enter title ')
-15-
a = input('Enter author ')
p = int(input('Enter price '))
sql2='''UPDATE LIBRARY
SET title='{}',author='{}',price={}
WHERE BOOKID={}'''.format(t,a,p,bno)
mycur.execute(sql2)
mycon.commit()
print('data updated successfully ')
mycur.execute(sql1)
data = mycur.fetchall()
for y in data:
print(y)
Output
mycon.close()
Existing records
(111, 'Java', 'Deepak', 540)
(222, 'Visual Basic', 'Umesh', 430)
(333, 'Python', 'Sunil', 680)
Enter book number to update 222
Enter title VB
Enter author Kartik
Enter price 450
data updated successfully
(111, 'Java', 'Deepak', 540)
(222, 'VB', 'Kartik', 450)
(333, 'Python', 'Sunil', 680)
-16-
Download