0% found this document useful (0 votes)
2 views19 pages

csboardprac

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

Aecs Anupuram

Computer Science 2024-25


BOARD PRACTICAL SOLUTIONS
#Done by P.Srividya XII B
#note: I made the solutions as I thought was best, I’m not responsible if the
solutions aren’t the “correct” method

#SET 1
#Q1. A binary file “emp.dat” has structure [EID,Ename,
designation, salary].
#a. Write a user defined function CreateEmp() to input data
for a record and create a file emp.dat.
import pickle
def CreateEmp():
file=open('emp.dat','wb')
ask='y'
while ask=='y':
EID=int(input('enter eid:'))
Ename=input('enter name:')
designation=input('enter designation:')
salary=float(input('enter salary:'))
empdata=[EID,Ename, designation, salary]
pickle.dump(empdata,file)
ask=input('do you want to enter more records? y/n :')
file.close()
#b. Write a function display() in Python to display the detail
of all employees whose salary is more than 50000.
import pickle
def display():
file=open('emp.dat','rb')
while True:
try:
empdata=pickle.load(file)
if empdata[3]>50000:
print(empdata)
except EOFError:
break

#Q2 Fill the Python & MySQL connectivity program to solve


the two queries.
import mysql.connector
con=mysql.connector.connect(host="localhost", user="root",
password="admin")
mycursor = con.cursor()
mycursor.execute('create database if not exists PRACTICAL')
mycursor.execute('use PRACTICAL')

#table creation
mycursor.execute('''
create table IF NOT EXISTS EMPLOYEE (
Empid INT NOT NULL PRIMARY KEY,
EName varchar(40) not null,
CompName varchar(40),
City char(40),
SEX char(1)
) ''')

#values insertion
mycursor.execute('''
INSERT into EMPLOYEE VALUES
(11, 'Aleshia', 'Rosenburg', 'Hawerby', 'F'),
(22, 'Evan', 'Gemini', 'Abbey Ward', 'M'),
(33, 'Franci', 'Elliott', 'Southbourne', 'F'),
(44, 'Ulysses', 'Mcmahan', 'Hawerby', 'M'),
(55, 'Tyisha', 'Champagne', 'Hawerby', 'F')
''')
con.commit()
print(mycursor.rowcount,'Records Inserted')

#Q2 a. Write a query to display employee’ data from above


