Term-2 - CSPractical File
Term-2 - CSPractical File
COMPUTER SCIENCE
SUB. CODE - 083
TERM-II PRACTICAL FILE
NAME:___________________________
STREAM:__________________________
External’s Signature
TERM-II INDEX PAGE
Section- A:- Python
PROGRAMS DATE SIGN.
Program 1:- Write a menu based program to perform the operation on stack in python.
Program 2:- Write a menu based program to Maintaining Book details like bcode, btitle
and price using stacks in python.
Program 3:- Write a menu based program to add, delete and display the record of hostel
using list as stack data structure in python. Record of hostel contains the fields : Hostel
number, Total Students and Total Rooms.
Section-B :- Mysql
PROGRAMS DATE SIGN.
1. Write SQL query to create a database Employee.
2. Write SQL query to open database Employee.
3. Write SQL query to create following Table name empl.
21. Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( ) .
22. TEXT FUNCTIONS ( TRIM FUNCTION, SUBSTR FUNCTION, LEFT
FUNCTION, RIGHT FUNCTION , MID FUNCTION )
23. MATH FUNCTIONS ( POWER (), ROUND (), MOD () ).
24. DATE FUNCTIONS NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (),
DAY (), DAYNAME ()
25. Querying and manipulating data using Group by, Having, Order by.
26. Create a table DEPT and show Cartesian Product, JOIN
(Cartesian Join, Equi Join, Natural Join)
*********************************
Section-A ( PYTHON)
Program 1:- Write a menu based program to perform the operation on stack in python.
Source Code:-
def isEmpty(S):
if len(S)==0:
return True
else:
return False
def Push(S,item):
S.append(item)
top=len(S)-1
def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val
def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]
def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t-=1
print()
book=[]
def push():
bcode=input("Enter bcode:- ")
btitle=input("Enter btitle:- ")
price=input("Enter price:- ")
bk=(bcode,btitle,price)
book.append(bk)
def pop():
if(book==[]):
print("Underflow / Book Stack in empty")
else:
bcode,btitle,price=book.pop()
print("poped element is ")
print("bcode:- ",bcode," btitle:- ",btitle," price:- ",price)
def traverse():
if not (book==[]):
n=len(book)
for i in range(n-1,-1,-1):
print(book[i])
else:
print("Empty , No book to display")
while True:
print("Book Stall")
print("*"*40)
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
ch=int(input("Enter your choice:- "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
traverse()
elif(ch==4):
print("End")
break
else:
print("Invalid choice")
Program 3:- Write a menu based program to add, delete and display the record of hostel using
list as stack data structure in python. Record of hostel contains the fields : Hostel number,
Total Students and Total Rooms.
Source Code:-
host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number:-"))
ts=int(input("Enter Total students:-"))
tr=int(input("Enter total rooms:-"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
print("Deleted Record is :",host.pop())
def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("St Joseph's Convent Hostel")
print("*"*40)
print("\n")
print("1. Add Record")
print("2. Delete Record")
print("3. Display Record")
print("4. Exit")
op=int(input("Enter the Choice:-"))
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")
Section-B ( MYSQL )
1. Write SQL query to create and show a database Employee.
8. Write SQL query to display Ename, Sal, and Sal added with comm from table empl.
Ans: select ename,sal,sal+comm from empl;
9. Write SQL query to display Ename, Sal, and deptno who are not getting commission from table
Ans:- select ename,sal,deptno from empl where comm IS NULL;
10. Write SQL query to display Eno, Ename ,Sal, and Sal*12 as Annual Salary whose commission is not NULL
from table empl.
Ans:- select empno,ename,sal,sal*12 "Annal Salary" from empl where comm IS NOT NULL;
11. Display the details of employees whose name have only four letters.
Ans: select * from empl where EName like “________ ‟;
12. Display name, job title and salary of employee who do not have manager.
Ans: select EName, job, sal from empl where mgr = NULL;
13. Display the name of employee whose name contains “A”as fourth letter.
Ans: select EName from empl where EName like “_ _ _A%”
14. Display the name of departments. Each department should be displayed once.
Ans: SELECT DISTINCT(Dept) FROM EMPLOYEE;
15. Display the details of all employee whose annual salary is between 25000 to 40000.Ans:
select * from empl where sal between 25000 and 40000;
16. Write SQL query to display tables data according to ascending order of sal.
Ans: select * from empl order by sal asc;
***********************
21. Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( )
a. Display total salary of all employee listed in empl.
25. Querying and manipulating data using Group by, Having, Order by.
a) Display department no, job and no. of employee in particular job Group by department no,
job from empl.
a) Cartesian Product
b) Write query to join two tables empl and dept on the basis of deptno field. (Equi join)
c) Natural Join
d) Left Join
e) Right join
27: Program to connect with database and store record of employee and
display records.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table if not exists employee(empno int, name varchar(20), dept varchar(20),salary
int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
query="select * from employee"
cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("## Bye!! ##")
else:
print("## INVALID CHOICE ##")
28: Program to connect with database and update the employeerecord of entered
empno.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE UPDATION FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO UPDATE :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
choice=input("\n## ARE YOUR SURE TO UPDATE ? (Y) :")
if choice.lower()=='y':
print("== YOU CAN UPDATE ONLY DEPT AND SALARY ==")
print("== FOR EMPNO AND NAME CONTACT ADMIN ==")
d = input("ENTER NEW DEPARTMENT,(LEAVE BLANK IF NOT WANT TO CHANGE )")
if d=="":
d=row[2]
try:
s = int(input("ENTER NEW SALARY,(LEAVE BLANK IF NOT WANT TO CHANGE ) "))
except:
s=row[3]
query="update employee set dept='{}',salary={} where empno={}".format(d,s,eno)
cur.execute(query)
con.commit()
print("## RECORD UPDATED ## ")
ans=input("UPDATE MORE (Y) :")
29. Program to connect with database and search employee number in table
employee and display record, if empno not found display appropriate message.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO", "%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")
30. Program to connect with database and delete the employeerecord of entered empno.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE DELETION FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO DELETE :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
choice=input("\n## ARE YOUR SURE TO DELETE ? (Y) :")
if choice.lower()=='y':
query="delete from employee where empno={}".format(eno)
cur.execute(query)
con.commit()
print("=== RECORD DELETED SUCCESSFULLY! ===")
ans=input("DELETE MORE ? (Y) :")
*********************************