[go: up one dir, main page]

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

Computer Science Term2 Practical Programs

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

COMPUTER SCIENCE TERM-2 PRACTICAL PROGRAMS

Ex.no.16
Write a python program to maintain book details like book code, book title and price using
stacks data structures? (implement push(), pop() and traverse() functions)
AIM: To write a python program to maintain book details like book code, book title and price
using stacks data structures

Source Code:
"""
push
pop
traverse
"""
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 ")

1
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("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")
2
OUTPUT:
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 1
Enter bcode 101
Enter btitle computer science
Enter price 600
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 1
Enter bcode 201
Enter btitle English
Enter price 100
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 2
poped element is
bcode 201 btitle English price 100

3
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 3
('101', 'computer science', '600')
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 4
End

4
Ex.no.17
Write a Python program to implement a stack using list (PUSH & POP Operation on Stack).

AIM:
To Write a Python program to implement a stack using list (PUSH & POP Operation on Stack).

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 occurs'
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 occurs'
else:
top=len(s)-1
return s[top]

def show(s):
if isEmpty(s):
print('no item found')
5
else:

t=len(s)-1
print('(TOP)',end='')
while(t>=0):
print(s[t],'<==',end='')
t=t-1
print()

s=[]
top=None
while True:
print('****** STACK IMPLEMENTATION USING LIST ******')
print('1: PUSH')
print('2: POP')
print('3: PEEK')
print('4: Show')

print('0: Exit')
ch=int(input('Enter choice:'))
if ch==1:
val=int(input('Enter no to push:'))
push(s,val)
elif ch==2:
val=pop(s)
if val=='Underflow':
print('Stack is empty')
else:
print('\nDeleted item is:',val)
elif ch==3:
val=peek(s)
if val=='Underflow':
print('Stack is empty')
else:
print('\nTop item is:',val)
elif ch==4:
show(s)
elif ch==0:

6
print('Bye')
break

Output is:
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:1
Enter no to push: 23
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:4
(TOP)23 <==
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:1
Enter no to push:23
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:2

7
Deleted item is: 23
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:3

Top item is: 23


****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: POP
3: PEEK
4: Show
0: Exit
Enter choice:0
Bye

8
Ex.no.18
Write a python program using function PUSH (Arr), where Arr is a list of numbers. From this list
push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message.
AIM:
To write a python program using function PUSH (Arr), where Arr is a list of numbers. From
this list push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if
it has at least one element, otherwise display appropriate error message.
SOURCE CODE:

def isEmpty(Arr):
if len(Arr)==0:
return True
else:
return False

def push(Arr,item):
if item%5==0:
Arr.append(item)
top=len(Arr)-1

def show(Arr):
if isEmpty(Arr):
print('No item found')
else:
t=len(Arr)-1
print('(TOP)',end='')
while(t>=0):
print(Arr[t],'<==',end='')
t=t-1
print()

Arr=[]
top=None
while True:
print('****** STACK IMPLEMENTATION USING LIST ******')
print('1: PUSH')
9
print('2: Show')
print('0: Exit')
ch=int(input('Enter choice:'))
if ch==1:
val=int(input('Enter no to push:'))
push(Arr,val)
elif ch==2:
show(Arr)
elif ch==0:
print('Bye')
break
Result:
Thus, the above Python program is executed successfully and the output is
verified.
Output is:
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: Show
0: Exit
Enter choice:1
Enter no to push:23
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: Show
0: Exit
Enter choice:1
Enter no to push:20
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: Show
0: Exit
Enter choice:2
(TOP)20 <==
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: Show
0: Exit
Enter choice:0
By
10
EX.NO: 19

DATE:

CREATING APYTHON PROGRAM TOINTEGRATE MYSQL WITH PYTHON

(INSERTING RECORDS AND DISPLAYING RECORDS)

AIM:

To write a Python Program to integrate MYSQL with Python by


inserting records to Emp table and display the records.

SOURCE CODE:

Result:
Thus, the above Python program is executed successfully and the output is
verified.

11
OUTPUT:
Python Executed Program Output:

SQL OUTPUT:

12
EX.NO: 20

DATE:

CREATING APYTHON PROGRAM TOINTEGRATE MYSQL WITH PYTHON

(SEARCHING AND DISPLAYING RECORDS)

AIM:
To write a Python Program to integrate MYSQL with Python to search an
Employee using EMPID and display the record if present in already
existing table EMP, if not display the appropriate message.

SOURCE CODE:

Result:
Thus, the above Python program is executed successfully and the output is
verified.

13
OUTPUT:
Python Executed Program

Output: RUN -1:

Run – 2:

SQL OUTPUT:

OUTPUT -1:

OUTPUT -2:

