PYTHON PRACTICE QUESTIONS
1A) Write a menu driven program to perform the following tasks on a text file:
i) To count the number of vowels.
ii) To count the number of characters.
iii) To count the number of words starting with a capital letter.
Ans:
i)def count_vowels():
f=open("file.txt")
count=0
dat=f.read()
for i in dat:
if i.lower() in "aeiou":
count+=1
print("No of vowels in file:",count)
ii) def count_characters():
f=open("file.txt")
data=f.read()
print("No of characters in file:",len(data))
def count_capital():
f=open("file.txt")
count=0
dat=f.read().split()
for i in dat:
if i.isupper():
count+=1
else:
pass
print("No of words with capital letters :",count)
#to display the file
def display():
f=open("file.txt","r")
data=f.read()
print(data)
while True:
print("__Menu__")
print("1.Count number of characters\n","2.Count number of vowels\n","3.Count number of
words starting with capital\n",”4.Display”)
ch=int(input("Enter choice"))
if ch==1:
count_characters()
elif ch==2:
count_vowels()
elif ch==3:
count_capital()
elif ch==4:
display()
else:
break
1B) Write a menu-based program that adds an employee record containing the employee code,
employee name, department, salary, and manager and writes it to a csv file “Employee.csv”. The
program should also allow searching for an employee based on their employee code and display
their details.
Ans:
def add_record():
with open("Employee.csv", "a", newline="") as f:
a = []
pen = csv.writer(f)
while True:
Epcode = int(input("Enter employee code:"))
Epname = input("Enter Employee name:")
depart = input("Enter Department:")
sal = int(input("Enter salary:"))
man = input("Manager:")
l = [Epcode, Epname, depart, sal, man]
a.append(l)
ch = input("Do you want to enter more records (Y or N): ")
if ch.lower() == "n":
break
pen.writerows(a)
def search(a):
# Open the file in read mode
found = False
with open("Employee.csv", "r") as f:
dat = csv.reader(f)
for i in dat:
if int(i[0]) == a:
print(i)
found = True
break
if not found:
print("Employee not found")
def display():
f=open("pass.csv","r")
data=csv.reader(f)
for i in data:
print(i)
while True:
print("_Menu_")
print("1. Add Record\n2. Search\n3.Display")
ch = int(input("Enter choice: "))
if ch == 1:
add_record()
elif ch == 2:
emcode = int(input("Enter employee code: "))
search(emcode)
elif ch==3:
display()
else:
break
1C) write a menu driven programme that creates a binary file "Employee.dat" containing the
following [Epno,Epname,city] . Use this to ask the user for a employee number and delete that
record.
Ans:
import pickle
def add_data():
f=open("Employee.dat","wb")
a=[]
while True:
Epno=int(input("Enter employee no:"))
Epname=input("Enter Employee name:")
city=input("Enter City:")
l=[Epno,Epname,city]
a.append(l)
ch=input("Do you want to enter more records Y or N")
if ch== "n":
break
else:
continue
pickle.dump(a,f)
f.close()
def Del(a):
f=open("Employee.dat","rb+")
n=a
l1=[]
try:
while True:
dat=pickle.load(f)
for i in dat:
if i[0]==n:
pass
else:
l1.append(i)
except:
f.close()
f=open("Employee.dat","wb+")
f.seek(0)
pickle.dump(l1,f)
f.close()
def display():
f=open("Employee.dat","rb")
try:
data=pickle.load(f)
print(data)
except:
f.close()
while True:
print("__Menu__")
print("1.Add Data\n","2.Delete Record\n",”3.display”)
ch=int(input("Enter choice"))
if ch==1:
add_data()
elif ch==2:
emno=int(input("Enter employee code"))
Del(emno)
Elif ch==3:
display()
else:
break
1D) Write a menu driven program to perform all operations of stack.
Ans:
stack=[]
def push(x):
stack.append(x)
def pop():
if len(stack)==0:
print("Empty Stack")
else:
stack.pop()
def display():
if len(stack)==0:
print("Empty Stack")
else:
for i in stack[::-1]:
print(i)
def peek():
print(stack[-1])
while True:
print("Menu\n1.Push\n2.Pop\n3.Display\n4.Peek")
ch=int(input("Enter your choice:"))
if ch==1:
n=int(input("enter any no.:"))
push(n)
elif ch==2:
pop()
elif ch==3:
display()
elif ch==4:
peek()
else:
break
To create both tables and perform the given sql queries
Customer Table:
Company table:
A1)Display company names whose price is between 30,000 and 50,000.
A2)Display names of customer in alphabetical order
A3)Decrease the price by 1000 for customer whose name ends with “n”. Also display the
updated customer list
A4)Display the city name and the number of records in each city
B1)Display the company names where price is less than 30000
B2)Find the customber name and company product where the price is between 20,000 and
40,000
B3)Display names of customer in alphabetical order.
B4)Increase the price by 1000 for customber whose name contains secound alphabet as “o”.Also
display the updated customer table.
C1) Display the names whose price is less than 30000
C2) Display the cities where SONY has its office
C3) Increase the price by 1000 for customers whose names ends with “AN”. Also display the
updated table
C4)Display the company name and total quantity in each company
1D)Display the company names whose price is more than 30000.
2D)Find the details of customers who
purchased mobile products.
3D)Increase the price by 100 for customers whose name contains ‘H’. Also
display the updated customer table.
4D) Display the city name and the number of records in each city.