You are on page 1of 23

TERM-2

XII - COMPUTER SCIENCE (CS 083) – PRACTICAL RECORD

1. Basic operations of a stack (push, pop, display) by using a list.


AIM:
To write a Python program to implement all basic operation of a Stack
by using a list.
ALGORITHM:
Step-1: Start the program.
Step-2: Create an empty stack
Step-3: Display the menu for stack operations.
Step-4: Read the user choice.
Step-5: Perform the stack operation based on the user’s choice.
Step-6: Display the result.
Step-7: Stop the program.

PROGRAM:

s=[]
c="y"
while (c=="y"):
print("1. PUSH")
print("2. POP")
print("3.DISPLAY")
choice=int(input("Enter your choice:"))
if(choice==1):
a=input("Enter any number:")
s.append(a)
elif(choice==2):
if(s==[]):
print("Stack Empty")
else:
print("Deleted element is: ", s.pop())
elif(choice==3):
l=len(s)
for i in range(1,-1,-1):
print([i])
else:
print("Wrong Input")
c=input("Do you want to continue or not?")

OUTPUT:

Result:
Thus Python program has been executed and the output is verified
successfully.
2. Create a stack called employee, to perform the operations on stack using list.
AIM:
To write a program to create a stack called employee, to perform the basic operations on
stack using list.

ALGORITHM:
Step-1: Start the program.
Step-2: Create an empty stack
Step-3: Display the menu for stack operations.
Step-4: Read the user choice.
Step-5: Perform the stack operation based on the user’s choice.
Step-6: Display the result.
Step-7: Stop the program.

PROGRAM:
# program to add, delete and display the records of an employee using list
# implementation through stack
Employee=[]
c="y"
while (c=="y"):
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
choice=int (input("Enter your choice:"))
if (choice==1):
e_id=input("Enter Employee no:")
ename=input("Enter the Employee name:")
emp=(e_id,ename)
Employee.append(emp)
elif(choice==2):
if(Employee==[]):
print("Stack Empty")
else:
e_id,ename=Employee.pop()
print("Deleted element is:", e_id,ename)
elif(choice==3):
i=len(Employee)
while i>0:
print(Employee[i-1])
i=i-1
else:
print("Wrong Input")
c=input("Do you want to continue or not?")

OUTPUT:

Result:
Thus Python program has been executed and the output is verified
successfully.
3. Menu-driven python program to implement stack operation.

Aim:
To write a menu-driven python program to implement stack operation.
ALGORITHM:
Step-1: Start the program.
Step-2: Define a necessary functions to perform stack operations.
Step-3: Create an empty stack
Step-4: Display the menu for stack operations.
Step-5: Read the user choice.
Step-6: Perform the stack operation based on the user’s choice.
Step-7: Display the result.
Step-8: Stop the program.

Program:

Program:
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 item in stack")
else:
t=len(S)-1
print("(top)",end=' ')
while(t>=0):
print(S[t],"<==",end='')
t-=1
print()
S=[]
top=None
while True:
print("**** Stack Demonstration******")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Show stack")
print("5. Exit")
ch =int(input("Enter your choice:"))
if ch==1:
val=int(input("Enter item to push:"))
push(S,val)
elif ch==2:
val=pop(S)
if val=="underdflow":
print("stack is empty")
else:
print("\ndeleted item was :",val)
elif ch==3:
val=peek(S)
if val=="underfloe":
print("stack empty")
else:
print("top item:",val)
elif ch==4:
show(S)
elif ch==5:
print("bye")
break

OUTPUT:
Result:
Thus Python program has been executed and the output is verified
successfully.
4. MYSQL QUERIES – I

AIM:
To write a SQL Query for the following instruction given below the table.
ALOGORITHM:
Step1: Open the mysql command line client
Step2: Create a name of the database
Step3: Use the database name
Step4: Create a required table name
Step5: Fetch data into the concern table
Step6: Create a required SQL Query to retrieve the data from the table
Step7: Exit from the mysql
Consider the following WORKERS and DESIG. Write SQL commands for the statements 1 to 4 and
give outputs for SQL queries 5 and 6

