DAV PUBLIC SCHOOL, JHARSUGUDA
CLASS XII – COMPUTER SCIENCE
PROGRAMS FOR PRACTICAL RECORD (2nd Part)
19. Write a Python program to perform stack operation.
Answer:
# stack implementation
def isempty(stk): # stk[50,60]
if stk==[]:
return True
else:
return False
def push(stk,item): # stk = [50] and item = 60
[Link](item) # stk=[50,60]
top=len(stk)-1 # top = 2-1 = 1
def pop(stk):
if isempty(stk):
return "Underflow"
else:
item=[Link]()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
return "Underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk): # stk=[50,60]
if isempty(stk):
print("stack empty")
else:
top=len(stk)-1 # len(stack) =2-1=1
print(stk[top],"<-- top")
for a in range(top-1,-1,-1): # 0, -1, -1
print(stk[a])
#_main_
stack=[]
top=None
while True:
print("Stack Operations")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit")
ch=int(input("Enter your choice")) #1 #4
if ch==1:
item=int(input("Enter item")) # item=50
push(stack,item) # calling - push and arguments - stack and item
elif ch==2:
item=pop(stack) # calling - pop and arguments - stack
if item=="Underflow":
print("Stack is empty")
else:
print("deleted item is",item)
elif ch==3:
item=peek(stack) # calling - peek and arguments - stack
if item=="Underflow":
print("Underflow ! stack is empty")
else:
print("Topmost item is",item)
elif ch==4:
display(stack) # calling display and arguments - stack
elif ch==5:
break
else:
print("invalid choice! Try again")
INPUT/OUTPUT:
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice1
Enter item100
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice1
Enter item200
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice4
200 <-- top
100
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice3
Topmost item is 200
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice2
deleted item is 200
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice4
100 <-- top
Stack Operations
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice5
20. Write a python program using function to write three records and
read the file [Link] and display the names of all those
Destination whose number of characters in the Destination are more
than 6. A CSV file [Link] contains data in the following order:
TID,DESTINATION,DAYS,FARE
Answer :
import csv
f=open("[Link]","w",newline='')
H=[Link](f)
[Link](['TID','DESTINATION','DAYS','FARE'])
while True:
ID=input("Enter the ID :")
country=input("Enter Country: ")
days=int(input("Enter the number of days"))
price=float(input("Enter price: "))
lst=[ID,country,days,price]
[Link](lst)
ch=input("more data: ")
if ch=='n' or ch=='N':
break
[Link]()
with open("[Link]","r") as f:
rec=[Link](f)
next(rec)
found=0
for i in rec:
if len(i[1])>6:
print(i)
found+=1
if found==0:
print('record not found')
INPUT :
Enter the ID :101
Enter Country: India
Enter the number of days3
Enter price: 10000
more data: y
Enter the ID :102
Enter Country: Srilanka
Enter the number of days5
Enter price: 5000
more data: y
Enter the ID :103
Enter Country: USA
Enter the number of days4
Enter price: 8000
more data: n
OUTPUT:
['102', 'Srilanka', '5', '5000.0']
21. Write a python program using function to search and display the
record of that Destination from the file [Link] which has
maximum Fare. A CSV file [Link] contains data in the
following order: TID,DESTINATION,DAYS,FARE. Write a python
Answer:
import csv
f=open("[Link]","w",newline='')
H=[Link](f)
[Link](['TID','DESTINATION','DAYS','FARE'])
while True:
ID=input("Enter the ID :")
country=input("Enter Country: ")
days=int(input("Enter the number of days"))
price=float(input("Enter price: "))
lst=[ID,country,days,price]
[Link](lst)
ch=input("more data: ")
if ch=='n' or ch=='N':
break
[Link]()
with open("[Link]","r") as f:
rec=[Link](f)
next(rec)
max1=-1
for i in rec:
if float(i[3])>max1:
max1=float(i[3])
r=i
print(r)
INPUT:
Enter the ID :101
Enter Country: India
Enter the number of days3
Enter price: 10000
more data: y
Enter the ID :102
Enter Country: Srilanka
Enter the number of days5
Enter price: 5000
more data: y
Enter the ID :103
Enter Country: USA
Enter the number of days4
Enter price: 8000
more data: n
OUTPUT :
['101', 'India', '3', '10000.0']
DATABASE NAME - EMPLOYEE
TABLE - EMPPERSONAL
empid firstname lastname address city
101 George Smith 83 First Street Howard
105 Marry Jones 842 Vine Ave Lusantville
152 sam Alkerman 440 Peter Street London
215 Manila Sengupta 24 Friends Street New Delhi
244 Robert Samuel 121 Marshall Street Washington
300 Henry Williams 12 Moore Street Boston
400 Rachel Lee Renault Street New York
441 Peter Thropson 11 Red Road Paris
442 Manoj Singh M B Road Mumbai
TABLE - EMPSALARY
empid salary benefit designation
101 75000 15000 Manager
105 65000 15000 Manager
152 80000 25000 Director
215 75000 12500 Manager
244 50000 12000 Clerk
300 45000 10000 Clerk
400 32000 2500 Clerk
441 28000 2500 Salesman
SQL COMMANDS
1. To display first name, last name, address and city of all employee living
in paris from the table emppersonal.
Ans : select firstname,lastname,address,city from emppersonal where city ='paris';
2. To display the content of emppersonal table in descending order of first
name.
Ans : select * from emppersonal order by firstname desc;
3. To display the first name, last name and total salary of all managers
from the tables emppersonal and empsalary where total salary is
calculated as salary + benefits.
Ans : select firstname,lastname,salary+benefit as 'Total Salary' from
emppersonal p, empsalary s where [Link]=[Link] and
designation='Manager';
4. To count the no. of employees with the salary>40000.
Ans : select count(*) from empsalary where salary>40000;
5. To insert a new row in the emppersonal table with following data :
(442, Manoj, Singh, M B Road, Mumbai )
Ans :insert into emppersonal values('442','Manoj','Singh','M B
Road','Mumbai') ;
6. To display the employee information whose first name starts with letter
M from the table emppersonal.
Ans : select * from emppersonal where firstname like 'm%';
Python connectivity with Mysql
import [Link]
con=[Link](host='localhost',user='root',password='12
3456',database='employee')
if con.is_connected():
print("Connection created")
cur=[Link]()
[Link]("select * from emppersonal where firstname='Marry'")
data=[Link]()
print(data)
Output:
Connection created
[('105', 'Marry', 'Jones', '842 Vine Ave', 'Lusantville')]
[Link]("select count(distinct designation) from empsalary")
data=[Link]()
for i in data:
print(i)
Output:
(4,)
[Link]("select min(salary)from empsalary where
designation='clerk'")
data=[Link]()
for i in data:
print(i)
Output:
(Decimal('32000'),)
[Link]("select sum(benefit) from empsalary where
designation='clerk'")
data=[Link]()
for i in data:
print(i)
Output:
(Decimal('24500'),)
[Link]("select avg(salary) from empsalary where benefit>12500")
data=[Link]()
for i in data:
print(i)
Output:
(Decimal('73333.3333'),)
[Link]("select firstname,salary from empsalary S,emppersonal P
where [Link]='manager' and [Link]=[Link]")
data=[Link]()
for i in data:
print(i)
Output:
('George', Decimal('75000'))
('Marry', Decimal('65000'))
('Manila', Decimal('75000'))
[Link]("select designation,sum(salary)from empsalary group by
designation having count(*)>2 ")
data=[Link]()
for i in data:
print(i)
Output:
('Manager', Decimal('215000'))
('Clerk', Decimal('127000'))
[Link]("select sum(benefit) from empsalary group by designation
having count(*)>2 ")
data=[Link]()
for i in data:
print(i)
Output:
(Decimal('42500'),)
(Decimal('24500'),)
[Link]("select firstname from emppersonal where firstname like
'_a%' ")
data=[Link]()
for i in data:
print(i)
Output:
('Marry',)
('sam',)
('Manila',)
('Rachel',)
('Manoj',)
---- END ---