0% found this document useful (0 votes)
42 views31 pages

Ssce Computer Science Lab Manual - 2024-2025-1

Uploaded by

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

Ssce Computer Science Lab Manual - 2024-2025-1

Uploaded by

Mishku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMPUTER SCIENCE

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:

To write a Python program to display different patterns using user defined


functions.

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 Customer Name:NILA


Enter Phone number:123456

1.Enter Customer Details


2.Display Customer Details
3.Exit
Enter Ur Choice: 1

Enter Customer Name:USHA


Enter Phone number:987654

1.Enter Customer Details


2.Display Customer Details
3.Exit
Enter Ur Choice: 2

Customer Name: NILA


Phone Number: 123456
Customer Name: USHA
Phone Number: 987654

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

update/modify the mark for a given roll number.

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:

Thus the program was executed successfully.


SET 3 B
Write a Python Program to perform stack operations,
a) Push the elements that are divisible by 3
b) Pop the elements from the stack

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

Enter Account Number: 101


Enter Account Holder Name: A
Enter Account Holder Address: TAMBARAM
Enter Account Holder Mobileno: 8889993331
Enter Account Holder Balance: 78456

1.Append
2.Search
3.Exit
Enter Ur Choice: 1

Enter Account Number: 102


Enter Account Holder Name: B
Enter Account Holder Address: PALLAVARAM
Enter Account Holder Mobileno: 9992223331
Enter Account Holder Balance: 45678

1.Append
2.Search
3.Exit
Enter Ur Choice: 1

Enter Account Number: 103


Enter Account Holder Name: C
Enter Account Holder Address: CHROMEPET
Enter Account Holder Mobileno: 8889996664
Enter Account Holder Balance: 12345

1.Append
2.Search
3.Exit
Enter Ur Choice: 2

Enter Account Number to be Searched: 103


['103', 'C', 'CHROMEPET', '8889996664', '12345.0']

1.Append
2.Search
3.Exit
Enter Ur Choice: 3

Thank you

RESULT:

Thus the program was executed successfully.


SET 4 B
To write a Python program to count the No. of sentences, No. of Lines and to find the
word “the" in a text file ‘story.txt’.
AIM:
To write a Python program to count the No. of sentences, No. of Lines and to
find the word “the".
SOURCE CODE:
file = open("Poem.txt","r")
ctlines = 0
ctsent = 0
ctthe = 0
st = file.read()
words = st.split()
for i in st:
if i ==".":
ctsent+=1
elif i =='\n':
ctlines+=1
for j in words:
k = j.lower()
if k =="the":
ctthe+=1
file.close()
print("No. of Lines: ", ctlines)
print("No. of Sentences: ", ctsent)
print("No. of the: ", ctthe)

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:

Thus the program was executed successfully.


SET 5 A
1.a. 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 using functions in a CSV file. The file
contains the details of Employee (EmpNo, EmpName and salary).

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

Do you want to continue(y/n)


Y
Enter Empno:2
Enter Ename:NILA
Enter salary:9000

Do you want to continue(y/n)


Y
Enter Empno:3
Enter Ename:KALA
Enter salary:7500

Do you want to continue(y/n)


N

DATA WRITTEN SUCCESSFULLY

Reading data from a CSV file..

['EmpNo', 'Empname', 'Salary']


['1', 'MALA', '2500']
['2', 'NILA', '9000']
['3', 'KALA', '7500']

sum of salary of all employees:


19000
['2', 'NILA', '9000']
['3', 'KALA', '7500']

Number of employees salary above 7000:


2

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

Enter Course ID: 2


Enter Course Name: C
Enter Faculty Name: MALA
Enter Course Fees: 3000

1.Append
2.Delete
3.Exit
Enter Ur Choice: 1

Enter Course ID: 3


Enter Course Name: PYTHON
Enter Faculty Name: KALA
Enter Course Fees: 4500
1.Append
2.Delete
3.Exit
Enter Ur Choice: 2

Enter Course ID to be Deleted: 2


Data Deleted Successfully
The records after deletion
['1', 'C++', 'NILA', '2500.0']
['3', 'PYTHON', 'KALA', '4500.0']
1.Append
2.Delete
3.Exit
Enter Ur Choice: 3

Thank You !!!


RESULT:

Thus the program was executed successfully.


SET 1, 3 & 5
Study the following tables TEACHER and SALARY and write SQL commands for
the questions (i) to (iv)
TABLE : FLIGHTS

TABLE : FARES

i. Display FL_NO and NO_FLIGHTS from “KANPUR” TO


“BANGALORE” from the table FLIGHTS.

SELECT FL_NO, NO_FLIGHTS FROM FLIGHTS WHERE SOURCE=”KANPUR” AND


DESTINATION=”BANGALORE”;

ii. Arrange the contents of the table FLIGHTS in the ascending order
of FL_NO.
SELECT * FROM FLIGHTS ORDER BY FL_NO;

iii. Display the minimum fare “Indian Airlines” is offering.


SELECT MIN(FARE) FROM FARES WHERE AIRLINES='INDIAN
AIRLINES';

iv. Display the total no.of flights from FLIGHTS where SOURCE starts
with letter ‘M’ and the no.of stops is more than 2.

SELECT COUNT(*) AS "RESULT" FROM FLIGHTS WHERE SOURCE LIKE


'M%' AND NO_STOPS>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.

SELECT TNAME, DA FROM TEACHER T, SALARY S WHERE T.TID=S.TID


AND EXPERIENCE>8 AND DA>=300;

ii. Display the number of teachers and maximum BASIC in each


department.

SELECT DEPT, COUNT(*) AS "No OF TEACHERS" , MAX(BASIC) AS


"MAX" FROM TEACHER,SALARY WHERE TEACHER.TID=SALARY.TID
GROUP BY DEPT;
iii. Display the name and department of all staff whose name has ‘am’ in
it.

SELECT TNAME,DEPT FROM TEACHER WHERE TNAME LIKE '%am%';

iv. Display the number of male teachers and female teachers in the Teacher
table.

SELECT GENDER, COUNT(*) AS " NO OF TEACHERS " FROM TEACHER


GROUP BY GENDER;

RESULT
The queries were executed successfully

You might also like