GR 12 CS Practical Programs (11-15)
GR 12 CS Practical Programs (11-15)
PROBLEM STATEMENT:
Create a CSV file by entering user-id and password, read and search the
password for given user_id.
AIM:
To write a Python program to create a CSV file and do read and search
operation on the file.
SOURCE CODE:
import csv
def create_csv():
with open('user_passwords.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["user_id", "password"])
while True:
user_id = input("Enter user-id (or 'quit' to stop): ")
if user_id.lower() == 'quit':
break
password = input("Enter password: ")
writer.writerow([user_id, password])
def search_password(user_id):
with open('user_passwords.csv', 'r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if row[0] == user_id:
return row[1]
return None
create_csv()
user_id = input("Enter user-id to search for: ")
password = search_password(user_id)
if password is not None:
print("The password for user-id ", user_id, "is", password)
else:
print("User-id", user_id, "is not found")
RESULT:
Thus, the program was executed successfully.
PGM NO: 12
PROBLEM STATEMENT:
Write a menu driven Python program to append, read and update
records from a CSV file containing the details of Employee (Empid, Name,
Department, Salary) stored as a list. Get empid as input to update the
salary.
AIM:
To write a Python program to create a CSV file and perform update
operation on the file.
SOURCE CODE:
import csv, os
def add_records(fname):
fname+=".csv"
file_obj=open(fname,"a",newline='')
cw=csv.writer(file_obj)
n=int(input("Enter no. of records:"))
for i in range(n):
empid=int(input("Enter employee id:"))
name=input("Enter name:")
dept=input("Enter department:")
sal=int(input("Enter salary:"))
emp_rec=[empid,name,dept,sal]
cw.writerow(emp_rec)
file_obj.close()
def display_records(fname):
fname+=".csv"
file_obj=open(fname,"r")
cr=csv.reader(file_obj)
for i in cr:
print(i)
file_obj.close()
def update_record(fname,eid):
fname+=".csv"
file_obj=open(fname,"r")
new_fileobj=open("temp.csv","w",newline='')
cr=csv.reader(file_obj)
cw=csv.writer(new_fileobj)
flag=False
for i in cr:
if int(i[0])==eid:
new_sal=int(input("Enter updated salary:"))
flag =True
new_rec=[i[0],i[1],i[2],new_sal]
cw.writerow(new_rec)
print("Record updated")
else:
cw.writerow(i)
if flag==False:
print("record not found")
file_obj.close()
new_fileobj.close()
os.remove(fname)
os.rename("temp.csv",fname)
while True:
print("\n1. Add records \n2. Display records \n3. Update salary \
\n4. Exit")
ch=int(input("Enter your choice:"))
if ch!=4:
name=input("Enter filename:")
if ch==1:
add_records(name)
elif ch==2:
display_records(name)
elif ch==3:
empid=int(input("Enter employee id to update:"))
update_record(name,empid)
elif ch==4:
break
else:
print("Invalid choice")
RESULT:
Thus, the program was executed successfully.
PGM NO: 13
PROBLEM STATEMENT:
Write a menu driven Python program to append, read and delete from
a CSV file containing the details of Course [CourseId, CourseName, Faculty,
Fees] as a list. Get CourseId as input and delete the record.
AIM:
To write a Python program to create a CSV file and implement delete
operation on the file.
SOURCE CODE:
import csv,os
def add_records(fname):
fname+=".csv"
file_obj=open(fname,"a",newline='')
cw=csv.writer(file_obj)
n=int(input("Enter no. of records:"))
for i in range(n):
courseid=int(input("Enter course id:"))
cname=input("Enter course name:")
faculty=input("Enter faculty:")
fees=int(input("Enter fees:"))
course=[courseid,cname,faculty,fees]
cw.writerow(course)
file_obj.close()
def display_records(fname):
fname+=".csv"
file_obj=open(fname,"r")
cr=csv.reader(file_obj)
for i in cr:
print(i)
file_obj.close()
def search_record(fname,cid):
fname+=".csv"
file_obj=open(fname,"r")
new_fileobj=open("temp.csv","w",newline='')
cr=csv.reader(file_obj)
cw=csv.writer(new_fileobj)
flag=False
for i in cr:
if int(i[0])==cid:
flag =True
print("Record deleted")
else:
cw.writerow(i)
if flag==False:
print("Course Id not found")
file_obj.close()
new_fileobj.close()
os.remove(fname)
os.rename("temp.csv",fname)
while True:
print("\n1. Add Course details \n2. Display courses \n3. Delete
Course\n4. Exit")
ch=int(input("Enter your choice:"))
if ch!=4:
name=input("Enter filename:")
if ch==1:
add_records(name)
elif ch==2:
display_records(name)
elif ch==3:
cid=int(input("Enter course id to delete:"))
search_record(name,cid)
elif ch==4:
break
else:
print("Invalid choice")
RESULT:
Thus, the program was executed successfully.
PGM NO: 14
DATE: STACK - 1
PROBLEM STATEMENT:
Write a menu-driven Python program to push, pop, peek and display
elements from a STACK containing the list of integers.
AIM:
To write a Python program to create a stack of integers and perform
all sstack operations on the file.
SOURCE CODE:
def Check_empty(stack):
return len(stack) == 0
def Pop(stack):
if (Check_empty(stack)):
return "stack is empty"
return stack.pop()
def Display(stack):
if Check_empty(stack):
print("Stack Empty")
else:
TOP = len(stack) -1
print(stack[TOP],"<--top")
for a in range(TOP-1,-1,-1):
print(stack[a])
stack = [ ]
ch=0
while(ch!=4):
print("\n1.Push \n2.Pop \n3.Display \n4.Exit")
ch=int(input("Enter Ur Choice: "))
if ch==1:
a = int(input("Enter an Integer"))
Push(stack, a)
elif ch==2: print("popped item: ",Pop(stack))
elif ch==3: Display(stack)
elif(ch==4): print("Thank You !!!")
else: print("Enter choice between 1 and 4")
RESULT:
Thus, the program was executed successfully.
PGM NO: 15
DATE: STACK - 2
PROBLEM STATEMENT:
Write a menu-driven Python program to Push, Pop, Peek, Search and
Display the elements from a STACK containing the details of Student name,
mark in Computer science as a list.
AIM:
To write a Python program to implement all stack operations on a list
containing student name and cs marks.
SOURCE CODE:
def Check_empty(stack):
return len(stack) == 0
def Pop(stack):
if (Check_empty(stack)):
return "stack is empty"
return stack.pop()
def Display(stack):
if Check_empty(stack):
print("Stack Empty")
else:
TOP = len(stack) -1
print(stack[TOP],"<--top")
for a in range(TOP-1,-1,-1):
print(stack[a])
def Search(stack):
found = False
if Check_empty(stack):
print("Stack Empty")
else:
name= input("Enter value to be searched: ")
for a in range(len(stack)-1,-1,-1):
if (stack[a][0]==name):
found = True
print(name, " Found in Stack, mark=",stack[a][1])
break
if(found==False):
print("No such item in Stack")
stack = [ ]
ch=0
while(ch!=5):
print("\n1.Push \n2.Pop \n3.Display \n4.Search\n5.Exit")
ch=int(input("Enter Ur Choice: "))
if ch==1:
stu_name = input("Enter student name:")
cs_mark = int(input("Enter CS marks:"))
Push(stack, [stu_name,cs_mark])
elif ch==2: print("popped item: ",Pop(stack))
elif ch==3: Display(stack)
elif ch==4: Search(stack)
elif(ch==5): print("Thank You !!!")
else: print("Enter choice between 1 and 4")
RESULT:
Thus, the program was executed successfully.