TABLE WORKERS

W_ID FIRSTNAME LASTNAME ADDRESS CITY


102 SAM TONES 33 ELM ST. PARIS
105 SARAH ACKERMAN 440 U.S. 110 NEW YORK
144 MANILA SENGUPTA 24 FRIENDS STREET NEW DELHI
210 GEORGE SMITH 83 FIRST STREET HOWARD
255 MARY JONES 842 VINE AVE LOSANTIVILLE
300 ROBERT SAMUEL 9 FIFTH CROSS WASHINGTON
335 HENRY WILLIAMS 12 MOORE STREET BOSTON
403 RONNY LEE 121 HARRISON ST. NEW YORK
451 PAT THOMPSON 11 RED ROAD PARIS

TABLE : DESIG

W_ID SALARY BENEFITS DESIGNATION


102 75000 15000 MANAGER
105 85000 25000 DIRECTOR
144 70000 15000 MANAGER
210 75000 12500 MANAGER
255 50000 12000 CLERK
300 45000 10000 CLERK
335 40000 10000 CLERK
403 32000 7500 SALESMAN
451 28000 7500 SALESMAN
1. To display the content of workers table in ascending order of first name.
SELECT * FROM WORKERS ORDER BY FIRSTNAME;
2. To display the FIRSTNAME, CITY and TOTAL SALARY of all Clerks from the tables workers and
design, where TOTAL SALARY = SALARY + BENEFITS.
SELECT FIRSTNAME, CITY, SALARY + BENEFITS “TOTAL SALARY” FROM WORKERS;

3. To display the minimum SALARY among Managers and Clerks from the table DESIG.
SELECT MIN(SALARY) FROM DESIG WHERE DESIGNATION IN (MANAGER, CLERK);
4. Increase the BENEFITS of all Salesmen by 10% in table DESIG.
UDATE DESIG SET BENEFITS = BENEFITS + BENEFITS / 100 * 10;
5. SELECT FIRSTNAME, SALARY FROM WORKERS, DESIG WHERE DESIGNATION =
‘Manager’ AND WORKERS W_ID = DESIG.W_ID;

FIRSTNAME SALARY
SAM 75000
MANILA 70000
GEORGE 75000

6. SELECT DESIGNATION, SUM(SALARY) FROM DESIG GROUP BY DESIGNATION


HAVING COUNT (*) >=2 ;

DESIGNATION SUM (SALARY)


MANAGER 42500
CLERK 32000
SALESMAN 15000

Result:
Thus SQL Query has been executed and the output is verified successfully.
5. MYSQL QUERIES – II

AIM:
To write a SQL Query for the following instruction given below the table.
ALOGORITHM:
Step1: Open the mysql command line client
Step2: Create a name of the database
Step3: Use the database name
Step4: Create a required table name
Step5: Fetch data into the concern table
Step6: Create a required SQL Query to retrieve the data from the table
Step7: Exit from the mysql

Write SQL commands for 1 to 10 and write output for 11 to 14 on the basis of Teacher relation given
below :

TABLE TEACHER
NO. NAME AGE DEPARTMENT DATEOFJOIN SALARY SEX
1 JUGAL 34 COMPUTER 10/01/97 12000 M
2 SHARMILA 31 HISTORY 24/03/98 20000 F
3 SANDEEP 32 MATHS 12/12/96 30000 M
4 SANGEETA 35 HISTORY 01/07/99 40000 F
5 RAKESH 42 MAHTS 05/09/97 25000 M
6 SHYAM 50 HISTORY 27/06/98 30000 M
7 SHIV OM 44 COMPUTER 25/02/97 21000 M
8 SHALAKHA 33 MATHS 31/07/97 20000 F

7. To show all information about the teacher of history department.


