You are on page 1of 9

Q1) Write a Python program to implement a stack using a list data-structure. (without using functions).

CODE:

glassStack = list()

def isEmpty(glassStack):

if len(glassStack)==0:

return True

else:

return False

def opPush(glassStack,element):

glassStack.append(element)

def top(glassStack):

if isEmpty(glassStack):

print('Stack is empty')

return None

else:

x =len(glassStack)

element=glassStack[x-1]

return element

ans='Y'

while ans=='Y' or ans=='y':

element=input('Enter value to enter in stack...')

print('Pushing element',element)

opPush(glassStack,element)

print('top element is',top(glassStack))

ans=input(' Do you want to continue...')

OUTPUT:
Q2) Write a menu driven program to perform the following operations on a Stack for the book details
(bookNo, bookName) using function:

a. Push operation

b. Pop operation

c. Peek opertion

d. Display all elements from a stack

CODE:

s=[]

while (True):

print('\nMENU DRIVEN PROGRAM')

print ('\n1. PUSH\n')

print ('2. POP \n')

print ('3. PEEK\n')

print ('4. DISPLAY\n')

choice=int(input('Enter your choice: '))

if (choice==1):

a=input('Enter any number :')

s.append(a)

print ('Number added to stack\n')

elif (choice==2):

if (s==[]):

print ('Stack Empty\n')

else:

print ('Deleted element is : ',s.pop())

elif (choice==3):

print('Topmost element is',s[-1])

elif (choice==4):

if (s==[]):

print ('Stack Empty\n')

continue

l=len(s)

print('Values in Stack..\n')

for i in range(l-1,-1,-1):
print (s[i])

else:

print('Wrong Input')

OUTPUT:
Q3) BCCI has created a dictionary containing top players and their runs as key value pairs of cricket team.
Write a program, with separate user defined functions to perform the following operations:

● Push the keys (name of the players) of the dictionary into a stack, where the corresponding value (runs) is
greater than 49.

● Pop and display the content of the stack.

For example:

If the sample content of the dictionary is as follows:

SCORE={‘KAPIL’:40, ‘SACHIN’:55, ‘SAURAV’:80,’RAHU’:35, ‘YUVRAJ:10, }

The output from the program should be: SACHIN SAURAV YUVRAJ

CODE:

SCORE={'KAPIL':40,'SACHIN':55,'SAURAV':80,'RAHUL':35,'YUVRAJ':110}

def push(s,n):

s.append(n)

def pop(s):

if s!=[]:

return s.pop()

else:

return none

st=[]

for k in SCORE:

if SCORE[k]>=49:

push(st,k)

while True:

if st!=[]:

print(pop(st),end=' ')

else:

break

OUTPUT:
Q4) Write a Python code to connect a MySQL database namely SHOP and use the following table Stationery
and Consumer and send SQL commands to the database and retrieve the records based on the queries given
below:

(i) To display the details of those consumers whose Address is Delhi.

Ans:- select * from Consumer where Address=’Delhi’;


(ii) To display the details of Stationery whose Price is in the range of 8 to 15. (Both Value included)

Ans:- select * from Stationery where Price>=8 and Price<=15;

(iii) To display the ConsumerName, Address from Table Consumer, and Company and Price from table
Stationery, with their corresponding matching S_ID.

Ans:- select * ConsumerName, Address, Company ,Price from Consumer,Stationery where


Stationery.S_ID=Consumer.S_ID;

(iv) Display the maximum and minimum price for each company.

Ans:- select min(Price) and max(Price) from Stationery;


Q5) Using Python MySQL Connector, create a student table and insert the following data.

Using Python MySQL Connector, execute the following SQL commands on the above

student table & display appropriate message.

i. Add a new attributes - Sec char(3) to the table

ii. UPDATE table to add Sec as A for all class 3 students

iii. ORDER By to display data in descending order

iv. GROUP BY and find the min, max, sum, count and average mark

v. DELETE to remove tuple(s) belonging to class 4

Creating Table:-

Inserting Data:-

i)
ii)

iii)

iv)
v)

You might also like