0% found this document useful (0 votes)
6 views10 pages

Solved Python Questions

The document contains multiple sets of Python code examples for managing employee data, product information, and text processing. Set 1 implements a stack for employee IDs and names, Set 2 handles employee records using pickle for appending, deleting, searching, and updating, Set 3 writes and searches product data in a CSV file, Set 4 manages employee data in a CSV format with functions for inserting, updating, and displaying records, and Set 5 processes a text file to extract unique words. Each set includes user interaction through command-line inputs.

Uploaded by

ariz.rizvi.usa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Solved Python Questions

The document contains multiple sets of Python code examples for managing employee data, product information, and text processing. Set 1 implements a stack for employee IDs and names, Set 2 handles employee records using pickle for appending, deleting, searching, and updating, Set 3 writes and searches product data in a CSV file, Set 4 manages employee data in a CSV format with functions for inserting, updating, and displaying records, and Set 5 processes a text file to extract unique words. Each set includes user interaction through command-line inputs.

Uploaded by

ariz.rizvi.usa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Set 1

1.

edata = list()
def Push():
eid = int(input(“Enter employee id: “))
ename = input(“Enter employee name: “)
tup = (eid,ename)
[Link](tup)
def Pop():
if edata:
elm = [Link]()
print(“Popped element is”, elm)
else:
print(“Underflow”)
def Display():
if edata:
for i in range(1,len(edata)+1):
print(edata[-i]) # from top to bottom
else:
print(“Empty stack”)
while True:
choice = int(input(“Enter the choice \[Link] \[Link] \
[Link] \[Link]”))
if choice == 1:
Push()
elif choice == 2:
Pop()
elif choice == 3:
Display()
else:
break

Set 2
1.
import pickle

def append():
with open("[Link]", "ab") as f:
emp_id = int(input("Enter the employee ID:" ))
emp_name = input("Enter the employee name: ")
emp_salary = int(input("Enter the employee salary: "))
L = [emp_id, emp_name, emp_salary]
[Link](L,f)
def delete():
with open("[Link]", "rb") as f:
emp_id = int(input("Enter the employee ID:" ))
L = list()
while True:
try:
rec = [Link](f)
[Link](rec)
except EOFError:
break
with open("[Link]", "wb") as f:
for i in L:
if i[0] == emp_id:
continue
else:
[Link](i,f)
def search():
with open("[Link]", "rb") as f:
Found = 0
emp_id = int(input("Enter the employee ID:" ))
while True:
try:
rec = [Link](f)
if rec[0] == emp_id:
Found = 1
break
except EOFError:
break
if Found:
print("Found \n", rec)
else:
print("Not found")
def Update():
with open("[Link]", "rb") as f:
emp_id = int(input("Enter the employee ID:" ))
L = list()
while True:
try:
rec = [Link](f)
[Link](rec)
except EOFError:
break
with open("[Link]", "wb") as f:
for i in L:
if i[0] == emp_id:
salary = int(input("Enter the new salary: "))
i[2] = salary
[Link](i,f)
else:
[Link](i,f)
def Display():
with open("[Link]", "rb") as f:
while True:
try:
rec = [Link](f)
print(rec)
except EOFError:
break

while True:
choice = input("Enter your choice:\na. Append Data \nb.
Delete record of employeed based on emp_id \nc. Search for
emp_id if it does not exist display appropriate message \nd.
Update the salary of the employee based on emp_id \ne. Display
records \nf. Exit\n")
if choice == "a":
append()
elif choice == "b":
delete()
elif choice == "c":
search()
elif choice == "d":
Update()
elif choice == "e":
Display()
else:
break

Set 3
1.
import csv

def writecsv():
with open("[Link]", "w", newline ="") as f:
columns = ["pid", "pname", "cost", "quantity"]
writer = [Link](f)
[Link](columns)
ans = "y"
while ans == "y":
pid = input("Enter the product ID: ")
pname = input("Enter the product name: ")
cost = int(input("Enter the cost: "))
quantity = int(input("Enter the quantity: "))
dict = {"pid": pid , "pname": pname, "cost": cost,
"quantity": quantity}
[Link]([dict["pid"],
dict["pname"],dict["cost"], dict["quantity"]])
ans = input("Add more records? (y/n)... ")

def searchcsv():
with open("[Link]", "r", newline="") as f:
reader = [Link](f)
header = next(reader)
print(header)

for rec in reader:


if int(rec[3]) > 150:
print(rec)

writecsv()
searchcsv()

Set 4
1.
import csv

header = ["Emp No", "Name", "Date of Joining", "Age",


"Address", "Salary", "Commission"]
with open("[Link]", "w", newline = "") as f:
writer = [Link](f)
[Link](header)

def insert():
with open("[Link]", "a", newline = "") as f:
writer = [Link](f)
ans = "y"
while ans == "y":
rec = input("Enter EmpNo, Name, DOJ, Age, Address,
Salary, Commission separated by underscores (DD/MM/YYYY):
").split("_")
[Link](rec)
ans = input("Add more records? (y/n)... ")

def update():
with open("[Link]", "r", newline = "") as f:
reader = [Link](f)
data = []
next(reader)
for i in reader:
[Link](i)
with open("[Link]", "w", newline = "") as f:
writer = [Link](f)
[Link](header)
emp_no = int(input("Enter EmpNo: "))
for rec in data:
if int(rec[0]) == emp_no:
address = input("Enter new address: ")
rec[4] = address
[Link](rec)
else:
[Link](rec)

def display():
with open("[Link]", "r", newline = "") as f:
reader = [Link](f)
next(reader)
emp_no = int(input("Enter EmpNo: "))
for rec in reader:
if int(rec[0]) == emp_no:
print("Salary is:", rec[5])

def sum():
with open("[Link]", "r", newline = "") as f:
reader = [Link](f)
next(reader)
for rec in reader:
print("Name:", rec[1],"Total Salary:",int(rec[5])
+int(rec[6]))

while True:
choice = input("a. Insert data\nb. Update the address
based on Emp No\nc. Display salary based on Emp No\nd. Show
name and Total Salary\ne. Exit\n")
if choice == "a":
insert()
elif choice == "b":
update()
elif choice == "c":
display()
elif choice == "d":
sum()
else:
break

Set 5
1.
clean_text=""
with open("[Link]", "r") as f:
text = [Link]()
for char in text:
if [Link]():
clean_text += char
else:
clean_text += " "
words = clean_text.lower().split()
unique_words = list()
for word in words:
if word in unique_words:
continue
else:
unique_words.append(word)
print(unique_words)

You might also like