MYSQL > SELECT * FROM TEACHER WHERE DEPARTMENT = ‘HISOTRY’ ;
8. To list the names of female teachers who are in Hindi department.
MYSQL > SELECT * FROM TEACHER WHERE DEPARTMENT = ‘HINDI’ AND SEX = ‘F’ ;
9. To list names of all teachers with their date of joining in ascending order.
MYSQL > SELECT * FROM TEACHER ORDER BY DATEOFJOIN
10. To display teacher’s Name, Salary, Age for male teacher only.
MYSQL > SELECT NAME, SALARY, AGE FROM TEACHER WHERE SEX = ‘M’ ;
11. To count the number of teachers with Age > 23.
MYSQL > SELECT COUNT (*) FROM TEACHER WHERE AGE > 23 ;
12. To insert a new row in the Teacher table with the following data : 9, “Vijay”, 26, “Computer”,
{13/05/95}, 35000, “M”
MYSQL > INSERT INTO TEACHER VALUES (9, “Vijay”, 26, “Computer”, ‘13/05/95’,
35000, “M”) ;
13. To show all information about the teachers in this table :
MYSQL > SELECT * FROM TEACHER ;
14. Add a new column named “Address”.
MYSQL> ALTER TABLE TEACHER ADD ADDRESS CHAR (40) ;
15. DISPLAY table in the alphabetical order to name :
SELECT * FROM TEACHER ORDER BY NAME ;

16. Display the age of the teachers whose name starts with ‘S’.
MYSQL > SELECT AGE FROM TEACHER WHERE NAME LIKE “S%’ ;
17. Select count (Distinct Department) from Teacher;
COMPUTER
HISTORY
MATHS
18. Select AVG (Salary) from Teacher where DateofJoin<’12/07/96’ ;
NULL
19. Select SUM (Salary) from Teacher where DateofJoin <’12/07/96’ ;
NULL
20. Select MAX(Age) from Teacher where Sex=”F” ;
35

Result:
Thus SQL Query has been executed and the output is verified successfully.
6. MYSQL QUERIES – III

AIM:
To write a SQL Query for the following instruction given below the table.
ALOGORITHM:
Step1: Open the mysql command line client
Step2: Create a name of the database
Step3: Use the database name
Step4: Create a required table name
Step5: Fetch data into the concern table
Step6: Create a required SQL Query to retrieve the data from the table
Step7: Exit from the mysql

Consider the following table INTERIORS and NEWONES and give the SQL Commands for
following questions.

TABLE INTERIORS
NO ITEMNAME TYPE DATE OF STOCK PRICE DISCOUNT
1. RED ROSE DOUBLE BED 23/02/02 32000 15
2. SOFT TOUCH BABY COT 20/01/02 9000 10
3. JERRY’S HOME BABY COT 19/02/02 8500 10
4. ROUGH WOOD OFFICE TABLE 01/01/02 20000 20
5. COMFORT ZONE DOUBLE BED 12/01/02 15000 20
6. JERRY LOOK BABY COT 24/02/02 7000 19
7. LION KING OFFICE TABLE 20/02/02 16000 20
8. ROYAL TIGER SOFA 22/02/02 30000 25
9. PARK SITTING SOFA 13/12/01 9000 15
10. DINE PARADISE DINING TABLE 19/02/02 11000 15

TABLE NEWONES
NO ITEMNAME TYPE DATE OF STOCK PRICE DISCOUNT
11. WHITE WOOD DOUBLE BED 23/03/03 20000 20
12. JAMES 007 SOFA 20/02/03 15000 15
13. TOM LOOK BABY COT 21/02/03 7000 10

1. To show all information about the Sofa from the INTERIORS table.
MYSQL > SELECT * FROM INTERIORS WHERE TYPE = ‘SOFA’ ;
2. To list the ITEMNAME which are priced at more than 10000 from the INTERIORS table?
MYSQL > SELECT ITEMNAME FROMINTERIORS WHERE PRICE > 10000 ;
3. To list ITEMNAME and type of those items, in which DATAOFSTOCK is before 22/01/02 from
INTERIORS table in descending order of ITEMNAME.
MYSQL > SELECT * FROM INTERIORS WHERE DATEOFSTOCK < ‘22/01/02’ ORDER BY
ITEMNAME
4. To display ITEMNAME and DATEOFSTOCK of those items, in which the DISCOUNT percentage
is more than 15 from INTERIORS table.
MYSQL> SELECT ITEMNAME, DATEOFSTOCK FORMINTERIORS WHERE DISCOUNT>15;

