csboardprac
csboardprac
csboardprac
#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
#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')
count = 0
file=open('student.txt','r')
lines=file.readlines()
for i in lines:
if i[0] in 'AEIaei':
count+=1
file.close()
#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()
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()
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()
con.close()
# SET 4
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')
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()
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')
-----------------------------------------------------------------------