1. Write a function write_emp() to write the records for 5 Employees in a file named as [Link].
Each record must contain Emp_no, Emp_name, Salary.
Write another function read_emp( ) to read all the records from that file and display them.
Ans.
import csv
def write_emp():
f=open("[Link]","a",newline="")
data = [Link](f)
[Link](['Emp_no','Emp_name','Salary'])
records = []
for i in range(5):
Emp_no= int(input("Enter emp no: "))
Emp_name = input("Enter Name: ")
Salary = int(input("Enter salary: "))
l=[Emp_no,Emp_name,Salary]
[Link](l)
[Link](records)
print("Record Added....")
[Link]()
def read_emp():
f=open("[Link]","r")
data = [Link](f)
#header = next(data)
for i in data:
print(i)
print(data)
[Link]()
while True:
print("[Link] Record [Link] Record [Link]")
ch=int(input("Enter your choice:"))
if ch==1:
write_emp()
elif ch==2:
read_emp()
elif ch==3:
break
else:
print("Invalid Choice")
2. Write a function write_bin() to write the records for 5 Employees in a file named as [Link].
Each record must contain Emp_no, Emp_name, Salary.
Write another function read_bin( ) to read all the records from that file and display them.
Ans.
import pickle
def write_bin():
f=open("[Link]","ab")
for i in range(5):
Emp_no= int(input("Enter emp no: "))
Emp_name = input("Enter Name: ")
Salary = int(input("Enter salary: "))
l=[Emp_no,Emp_name,Salary]
[Link](l,f)
print("Record Added....")
[Link]()
def read_bin():
f=open("[Link]","rb")
try:
while True:
data = [Link](f)
print(data)
except:
[Link]()
while True:
print("[Link] Record [Link] Recor [Link]")
ch=int(input("Enter your choice:"))
if ch==1:
write_bin()
elif ch==2:
read_bin()
elif ch==3:
break
else:
print("Invalid Choice")
3. Write a function in python , Push(Vehicle) where Vehicle is a dictionary containing details of vehicles-
{Car_name: Maker}. The function should push the name of car manufactured by ‘TATA’. Write
another function Pop() that will pop the values from the stack and in case of underflow situation show
proper message.
E.g. If the Vehicle dictionary contains {'Santro': 'Hyundai','Nexon': 'TATA','Safari': 'TATA','Civic':
'Honda'}, then the values in stack should be [‘Nexon’,Safari’]. The output of the Pop() should be
Safari Nexon Stack empty.
Ans.
stack = []
def Push(Vehicle):
for i in Vehicle:
if Vehicle[i] == 'TATA':
[Link](i)
print(stack)
def Pop(stack):
while len(stack)!= 0:
print([Link]())
else:
print("Stack empty")
# Example usage:
vehicle = {'Santro': 'Hyundai','Nexon': 'TATA','Safari': 'TATA','Civic': 'Honda'}
while True:
print("\[Link] [Link] [Link]")
ch=int(input("Enter your choice:"))
if ch==1:
Push(vehicle)
elif ch==2:
Pop(stack)
elif ch==3:
break
else:
print("Invalid Choice")
4. There is list of 10 numbers . Write a function push() to traverse the content of the list and push the
numbers into a stack which are divisible by 5. Also write pop() function to display the content of the
stack.
For Example:
If the sample Content of the list is as follows:
N=[2,5,10,13,20,23,45,56,60,78]
Sample Output of the code should be:
60, 45, 20, 10, 5
Ans.
stack = []
def push(N):
for i in N:
if i%5 == 0:
[Link](i)
def pop(stack):
while len(stack)!= 0:
print([Link](),end = " ")
N=[2,5,10,13,20,23,45,56,60,78]
while True:
print("\[Link] [Link] [Link]")
ch=int(input("Enter your choice:"))
if ch==1:
push(N)
elif ch==2:
pop(stack)
elif ch==3:
break
else:
print("Invalid Choice")
5. Write SQL queries for (i) to (iv) which are based on the tables.
Table: STUDENT
STUDENT_ID Name GENDER MARKS GRADE
SCS101 Anil M 90 9
SCS102 Rima F 88 10
SCS103 Avi M 96 9
SCS104 Arun M 75 11
SCS105 Ritu F 99 10
Table : GAMESPLAYED
GAME_ID STUDENT_ID GAME_NAME
G1 SCS101 CRICKET
G2 SCS102 CHESS
G3 SCS103 TENNIS
G4 SCS101 SWIMMING
G5 SCS103 BADMINTON
i) Display the student id, Gender, Name, Marks in the ascending order of their marks.
ii) Display the grade wise, maximum and minimum marks of the students.
iii) Display the records of the students whose name start with ‘A’.
iv) Display the student id, student name and game name where student name is ‘Anil’.
Ans.
CREATE DATABASE 12BOARDS;
USE 12BOARDS;
CREATE TABLE STUDENT(STUDENT_ID VARCHAR(20) PRIMARY KEY, NAME
VARCHAR(100), GENDER CHAR(1), MARKS INT, GRADE INT);
INSERT INTO STUDENT VALUES('SCS101', 'ANIL', 'M', 90, 9);
INSERT INTO STUDENT VALUES('SCS102', 'RIMA', 'F', 88, 10);
INSERT INTO STUDENT VALUES('SCS103', 'AVI', 'M', 96, 9);
INSERT INTO STUDENT VALUES('SCS104', 'ARUN', 'M', 75, 11);
INSERT INTO STUDENT VALUES('SCS105', 'RITU', 'F', 99, 10);
CREATE TABLE GAMESPLAYED(GAME_ID VARCHAR(20) PRIMARY KEY,
STUDENT_ID VARCHAR(20), GAME_NAME VARCHAR(100));
INSERT INTO GAMESPLAYED VALUES('G1', 'SCS101', 'CRICKET');
INSERT INTO GAMESPLAYED VALUES('G2', 'SCS102', 'CHESS');
INSERT INTO GAMESPLAYED VALUES('G3', 'SCS103', 'TENNIS');
INSERT INTO GAMESPLAYED VALUES('G4', 'SCS101', 'SWIMMING');
INSERT INTO GAMESPLAYED VALUES('G5', 'SCS103', 'BADMINTON');
i) SELECT STUDENT_ID, GENDER, NAME, MARKS FROM STUDENT ORDER BY
MARKS;
ii) SELECT GRADE, MAX(MARKS), MIN(MARKS) FROM STUDENT GROUP BY GRADE;
iii) SELECT * FROM STUDENT WHERE NAME LIKE 'A%';
iv) SELECT STUDENT.STUDENT_ID, NAME, GAME_NAME FROM STUDENT,
GAMESPLAYED WHERE STUDENT.STUDENT_ID = GAMESPLAYED.STUDENT_ID AND
NAME=’ANIL’;
6. Write SQL queries for (i) to (iv) which are based on the tables.
Table: GAMES
GCode GameName Number PrizeMoney
101 Carom Board 2 5000
102 Badminton 2 12000
103 Table Tennis 4 8000
105 Chess 2 9000
108 Lawn Tennis 4 25000
Table : PLAYER
PCode Name Gcode
1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101
4 Nazneen 103
v) Display the name of all players with their game names.
vi) Display the details of those games which are having PrizeMoney more than 7000.
vii) Display the records of the games where game name start with ‘C’.
viii) Display the content of games table in the descending order of the prize money.
Ans.
CREATE DATABASE 12BOARDS;
USE 12BOARDS;
CREATE TABLE GAMES(GCODE INT PRIMARY KEY, GAMENAME VARCHAR(50),
NUMBER INT, PRIZEMONEY INT);
INSERT INTO GAMES VALUES(101,'CAROM BOARD', 2, 5000);
INSERT INTO GAMES VALUES(102,'BADMINTON', 2, 12000);
INSERT INTO GAMES VALUES(103,'TABLE TENNIS', 4, 8000);
INSERT INTO GAMES VALUES(104,'CHESS', 2, 9000);
INSERT INTO GAMES VALUES(108,'LAWN TENNIS', 4, 25000);
CREATE TABLE PLAYER(PCODE INT PRIMARY KEY, NAME VARCHAR(100), GCODE
INT);
INSERT INTO PLAYER VALUES(1,'NABI AHMAD',101);
INSERT INTO PLAYER VALUES(2,'RAVI SAHAI',108);
INSERT INTO PLAYER VALUES(3,'JATIN',101);
INSERT INTO PLAYER VALUES(4,'NAZNEEN',103);
i) SELECT NAME,GAMENAME FROM PLAYER,GAMES WHERE [Link] =
[Link];
ii) SELECT * FROM GAMES WHERE PRIZEMONEY >7000;
iii) SELECT * FROM GAMES WHERE GAMENAME LIKE 'C%';
iv) SELECT * FROM GAMES ORDER BY PRIZEMONEY DESC;