5. To count the number of items, whose TYPE is “Double Bed” from INTERIORS table?
MYSQL > SELECT COUNT (*) FROM INTERIORS WHERE TYPE = ‘DOUBLE BED’ ;
6. To insert a new row in the NEWONES table with the following data : 14, “TRUE Indian”, “Office
Table”, {28/03/03}, 15000?
INSERT INTO NEWONES VALUES (14, “TRUE INDIAN”, “OFFICE TABLE”, ‘28/03/03’,
15000, 20);
7. MYSQL> Select count (Distinct TYPE) from INTERIORS;
4
8. MYSQL> Select AVG (DISCOUNT) from INTERIORS where TYPE=”Baby cot”;
13
9. MYSQL> Select SUM (Price) from INTERIORS where DATEOF STOCK< {12/02/02};
43000
10. MYSQL> Select MAX (DISCOUNT) from INTERIORS;
25

Result:
Thus SQL Query has been executed and the output is verified successfully.
7. MYSQL QUERIES – IV

AIM:
To write a SQL Query for the following instruction given below the table.
ALOGORITHM:
Step1: Open the mysql command line client
Step2: Create a name of the database
Step3: Use the database name
Step4: Create a required table name
Step5: Fetch data into the concern table
Step6: Create a required SQL Query to retrieve the data from the table
Step7: Exit from the mysql

Consider the following BOOKS and ISSUED Write SQL commands for the statements 1 to 6 and give
outputs for SQL queries 7

TABLE BOOKS

Book_Id Book_Name Author_Name Publishers Price Type Qty.


C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5
F0001 The Tears William Hopkins First Publ. 650 Fiction 20
T0001 My First C++ Brain & Brooke EPB 350 Text 10
T0002 C++ Brainworks A.W. Rossaine TDH 350 Text 15
F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50

TABLE : ISSUED

Book_Id Quantity_Issued
T0001 4
C0001 5
F0001 2

1. To show Book name, Author name and Price of books of First Publ. publishers.
SELECT Book_Name, Author_Name, Price FROM Books WHERE Publishers = “First Publ.” ;
2. To list the names from books of Test type.
SELECT Book_Name FROM Books WHERE Type = “Text”;
3. To display the names and price from books in ascending order of their price.
SELECT Book_Name, Price FROM Books ORDER BY Price;
4. To increase the price of all books of EPB Publishers by 50.
UPDATE Books SET Price = Price + 50 WHERE Publishers = “EPB” ;

5. To display the Book_Id, Book_Name and Quantity_Issued for all books which have been issued.
(The query will require contents from both the tables.)
SELECT Books.Book_Id, Book_Name, Quantity_IssuedFROM Books, Issued
WHERE Books.Books_Id = Issued.Book_Id;
6. To insert a new row in the table Issued having the following data : “F0003”, 1
INSERT INTO Issued VALUES(“F0003”, 1);
7. Give the output of the following queries based on the above tables:
i) SELECT COUNT(*) FROM Books;
5
ii) SELECT MAX (Price) FROM Books WHERE Quantity >=15;
750
iii) SELECT Book_Name, Author_Name FROM Books WHERE Publishers = “EPB”;
Fast Cook Lata Kapoor
My First C++ Brain & Brooke
iv) SELECT COUNT (DISTINCT Publishers) FROM Books WHERE Price >=400;
1

Result:
Thus SQL Query has been executed and the output is verified successfully.
8. MYSQL QUERIES – V

AIM:
To write a SQL Query for the following instruction given below the table.
ALOGORITHM:
Step1: Open the mysql command line client
Step2: Create a name of the database
Step3: Use the database name
Step4: Create a required table name
Step5: Fetch data into the concern table
Step6: Create a required SQL Query to retrieve the data from the table
Step7: Exit from the mysql

