Program 6
Q. Create a CSV file with empno,name and salary. Search for a given empno and
display the name,salary and if not found display appropriate message.
Ans.
import csv
with open('[Link]','w') as csvfile:
mywriter=[Link](csvfile,delimiter=',')
ans='y'
while [Link]()=='y':
eno=int(input("Enter Employee Number:"))
name=input("Enter Employee Name:")
salary=int(input("Enter Employee Salary:"))
[Link]([eno,name,salary])
ans=input("Add More?(Y)")
ans='y'
with open('[Link]','r') as csvfile:
myreader=[Link](csvfile,delimiter=',')
while ans=='y':
found=False
e=int(input("Enter Employee Number to search:"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("NAME:",row[1])
print("SALARY:",row[2])
found=True
break
if not found:
print("EMPNO NOT FOUND")
ans=input("Search More?(Y)")
Output:
Enter Employee Number:101
Enter Employee Name:Ajay
Enter Employee Salary:10000
Add More?(Y)Y
Enter Employee Number:102
Enter Employee Name:Vijay
Enter Employee Salary:20000
Add More?(Y)n
Enter Employee Number to search:101
NAME: Ajay
SALARY: 10000
Search More?(Y)y
Enter Employee Number to search:103
EMPNO NOT FOUND
Search More?(Y)n
Program 8
Q. Write a Python program to implement a stack using a list data-structure.
Ans.
# Implementation of List as stack
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 :")
[Link](a)
elif choice==2:
if s==[]:
print("Stack Empty")
else:
print("Deleted element is : ",[Link]())
elif choice==3:
l=len(s)
for i in range(l-1,-1,-1): # to display elements from last element to first
print(s[i])
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
Output:
1. PUSH
2. POP
3. Display
Enter your choice:1
Enter any number :5
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:1
Enter any number :t
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:1
Enter any number :66
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:1
Enter any number :88
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:3
88
66
t
5
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:2
Deleted element is : 88
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:2
Deleted element is : 66
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choice:3
t
5
Do you want to continue or not? n