Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

INDEX

S. No. Date Contents Page No.

Python program to implement stack


1. 07/01/2022 1 to 4
operation.
Write a program to implement a stack
2. 12/01/2022 for the employee details (empno, 5 to 8
name).
To implement stack for inserting
3. 21/01/2022 9 to 13
criminal records in it using list. .

To create Table in mysql having data


4. 29/01/2022 14
belonging to a particular institutions.

To create a table student in mysql as


5. 07/02/2022 15
shown in book on page 534
Given a table student as above. Write
a query to display name, sex, and
aggregate/5 and name this
6. 11/02/2022 computation as percentage. 16
Count number of students in student
table. Display maximum aggregate in
student table
Given the following tables for a
7. 18/02/2022 17 to 20
database LIBRARY:

Creating Table members (page-589)


8. 24/02/2022 21 to 24
Perform all operation
Write a Python program that displays
first three rows fetched from student1
9. 02/03/2022 25
table of MySQL database
“devogmotors”.
Write a python program that displays
first three rows fetched from student1
10. 07/03/2022 table of MySQL database 26
“devogmotors”. Use pymysql to
connect with the database.
Question 1: Python program to implement
stack operation.
Program
def push():
if len(stack)==n:
print("Stack Overflow...")
else:
element=input("Enter the element:")
stack.append(element)
print("Element Added Successfully...")

def pop_element():
if not stack:
print("Stack Underflow...")
else:
e=stack.pop()
print("Element Popped Succesfully...",e)

def peek():
if len(stack)==0:
print("Stack Underflow...")
else:
print("The Peek Element is",stack[-1])

def display():
if len(stack)==0:
print("Stack Underflow...")
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i])

#--main--

stack=[]
n=int(input("Enter the limit of stack:"))
while True:
print()
choice=int(input("Select the operation\n1->Push\n2->Pop\n3->Peek\n4->Display
All\n5->Exit\nEnter Your Choice:"))
if choice==1:
push()
elif choice==2:
pop_element()
elif choice==3:
peek()
elif choice==4:
display()
elif choice==5:
break
else:
print("Please, Enter The Valid operation!!")
Output:

1^ 2^
3^ 4^
Question 2: Write a program to implement a
stack for the employee details (empno, name).
Program
#Employee
stk=[]
top=-1
de=0
a=input("Please Enter Your Name:")
def line():
print('~'*100)

def isEmpty():
global stk
if stk==[]:
print("Stack is Empty!!")
else:
None

def push():
global stk
global top
empno=int(input("Enter the employee number to push:"))
ename=input("Enter the employee name to push:")
stk.append([empno,ename])
top=len(stk)-1
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])

def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
stk.pop()
top=top-1

def main():
while True:
line()
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter Your choice:"))
if ch==1:
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
elif ch==4:
de=10
else:
print("Invalid Choice")

if len(a)==0:
de=0
else:
main()

Output:
Question 3: To implement stack for
inserting criminal records in it using list.
Program
stk=[]
top=-1
de=0
a=input("Please Enter The Jail Name:")
def line():
print('~'*100)

def isEmpty():
global stk
if stk==[]:
print("No prisoner found!!")
else:
None

def push():
global stk
global top
pri_no=input("Please, Enter the Prisoner number:")
pri_name=input("Enter the Prisoner name to push:")
stk.append([pri_no,pri_name])
top=len(stk)-1

def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])

def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
x=stk.pop()
print("Criminal Popped is:",x)
top=top-1

def peek():
global stk
if len(stk)==0:
print("Stack Underflow...")
else:
print("The Last Criminal Added in",a,"is",stk[-1])

def main():
while True:
line()
print("1. Add Criminal")
print("2. Delete Criminial Record")
print("3. Display All Criminal")
print("4. Show Last Criminal Added")
print("5. Exit")
ch=int(input("Enter Your choice:"))
if ch==1:
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
elif ch==4:
peek()
elif ch==5:
break
else:
print("Invalid Choice")

if len(a)==0:
de=0
else:
main()
Output:
Question 4: To create Table in mysql
having data belonging to a particular
institutions.
Program 5: To create a table student in
mysql as shown in book on page 534.
Question 6: Given a table student as above.
Write a query to display name, sex, and
aggregate/5 and name this computation as
percentage. Count number of students in
student table. Display maximum aggregate in
student table.
Question 7: Given the following tables for a database
LIBRARY:

Write SQL queries for (a) to (f) :


(a) To show Book name, Author name and Price of books of First Publ. publishers.
(b) To list the names from books of Text type.
(c) To display the names and price from books in ascending order of their price.
(d) To increase the price of all books of EPB Publishers by 50.
(e) 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.)
(f) To insert a new row in the table Issued having the following data : "FO003", 1
(g) Give the output of the following queries based on the above tables :
(i) SELECT COUNT(*) FROM Books ;
(ii) SELECT MAX(Price) FROM Books WHERE Quantity >=15 ;
(iii) SELECT Book_Name, Author_Name FROM Books WHERE Publishers = "EPB" ;
(iv) SELECT COUNT (DISTINCT Publishers) FROM Books WHERE Price > 400;

Answer:
(a)-
(b)-

(c)-

(d)-
(e)-

(f)-

(g)-
(i)
(ii)

(iii)

(iv)
Question 8: Creating Table members (page-589)
Perform all operation

>ALTER table to add new attributes / modify data type / drop attribute
**Adding new Attribute Salary:
**Modifying data type and dropping attribute:

>UPDATE table to modify data

>ORDER By to display data in ascending / descending order

> DELETE to remove tuple(s)

>GROUP BY and find the min, max, sum, count and average
>Joining of two tables.
Question 9:- Write a Python program that
displays first three rows fetched from student1
table of MySQL database “devogmotors”.
(Note: User is “root” and passsword is “Devendragupta@2022”)

Program:
import mysql.connector as sqltor
mycon=sqltor.connect(host="localhost",user="root",password="De
vendragupta@2022",database="devogmotors")
if mycon.is_connected()==False:
print('Error while connecting to Mysql Database')
cursor=mycon.cursor()
cursor.execute("select * from student1")
data=cursor.fetchmany(3)
count=cursor.rowcount
for row in data:
print(row)
mycon.close()
Output:
Question 10:Write a python program that displays first
three rows fetched from student1 table of MySQL database
“devogmotors”. Use pymysql to connect with the database.
(Note: User is “root” and passsword is “Devendragupta@2022”)

Program:
import pymysql as pym
mycon=pym.connect("localhost","root","Devendragupta@2022","d
evogmotors")
cursor=mycon.cursor()
cursor.execute("select * from student1")
data=cursor.fetchmany(3)
for row in data:
print (row)
mycon.close()
Output:

You might also like