Consider the following BANK and table CUSTOMER Write SQL commands for the statements i to vi
and give outputs for SQL queries vii and viii
TABLE BANK

Acc_no CName Bname Amount Dateofopen T_Transactions


1 Karan Bank of Baroda 15000 12/01/98 10
2 Puneet State Bank 25000 01/02/97 09
3 Anirban Oriental Bank 17000 15/07/99 05
4 Yatin Standard Charted 38000 10/02/99 11
5 Sunny State Bank 47000 06/02/98 15
6 Jayant Uco Bank 34000 10/08/98 07
7 Nikhil Bank of Baroda 56000 02/01/99 12
8 Tarun Oriental bank 22000 04/04/99 08
9 Jisha Uco Bank 34500 05/01/98 11

TABLE : CUSTOMER

Cno Tname TBank


1 Yatin Standard Charted
2 Sunny State Bank
3 Puneet Uco Bank
4 Nikhil Bank of Baroda
5 Varun Oriental bank
Write SQL queries for (i) to (iv) :
i) Display data for all customers whose transaction exists between 8 and 11.
SELECT * FROM BANK WHERE T_Transactions BETWEEN 8 And 11;
ii) Display data for all customers sorted by their Dateofopen.
SELECT * FROM BANK ORDER BY Dateofopen;
iii) To count the number of customers with amount <30000.
SELECT COUNT(*) FROM BANK WHERE Amount < 30000;
iv) List the minimum and maximum amount from the BANK.
SELECT MIN(Amount), MAX(Amount) FROM BANK;
v) To list Cname, Bname, Amount for all the clients whose amount is <20000.
SELECT Cname, Bname, Amount FROM BANK WHERE Amount < 20000;
vi) To display Acc_no, Cname, Bname, total transaction in reverse order of amount.
SELECT Acc_no, Cname, Bname, T_Transactions FROM BANK ORDER BY Amount DESC;
vii) Give the output of the following SQL commands:
a) SELECT AVG(Amount) FROM BANK WHERE Amount < 23000;
18000
b) SELECT MAX(Amount) FROM BANK WHERE Amount > 30000;
56000
c) SELECT SUM(T_Transactions) FROM BANK;
88
d) SELECT COUNT (DISTINCT Bname) FROM BANK;
5
viii) What will be the output of the following query:
SELECT CName, TBank FROM BANK, CUSTOMER WHERE CName = Tname;

Cname Tbank
Puneet Uco Bank
Yatin Standard Charted
Sunny State Bank
Nikhil Bank of Baroda

Result:
Thus SQL Query has been executed and the output is verified successfully.
9. Interface Python with MYSQL and extract data from table by using fetchone() function.

Aim:
To Write a python Program for connect Python with MYSQL and extract data from table

by using fetchone() function.

ALGORITHM:
Step 1 : Open the python script mode
Step 2 : Import mysql.connector package
Step 3 : Create a connection using connection object name as “mycon”.
Step 4 : Create a cursor using cursor object name as “mycursor”.
Step 5 : Execute a Query with the help of cursor object name
Step 6 : Count the total number of record exist from table
Step 7 : Extract data from table one by one using fetchone() function
Step 8: Stop the program
PROGRAM:

Result:
Thus Python program has been executed and the output is verified
successfully.
10. Interface Python with MYSQL and insert data into the table by using format() function

AIM:
To write a python Program for connect Python with MYSQL and insert data into the table by
using format() function
ALGORITHM:
Step 1 : Open the python script mode
Step 2 : Import mysql.connector package
Step 3 : Create a connection using connection object name as “mycon”.
Step 4 : Create a cursor using cursor object name as “mycursor”.
Step 5: Get required input from the user.
Step 6 : Insert the values with the help of format() function
Step 7 : Execute a query
Step 8 : Close the connection by using commit() function
Step 9: Stop the program

PROGRAM:
OUTPUT:

Result:
Thus Python program has been executed and the output is verified
successfully.

*******************

You might also like