14
Sql exercise -1
a. To show all information about the students of the history department
b. To list the names of female students who are in hindi department.
c. To list the names of all the students with their date of admission in descending order.
d. To display the students name, age, fee, for male students only.
e. To count the number of students with age less than 20.

TABLE STUDENT
s.no Name Age Dept Date of admin Fee gend
er
1. Panbaj 24 Computer 10/01/1999 120
Shalini 21 History 24/03/1998 200
3. Sanjay 20 Hindi 12/12/1996 300
Sudha 19 History 01/7/1999 400
Rakesh 18 Hindi 05/09/1991 250
Shaku 19 History 27/06/1997 300
Surya 22 Computer 25/02/1997 210
Shika 23 Hindi 31/07/1997 200

Note: Draw the table on left hand side


Answers
a. SELECT * from student where DEPT=”HISTORY”;
b. SELECT NAME FROM STUDENT WHERE GENDER =’F’ AND DEPT =’HINDI’;
c. SELECT NAME, FEE, AGE FROM STUDENT WHERE GENDER=’M’;
d. SELECT NAME ,DATE OF ADMIN FROM STUDENT ORDER BY DATEOFADMIN;
e. SELECT COUNT(*) FROM STUDENTS WHERE AGE<20;

15
sq1 Exercise-2
a. To display dcode and description of each classes in ascending order dcode.
b. to display the details of all the dresses which have launchdate in between 05-dec-07 to 20-
jun-08
c. To display the average price of all the dress which are made up of material with mcode as
M003.
d. to display material wise higher and latest price of dresses from dress table.
e. To display the description and type from table DRESS and

MATERIAL. TABLE DRESS

DCODE DESCRIPTION PRICE MCODE LAUNCH DATE

10001 FORMAL SHIRT 1250 M007 06-Jun-08


10020 FROCK 750 M004 06-Sep-07
10012 INFORMAL SHIRT 1150 M002 06-Jun-08
10090 TULIP SHIRT 850 M003 31-Mar-07
10019 EVENING GOWN 850 M003 20-Oct-08
10023 PENCIL SHIRT 1250 M003 20-Oct-08
10081 SLACHS 850 M003 09-Mar-08
10007 FORMAL PANT 1450 M001 20-Oct-08
10008 INFORMAL PANT 1400 M002 07-Apr-09
10024 BABY TOP 850 M003 06-Jun-08

TABLE MATERIAL
MCODE TYPE
M001 TERELENE
M002 COTTON
M003 POLYSTER
M004 SILK

16
ANSWERS.
a.) SELECT DCODE, DESCRIPTION FROM DRESS ORDER BY DCODE;
b.) SELECT * FROM DRESS WHERE LAUNCH DATE BETWEEN ’05-DEC-07’ AND ’20-JUN-08’
c.) SELECT AVG(PRICE) FROM DRESS WHERE DCODE =”M008”;
d.)SELECT MCODE, MAX(PRICE),MIN(PRICE)FROM DRESS GROUP BY MCODE;
e.) SELECT DESCRIPTION, TYPE FROM DRESS D, MATERIAL WHERE D.CODE=M.CODE;

17
sq1 Exercise-3
a.) To display the details of those consumer whose address is delhi
b. To display the details of stationary whose price is in the range of 8 to 15.
c. To display the consumer name, address from table consumer and company and price from table
stationary with their corresponding matching S_ID.
d.) To increase the price of all the stationary by 2.
e. add a new record in consumer table with following details.
(17,”FAST WRITER”, “MUMBAI”,”GPO1”);

TABLE: STATIONARY
STATIONARY
S_ID COMPANY PRICE
NAME
DP01 DET PEN ABC 10
PL02 PENCIL XYZ 6
ER05 ERASER XYZ 7
PL01 PENCIL CAN 5
GP02 GELPEN ABC 15

TABLE : CONSUMER
CONSUMER
C_ID ADDRESS S_ID
NAME
1 GOOD EARNER DELHI PL01
6 WHITE WELL MUMBAI GP02
12 TOPPER DELHI DP01
15 WRITE 4 DRAW DELHI PL02
16 MOTIVATION BANGALORE PL01

ANSWERS.
a.) SELECT * FROM CONSUMER WHERE ADDRESS=”DELHI”;
b.) SELECT * FROM STATIONARY WHERE PRICE BETWEEN 8 AND 15;
c.) SELECT CONSUMER NAME, ADDRESS ,COMPANY, PRICE FROM CONSUMER A,
STATIONARY B WHERE A.S_ID=B.S_ID;
d.) UPDATE STATIONARY SETPRICE=PRICE+2;
e.) INSERT INTO CONSUMER VALUES(17,”FAST WRITER”,”MUMBAI’,”GP01”);

18
19

You might also like