Ssce Computer Science Lab Manual - 2024-2025-1
Ssce Computer Science Lab Manual - 2024-2025-1
QP - SET 1 a.
TEXT FILE
PROBLEM STATEMENT:
Write a Python program to accept the details of a book like book number, book
name, author and price as input from the user and write it into a text file as
a single record.
AIM:
To write a Python program to accept the details of a book and write it into a
text file as a single record.
SOURCE CODE:
myfile=open("Book.txt","a")
ch='y'
while ch=='y' or ch=='Y':
bno=int(input("Enter a book number:"))
bname=input("Enter the book name:")
author=input("Enter the author name:")
price=int(input("Enter book price:"))
bookrec=str(bno)+","+bname+","+author+","+str(price)+"\n"
myfile.write(bookrec)
print("Data written successfully:")
ch=input("Do you want to continue: y/n")
myfile.close()
OUTPUT:
Enter a book number:1
Enter the book name:Harrypotter
Enter the author name:J.K.Rowling
Enter book price:569
Data written successfully:
Do you want to continue: y/nY
Enter a book number:2
Enter the book name:Ponniyin selvan
Enter the author name:kalki
Enter book price:1500
Data written successfully:
Do you want to continue: y/nn
RESULT:
Thus the program was executed successfully.
QP - SET 1 b.
Write a menu driven program in Python using three different user defined functions to
display the patterns as follows:
• Horizontal display of numbers
• Vertical display of numbers
• Continuous display of numbers
AIM:
SOURCE CODE:
def Horizontal():
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(i, end=" ")
print()
def Vertical():
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print()
def Continous():
k=1
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(k, end=" ")
k=k+1
print()
ch=0
while(ch!=4):
print("\n1.Horizontal \n2.Vertical \n3.Continous \n4.Exit")
ch=int(input("Enter Ur Choice: "))
if(ch==1):
Horizontal()
elif(ch==2):
Vertical()
elif(ch==3):
Continous()
elif(ch==4):
print("Thank you")
else:
print("Enter the valid choice between 1 and 4")
OUTPUT:
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 1
Enter the no of Steps: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 2
Enter the no of Steps: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 3
Enter the no of Steps: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 4
Thank you
RESULT:
Thus the program was executed successfully.
QP - SET 2 a.
1. Write a Python program with the following specification: Create a binary file to store the
details of the customer as follows.
Dictionary Name: Customer Key: Phone number Value: Name
The program must have the following options.
• Enter data into the file
• Display the records.
AIM:
To write a Python program to create a binary file to store the details of the customer in a
dictionary.
SOURCE CODE:
import pickle
def EnterCustomerDetails():
Customer = dict()
CName = input('Enter Customer Name:')
PhNo = input('Enter Phone number:')
Customer[PhNo] = CName
f = open("Customer.dat",'ab')
pickle.dump(Customer,f)
f.close()
def DisplayCustomerDetails():
with open("Customer.dat",'rb') as f :
while True:
try:
record = pickle.load(f)
for phno in record:
print('Customer Name: ',record[phno])
print('Phone Number: ',phno)
except EOFError:
break
ch=0
while(ch!=3):
print("\n1.Enter Customer Details \n2.Display Customer Details \n3.Exit")
ch=int(input("Enter Ur Choice: "))
print()
if(ch==1):
EnterCustomerDetails()
elif(ch==2):
DisplayCustomerDetails()
elif(ch==3):
print("Thank You !!!")
else:
print("Enter the valid choice between 1 and 4")
OUTPUT
1.Enter Customer Details
2.Display Customer Details
3.Exit
Enter Ur Choice: 1
Enter Ur Choice: 3
Thank You !!!
Result
The program was executed successfully
SET 2B.
PROBLEM STATEMENT:
Write a Python program to Push, Pop, and Display from a STACK containing
the details of CS marks as a list. The details are stored as an integer.
AIM:
To write an interactive menu driven Python program that will implement the
STACK created with the given details.
SOURCE CODE:
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
print ("4. Exit")
choice=int(input("Enter your choice: "))
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print (s[i])
elif (choice==4):
print('Thank you')
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
OUTPUT
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :32
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :95
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 2
Deleted element is : 95
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
32
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 4
Thank you
Do you want to continue or not? n
RESULT
Thus the program was executed successfully.
SET 3 A
1. a. To write a Python program to create a binary file with roll number, name , mark and
AIM:
To write a Python program to create a binary file with roll number, name ,
mark and update/modify the mark for a given roll number.
SOURCE CODE:
import pickle
def create():
f=open('marks.dat','ab')
opt='y'
while opt=='y':
rollno=int(input('Roll number'))
name=input('Name')
mark=int(input('Mark'))
lst=[rollno,name,mark]
pickle.dump(lst,f)
opt=input('Do you want to continue y or n')
f.close()
def update():
f=open('marks.dat','rb+')
no=int(input('Enter rollno'))
found=0
try:
while True:
pos=f.tell()
s=pickle.load(f)
if s[0]==no:
print("The searched rollnumber",s)
s[2]=int(input("Enter new marks"))
f.seek(pos)
pickle.dump(s,f)
found=1
f.seek(pos)
print('Updated maks',s)
break
except:
f.close()
if found==0:
print("The searched roll number is not found")
create()
update()
OUTPUT:
Roll number1
NameMALA
Mark98
Do you want to continue y or ny
Roll number2
NameKALA
Mark78
Do you want to continue y or ny
Roll number3
NameNILA
Mark56
Do you want to continue y or nn
Enter rollno2
The searched rollnumber [2, 'KALA', 78]
Enter new marks100
Updated maks [2, 'KALA', 100]
RESULT:
Aim :
To write an interactive menu driven Python program that will add the
elements in to the stack that are divisible by 3 and the pop the elements from
the stack.
Source Code:
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
print ("4. Exit")
choice=int(input("Enter your choice: "))
if (choice==1):
a=int(input("Enter any number :"))
if (a%3 == 0):
s.append(a)
else:
print('The number is not dicisible by 3')
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print (s[i])
elif (choice==4):
print('Exit')
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
Output
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :12
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :30
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :11
The number is not divisible by 3
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 2
Deleted element is : 30
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
12
Do you want to continue or not? n
Result:
The program were executed successfully.
SET 4 A
1. Write a menu driven Python program to append and search from a CSV file containing the
details of BankAccount (Accno, Name, Address, Mobileno, Balance) as a list
PROBLEM STATEMENT:
Write a menu driven Python program to append and search from a CSV file
containing the details of BankAccount (Accno, Name, Address, Mobileno,
Balance) as a list.
SOURCE CODE:
import csv
def Append():
csvf = open('BankAccount.csv', mode = 'a', newline='')
mywr = csv.writer(csvf,delimiter=',')
Accno = input("Enter Account Number: ")
Name = input("Enter Account Holder Name: ")
Address = input("Enter Account Holder Address: ")
Mobileno = input("Enter Account Holder Mobileno: ")
Balance = float(input("Enter Account Holder Balance: "))
mywr.writerow([Accno, Name, Address, Mobileno, Balance])
csvf.close()
def Search(accno):
found = False
with open('BankAccount.csv') as csvf:
myrd = csv.reader(csvf,delimiter=',')
for r in myrd:
if(r[0] == accno):
print(r)
found = True
if found == False :
print("No such Account Holder in the File")
ch=0
while(ch!=3):
print("\n1.Append \n2.Search\n3.Exit")
ch=int(input("Enter Ur Choice: "))
print()
if(ch==1):
Append()
elif (ch==2):
accno=input("Enter Account Number to be Searched: ")
Search(accno)
elif (ch==3):
print("Thank you")
else:
print("Enter a valid choice between 1 and 3")
OUTPUT
1.Append
2.Search
3.Exit
Enter Ur Choice: 1
1.Append
2.Search
3.Exit
Enter Ur Choice: 1
1.Append
2.Search
3.Exit
Enter Ur Choice: 1
1.Append
2.Search
3.Exit
Enter Ur Choice: 2
1.Append
2.Search
3.Exit
Enter Ur Choice: 3
Thank you
RESULT:
OUTPUT
Poem.txt:
Far far from gusty waves these children’s faces. Like rootless weeds, the hair
torn round their pallor: The tall girl with her weighed-down head. The paper
seeming Boy, with rat’s eyes. The stunted, unlucky heir Of twisted bones,
reciting a father’s gnarled disease, His lesson, from his desk. At back of the
dim class One unnoted, sweet and young. His eyes live in a dream, Of
squirrel’s game, in tree room, other than this.
No. of Lines: 8
No. of Sentences: 6
No. of the: 5
RESULT:
AIM:
To write a Python program to find the sum of salary of all the employees and
count the number of employees who are getting salary more than 7000.
SOURCE CODE:
import csv
def writeemp():
f=open("employee.csv","w",newline='')
swriter=csv.writer(f)
swriter.writerow(['EmpNo','Empname','Salary'])
rec=[]
while True:
eno=int(input("Enter Empno:"))
ename=input("Enter Ename:")
salary=int(input("Enter salary:"))
data=[eno,ename,salary]
rec.append(data)
ch=input("\nDo you want to continue(y/n)\n")
if ch in "nN":
break
swriter.writerows(rec)
print("\nDATA WRITTEN SUCCESSFULLY\n")
f.close()
def read():
f=open("employee.csv","r")
print("\nReading data from a CSV file..\n")
sreader=csv.reader(f)
for i in sreader:
print(i)
f.close()
def sumsalary():
f=open("employee.csv","r")
sreader=csv.reader(f)
next(sreader)
sum1=0
for i in sreader:
sum1=sum1+int(i[2])
print("\nsum of salary of all employees:\n",sum1)
f.close()
def salagr():
f=open("employee.csv","r")
sreader=csv.reader(f)
count1=0
next(sreader)
for i in sreader:
if int(i[2])>7000:
print(i)
count1+=1
print("\nNumber of employees salary above 7000:\n",count1)
f.close()
writeemp()
read()
sumsalary()
salagr()
OUTPUT
Enter Empno:1
Enter Ename:MALA
Enter salary:2500
RESULT:
Thus the program was executed successfully.
QP - SET 5B.
PROBLEM STATEMENT:
. Write a menu driven program in Python using different user defined functions which
accepts nested list as a parameter to perform the following Matrix operations such as
• Upper Half display
• Diagonal elements
AIM:
To write a menu driven Python program to append and delete from a CSV file
containing the details of Course (CourseId, CourseName, Faculty, Fees).
SOURCE CODE:
import csv
def Append():
csvf = open('CourseDetails.csv', mode = 'a',newline='')
mywr = csv.writer(csvf,delimiter=',')
CourseId = input("Enter Course ID: ")
CourseName = input("Enter Course Name: ")
Faculty = input("Enter Faculty Name: ")
Fees = float(input("Enter Course Fees: "))
mywr.writerow([CourseId, CourseName, Faculty, Fees])
csvf.close()
def Delete(crd):
found = False
csvf = open('CourseDetails.csv', mode = 'r')
temp=[]
myrd = csv.reader(csvf,delimiter=',')
for r in myrd:
if(r[0] != crd):
temp.append(r)
else:
found = True
csvf.close()
csvf = open('CourseDetails.csv', mode = 'w',newline='')
mywr = csv.writer(csvf,delimiter=',')
mywr.writerows(temp)
csvf.close()
if found == False:
print("No such Course in the File")
else:
print("Data Deleted Successfully")
print("The records after deletion")
for i in temp:
print(i)
ch=0
while(ch!=3):
print("\n1.Append \n2.Delete \n3.Exit")
ch=int(input("Enter Ur Choice: "))
print()
if(ch==1):
Append()
elif (ch==2):
crd=input("Enter Course ID to be Deleted: ")
Delete(crd)
elif (ch==3):
print("Thank You !!!")
else:
print("Enter the valid choice between 1 and 3")
OUTPUT
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
Enter Course ID: 1
Enter Course Name: C++
Enter Faculty Name: NILA
Enter Course Fees: 2500
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
TABLE : FARES
ii. Arrange the contents of the table FLIGHTS in the ascending order
of FL_NO.
SELECT * FROM FLIGHTS ORDER BY FL_NO;
iv. Display the total no.of flights from FLIGHTS where SOURCE starts
with letter ‘M’ and the no.of stops is more than 2.
RESULT
The queries were executed successfully
SET 2 & 4
Study the following tables TEACHER and SALARY and write SQL commands for
the questions (i) to (iv) 4
TABLE : TEACHER
TABLE : SALARY
i. Display name and DA of all staff who are in Accounts department and
having more than 8 years of experience and DA is more than or equal
to 300.
iv. Display the number of male teachers and female teachers in the Teacher
table.
RESULT
The queries were executed successfully