table for female gender.
mycursor.execute("select * from EMPLOYEE where SEX='F'
")
for i in mycursor.fetchall():
print(i)
#Q2 b.Write a query to display name and city of employee’
whose empid is 30 and above.
mycursor.execute("select EName, City from EMPLOYEE
where Empid>30")
for i in mycursor.fetchall():
print(i)
#SET 2

#Q1 Write a function in python to count number of lines in


a text file “student.txt” which is starting with alphabet ‘A’ or
‘E’ or ‘I’.
def countAEI():

count = 0
file=open('student.txt','r')
lines=file.readlines()
for i in lines:
if i[0] in 'AEIaei':
count+=1
file.close()

#Q2 Fill the Python & MySQL connectivity program to solve


the two queries.
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", password="admin")
mycursor = con.cursor()
mycursor.execute('CREATE DATABASE IF NOT EXISTS
PRACTICAL')
mycursor.execute('USE PRACTICAL')
# table creation
mycursor.execute('''CREATE TABLE IF NOT EXISTS SALES
(
SalesId INT PRIMARY KEY,
Salesperson VARCHAR(50),
Item VARCHAR(50),
Units INT,
UnitCost INT,
TotalCost INT)''')

#record insertion
mycursor.execute('''INSERT INTO SALES (SalesId,
Salesperson, Item, Units, UnitCost, TotalCost)
VALUES (100, "Jones", "Pen", 45, 10, 450),
(102, "Kivell", "Binder", 50, 19, 950),
(105, "Jardine", "Pencil", 36, 16, 576),
(106, "Gill", "Pen", 27, 19, 513),
(115, "Andrews", "Pen", 75, 10, 750),
(119, "Jardine", "Pencil", 90, 11, 990)''')
con.commit()
#a.To search records for item pen for Totalcost more than
600 from Sales table.
mycursor.execute("SELECT * FROM SALES WHERE
item='pen' AND Totalcost>600")

result=mycursor.fetchall()
for i in result:
print(i)
print()

#b.List the details of those items whose UnitCost is more


than 15 sorted by Units
mycursor.execute('SELECT * FROM SALES WHERE
UnitCost>15 ORDER BY Units')
result=mycursor.fetchall()
for i in result:
print(i)
#SET 3

#Q1 Write a python program to get student data (Roll


number, Name, mark) from the user and
# store those details in a CSV file called “marks.csv” and also
display the file contents.

#to store details:


import csv

file = open("marks.csv",'w', newline='')


obj = csv.writer(file)
obj.writerow(['Roll Number', 'Name', 'Mark'])

rows=[]
ask='yes'
while ask=='yes':
rollno = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
# i have appended tuples as elements of the list rows, so
rows is a list of tuples below
rows.append((rollno, name, marks))
ask=input("Do you want to continue? (yes/no): ")

print(rows)
obj.writerows(rows)
file.close()

#to display contents


file = open("marks.csv", 'r')
print(file.read())
file.close()

#Q2 Fill the Python & MySQL connectivity program to solve


the two queries.
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", password="admin")
mycursor = con.cursor()
mycursor.execute('CREATE DATABASE IF NOT EXISTS
PRACTICAL')
mycursor.execute('USE PRACTICAL')

# Creating the table


mycursor.execute('''
CREATE TABLE IF NOT EXISTS STUDENT (
Id INT PRIMARY KEY,
SName VARCHAR(50),
Gender VARCHAR(20),
Country VARCHAR(50),
Age INT,
Marks INT
)
''')

# Inserting records into STUDENT table


mycursor.execute('''
INSERT INTO STUDENT VALUES
(1562, 'Dulce', 'Female', 'United States', 32, 495),
(1582, 'Mara', 'Female', 'Great Britain', 25, 426),
(2587, 'Philip', 'Male', 'France', 36, 480),
(3549, 'Kathleen', 'Female', 'United States', 25, 400),
(3598, 'Etta', 'Female', 'Great Britain', 56, 325),
(6548, 'Vinca', 'Male', 'United States', 40, 485)
''')

con.commit()
print(mycursor.rowcount, 'Records Inserted')
# a. Query to list the names of Male students sorted by
highest to lowest marks
mycursor.execute('''
SELECT SName FROM STUDENT
WHERE Gender = 'Male'
ORDER BY Marks DESC
''')
result = mycursor.fetchall()
for i in result:
print(i[0])

print()

# b. Query to search minimum Marks from student table for


United States
mycursor.execute('''
SELECT MIN(Marks) FROM STUDENT WHERE Country =
'United States'
''')
result = mycursor.fetchone()
print(result[0])

con.close()
# SET 4

#Q1.A binary file “std.dat” has structure [ SID, Sname,


Stream, marks ]

# i)Write a user defined function CreateStd() to input data


for a record and create a file std.dat.
import pickle
def CreateStd():
file = open("std.dat", "wb")
ask = 'yes'
while ask == 'yes':
SID = int(input("Enter Student ID: "))
Sname = input("Enter Student Name: ")
Stream = input("Enter Stream: ")
marks = int(input("Enter Marks: "))
student = [SID, Sname, Stream, marks]
pickle.dump(student, file)
ask = input("Do you want to enter more records?
(yes/no): ")
file.close()
#ii) Write a function sdisplay() in Python to display the
detail of all students whose marks is more than 350.
import pickle
def sdisplay():
file = open("std.dat", "rb")
while True:
try:
student = pickle.load(file)
if student[3] > 350:
print(student)
except EOFError:
break
file.close()

#Q2 Fill the Python & MySQL connectivity program to solve


the two queries.
import mysql.connector

con = mysql.connector.connect(host="localhost",
user="root", password="admin")
mycursor = con.cursor()
mycursor.execute('CREATE DATABASE IF NOT EXISTS
PRACTICAL')
mycursor.execute('USE PRACTICAL')
# creating table
mycursor.execute('''
CREATE TABLE IF NOT EXISTS BOOKS (
BOOK_NAME VARCHAR(100),
PUBLISHERS VARCHAR(50),
PRICE INT
)
''')

# inserting record
mycursor.execute('''
INSERT INTO BOOKS VALUES
('FAST COOK', 'EPB', 355),
('THE TEARS', 'TDH', 650),
('MY FIRST C++', 'EPB', 350),
('C++ BRAINWORKS', 'TDH', 350),
('THUNDERBOLTS', 'FIRST PUBL', 750)
''')
con.commit()
print(mycursor.rowcount, 'Records Inserted')

# a. increase the Profit of all books of TDH publishers by 100


mycursor.execute('''
UPDATE BOOKS SET PRICE = PRICE + 100 WHERE
PUBLISHERS = 'TDH'
''')
con.commit()

# b. display all the books sorted by name in Z to A in


alphabetical order
mycursor.execute('''
SELECT * FROM BOOKS ORDER BY BOOK_NAME DESC
''')
result = mycursor.fetchall()
for i in result:
print(i)

con.close()
SET 5
#Q1. Write a program with separate user defined functions
to perform the following operation.
i) The function should create a list of 10 elements. The
function should traverse the
content of the list and push the numbers higher than 50 into
a stack.

def createlist():
lst=eval(input("enter a list of 10 elements:"))
return lst

def push50(lis,stk):
for i in lis:
if i>50:
stk.append(i)

def create_and_push():
stk=[]
lis=createlist()
push50(lis,stk)

ii) Pop and display the content of the stack and if the stack is
empty, the function should display "Stack is empty".
def popp():
if stk==[]:
print("Stack Empty! Underflow!")
else:
print('Popped out item:',stk.pop())

def display():
for i in range(len(stk)-1,-1,-1):
print(stk[i], end=", ")

def popp_and_display():
popp()
display()

#Q2. Fill the Python & MySQL connectivity program to


solve the two queries.
import mysql.connector

con = mysql.connector.connect(host="localhost",
user="root", password="admin")
mycursor = con.cursor()
mycursor.execute('CREATE DATABASE IF NOT EXISTS
PRACTICAL')
mycursor.execute('USE PRACTICAL')

# creating tables
mycursor.execute('drop table STUDENT')
mycursor.execute('''
CREATE TABLE IF NOT EXISTS STUDENT (
Name VARCHAR(100),
AvgMark FLOAT,
Grade CHAR(1),
Class VARCHAR(10)
)
''')
# inserting records

mycursor.execute('''
INSERT INTO STUDENT (Name, AvgMark, Grade, Class)
VALUES
('Karan', 78.5, 'B', '12B'),
('Divakar', 89.2, 'A', '11C'),
('Divya', 68.6, 'C', '12C'),
('Arun', 73.1, 'B', '12C'),
('Sabina', 90.6, 'A', '11A')
''')
con.commit()
print(mycursor.rowcount, 'Records Inserted')

# a. List the name of students who are in class 12 sorted by


average marks
mycursor.execute('''
SELECT Name FROM STUDENT WHERE Class LIKE '12%'
ORDER BY AvgMark
''')
students = mycursor.fetchall()
print("Students in class 12 sorted by average marks:")
for i in students:
print(i[0])

# b. Delete all records where AvgMark is less than 80


mycursor.execute('''
DELETE FROM STUDENT WHERE AvgMark < 80
''')
con.commit()

-----------------------------------------------------------------------

You might also like