Cs Rec Final2024
Cs Rec Final2024
2025
(Senior Secondary)
COMPUTER SCIENCE
Practical Record
CERTIFICATE
#Main program
n=int(input("Enter the limit: "))
fib(n)
Output
Enter a text: I love my dad and mom
1.Palindromic words
2.Longest word
3.Exit
Enter choice: 1
Palindromic words are
I
Dad
mom
1. Palindromic words
2. Longest word
3. Exit
Enter choice: 2
Longest word is: love
1. Palindromic words
2. Longest word
3. Exit
Enter choice: 3
Program – 3
Write a menu driven program to accept a line of text and to
1. Print the palindromic words
2. Display the longest word
#Main program
s = input("Enter a text: ")
t = s.split()
while True:
print("1. Palindromic words")
print("2. Longest word")
print("3. Exit")
ch=int(input("Enter choice: "))
if ch == 1:
pal(t)
elif ch == 2:
long(t)
elif ch==3:
break
else:
print("wrong choice")
Output
Enter limit: 6
Enter elements: 1
Enter elements: 2
Enter elements: 3
Enter elements: 4
Enter elements: 5
Enter elements: 6
Prime numbers are[ 2, 3, 5]
Program – 4
Define a function which takes an integer as argument and returns 0 if the number
is prime and 1 if it is not prime. Using this function, write a program to copy the
prime numbers of a list into a new list
#Function to Display
def disp(d):
n=input("Enter the name of a person whose birthday has to be displayed”)
print(d[n],"is the birthday of",n)
#Function to Modify
def modify(d):
n=input("Enter name whose birthday is to be modified: ")
b=input("Enter birthday: ")
d[n]=b
print(d)
#Function to Delete
def dele(d):
n = input("Enter name to be deleted: ")
if n in d:
del d[n]
print(f"{n} has been deleted.")
else:
print(f"{n} not found in the dictionary.")
print(d)
#Main program
n=int(input("Enter limit: "))
d ={}
for i in range(n):
n=input("Enter name:")
b=input("Enter birthday: ")
d[n]=b
while True:
print("1. Display birth day")
print("2. Modify birth day")
print("3. Delete friend")
print("4. Exit")
ch=int(input("Enter choice: "))
if ch== 1:
disp(d)
elif ch==2:
modify(d)
elif ch==3:
dele(d)
elif ch==4:
break
else:
print('wrong output')
Output
Displaying the contents of the file
Python is a programming Language. Python is used for writing Al program.
Displaying each word in the file separated by '#'
Python#is#a#programming#Language.Python#is#used#for#writing#AI#program
Program - 7
Write a python program to read a text file line by line and display each word separated
by ‘#’.
#python program to read a text file line by line and display each word separated by‘#’.
f=open("sample.txt","w")
str "Python is a programming Language. Python is used for writing Al program."
f.write(str)
f.close()
print("Displaying the contents of the file")
f=open("sample.txt","r")
ch=f.readlines()
for i in ch:
print(i)
f.close()
print("Displaying each word in the file separated by '#"\n")
f=open("sample.txt","r")
ch-f.read()
ch=ch.replace(" ",'#')
print(ch)
f.close()
Output
f-open("sample.txt","w")
str="Python is a programming language. \nIt is so simple"
f.write(str)
f.close()
print("Displaying the contents of the file\n")
f=open("sample.txt","r")
ch=f.readlines()
for i in ch:
print(i)
f.close()
fl=open('New.txt','w')
f=open("sample.txt","r")
ch-f.readlines()
for i in ch:
if 'a' not in i:
fl.write(i)
f.close()
fl.close()
print("\nDisplaying the contents of the file which do not contain 'a' ")
fl=open('New.txt','r')
ch=fl.read()
print(ch)
fl.close()
Output
Enter text: Python is a simple language
Number of vowels: 9
Number of consonants: 14
Number of upper case: 1
Number of lower case: 22
Program - 9
Write a program to create a text file. Read the text file and display the number of
vowels consonants/uppercase/ lowercase characters in the file
#Read a text file and display the number of vowels, consonants, uppercase, lowercase
characters in the file.
#creating a text file
f=open("sample.txt","w")
str=input("Enter text: ")
f.write(str)
f.close()
Enter no of employees: 3
Enter Employee no.: 101
Enter Employee name : Dhanya
Enter Salary: 45000
Enter Employee no: 102
Enter Employee name : Rahul
Enter Salary: 87000
Enter Employee no.: 104
Enter Employee name : Anand
Enter Salary: 90000
The Contents of the File:
101 Dhanya 45000
102 Rahul 87000
104 Anand 90000
Program – 11
Create a binary file to store details of n employees in a binary file and display it
on the screen
#program to read n employees employ no, name and salary #and create a binary file
and read and display the #contents of the file on the screen
import pickle
def create():
fw-open("employ.dat","wb")
n=int(input("Enter n"))
for i in range(n):
eno=int(input("Enter Employee Number"))
ename=input("Enter Employee Name")
salary=int(input("Enter Salary"))
l=[eno,ename,salary]
pickle.dump(1,fw)
fw.close()
def display():
fr=open("employ.dat","rb")
try:
while True:
l=pickle.load(fr)
print(l[0],’\t’,l[1],’\t’,l[2])
except EOFError:
fr.close()
create()
display()
Output
Enter Number of books: 2
Enter Book Number: 1001
Enter Book Name: Python
Enter Book Number: 1002
Enter Book Name: C++
The contents of the file:
1001 Python
1002 C++
Enter the bookno of the book to be searched: 1002
The Book Name of Book Number 1002 is C++
Enter the bookno of the book to be searched:1004
Search not found
Program - 12
Create a binary file with book name and book number. Search for a given book
number and display the name of the book. If not found, display an appropriate
message
import pickle
#creating a binary file
def create():
fw open("book.dat","wb")
n=int(input("Enter Number of books"))
for i in range(n):
bno=int(input("Enter Book Number"))
bname input("Enter Book Name").
1=[bno,bname]
pickle.dump(1,fw)
fw.close()
#displaying the contents of the binary file
def display():
import pickle
fr=open("book.dat","rb")
try:
while True:
1-pickle.load(fr)
print(1[0],'\r'.1[1])
except EOFError:
fr.close()
#searching the contents of the file
def search():
fr=open("book.dat","rb")
sbno=int(input("Enter the bookno of the book to be searched"))
found=0
try:
while True:
l=pickle.load(fr)
if 1[0]==sbno:
print("The Book Name of Book Number ",l[0]," is ",l[1])
found=1
break
except EOFError:
if found==0:
print("Search not found")
fr.close()
#main program
create()
print("The contents of the file:")
display()
search()
Output
Enter no of Students: 2
Enter Roll no.: 1
Enter Student name: Anand
Enter Marks: 99
Enter Roll no.: 2
Enter Student name: Adish
Enter Marks: 100
Displaying original data
Rollno Name Mark
1 Anand 99
2 Adish 100
Enter the rollno of the student whose data has to be modified:
Enter Name: Anand
Enter Mark:100
Record modified
Displaying contents of the file after modification
Rollno Name Mark
1 Anand 100
2 Adish 100
Program -13
Create a binary file with roll number, name and marks. Input a roll number and
update the marks
import pickle
import os
#creating the file
f=open("stud.dat","wb")
n=int(input("Enter no of Students: "))
for i in range(n):
rollno=int(input("Enter Roll no.: "))
sname=input("Enter Student name: ")
marks=int(input("Enter Marks: "))
I=[rollno,sname,marks]
pickle.dump(1,f)
f.close()
#displaying the contents of the file
print("Displaying original data")
f-open("stud.dat","rb")
print("Rollno\tName\tMark")
try:
while True:
e=pickle.load(f)
print(e[0].'\t',e[1],'\t',e[2])
except EOFError:
f.close()
#modify the content of the file
f=open("stud.dat","rb")
fl=open("temp.dat","wb")
found=0
no=int(input("Enter the rollno of the student whose data has to be modified"))
try:
while True:
e=pickle.load(f)
if e[0]==no:
e[1]=input("Enter Name:")
e[2]=int(input("Enter Mark:"))
found=1
pickle.dump(e,fl)
except EOFError:
if found=0:
print("Record to be modified not present")
fl.close()
f.close()
else:
print("Record modified")
fl.close()
f.close()
os.remove("stud.dat")
os.rename("temp.dat","stud.dat")
#displaying the contents of the file after modification print("Displaying contents of the
file after modification")
F=open("stud.dat","rb")
print("Rollno\tName\tMark")
try:
while True:
e=pickle.load(f)
print(e[0],'\t',e[1],'\t',e[2])
except EOFError:
f.close()
OUTPUT
Enter number of records:5
Enter Rollno: 1
Enter Name: Anchal
Enter Rollno:2
Enter Name: Abhi
Enter Rollno:3
Enter Name:Hari
Enter Rollno:4
Enter Name:krish
Enter Rollno:5
Enter Name:Sachu
The Student Details:
Enter the rollno of the record to be deleted:4
Record deleted
The Student Details after Deletion:
1 Anchal
2 Abhi
3 Hari
5 Sachu
Program - 14
Create a binary file student.dat with contents rollno and name. Delete a record by
entering the rollno.Display the contents of the file before and after deletion
#program to create a binary file student.dat with contents rollno and name
#Delete a record by entering the rollno Display the contents of the file before and after
deletion
def create():
import pickle
fw-open("student.dat","wb")
n=int(input("Enter number of records:"))
for i in range(n):
rollno int(input("Enter Rollno:"))
name=input("Enter Name")
I=[rollno,name]
pickle.dump(l,fw)
fw.close()
#displaying the contents of the binary file
def display():
import pickle
fr=open("student.dat","rb")
try:
while True:
I=pickle.load(fr)
print(1[0],'\t'.[1])
except EOFError:
fr.close()
#Deleting the contents from the binary file
def delete():
import pickle
import os
fr=open("student.dat","rb")
fw=open("temp","wb")
found=0
r=int(input("Enter the rollno of the record to be deleted"))
try:
while True:
1=pickle.load(fr)
if 1[0]==r:
found=1
else:
pickle.dump(1,fw)
except EOFError:
fr.close()
fw.close()
if found==1:
print("Record deleted")
os.remove("student.dat")
os.rename("temp","student.dat")
else:
print("Search Record not found")
#main program
create()
print("The Student Details:")
display()
delete()
print("The Student Details after Deletion:")
display()
Output
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 1
Enter Package number: 1
Enter Package name: A
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 1
Enter Package number: 2
Enter Package name: B
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 1
Enter Package number: 3
Enter Package name: C
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 3
['3', 'C']
['2', 'B']
['1', 'A']
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 2
Deleted value: ['3', 'C']
Program – 15
Write a function in python, MakePush(Package) and MakePop(Package) to add a
new Package and delete a Package from a List of Package Description,
considering them to act as push and pop operations of the Stack data structure.
Program
def Makepush(Package):
id= input("Enter Package number: ")
name=input("Enter Package name: ")
p=[id,name]
Package.append(p)
def Makepop(Package):
if Package==[]:
print("Empty stack")
else:
print("Deleted value: ", Package.pop())
#main
Package=[ ]
while (True):
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4. EXIT")
choice=int(input("Enter a choice: "))
if (choice-1):
Makepush(Package)
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 2
Deleted value: ['2', 'B']
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 2
Deleted value: ['1', 'A']
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 3
empty stack
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter a choice: 4
End of program
elif (choice-2):
Makepop(Package)
elif choice-3: d
display(Package)
else:
print("End of program")
break
Output
Enter the no of students: 4
Enter name of the student: Anoop
Enter marks: 67
Enter name of the student: Gokul
Enter mark: 90
Enter name of the student: Adithya
Enter marks: 09
Enter name of the student: Geethu
Enter marks: 74
The dictionary is
['Anoop: 67, Gokul: 90, "Adithya': 99, Geethư: 741
The stack is
Adithya
Gokul
After calling pop()....
Deleted name is. Adithya
The stack is Global
Program – 16
Create a dictionary containing names and marks as key value pairs of a students.
Write a program, with separate user defined functions to perform the following
operations:
• Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
• Pop and display the content of the stack.
Program
def push(student,a):
student.append(i)
def pop(S):
if student==[]:
print("Student list is empty")
else:
print("Deleted name is..", student.pop())
def display(student):
if student==[]:
print("Student list is empty")
else:
print("The stack is...")
n=len(student)
for i in range(n-1,-1,-1);
print (student[i])
student=[]
a={}
import csv
#create function
def create():
f=open("11.csv","w".newline")
w=csv.writer(f)
n=int(input("Enter number of records"))
for i in range(n):
eno=input("Enter Employee No")
ename=input("Enter Ename")
s=int(input("Enter salary"))
1=[eno,ername.s]
w.writerow(1)
f.close()
#search function
def search():
f=open("tl.csv","")
resv.reader(f)
n=input("Enter employee no to be searched")
for i in r:
if i[0]=n:
print("employ no" i[0],"name" i[1],"salary".[2])
break
else:
print("Record not found")
f.close()
#display function
def display():
f=open("tl.csv","r")
r=csv.reader(f):
for i in r:
print(i)
f.close()
#main program
create()
display()
search()
Output
1. Modify Salary
2. Display Employees
3. Exit
Enter choice: 1
Enter Employee Number: 1234
Enter new Salary: 7000
1. Modify Salary
2. Display Employees
3. Exit
Enter choice: 2
(1234, 'GEORGE', 'MANAGER', 7698, datetime.date(2013, 4, 12),
Decimal('7000.00'), None, 20)
(7369, 'SMITH', 'CLERK', 7902, datetime.date(2010, 12, 17), Decimal('5000.00'),
Decimal('800.00'), 20)
(7499, 'ALLEN', 'SALESMAN', 7698, datetime.date(2012, 2, 10), Decimal('6000.00'),
Decimal('500.00'), 30)
(7521, 'WARD', 'SALESMAN', 7698, datetime.date(2015, 7, 10), Decimal('4000.00'),
Decimal('900.00'), 30)
(7566, 'JONES', 'MANAGER', 7698, datetime.date(2010, 8, 12), Decimal('8000.00'),
None, 20)
(7934, 'JAMES', 'CLERK', 7698, datetime.date(2011, 12, 12), Decimal('3000.00'),
None, 10)
1. Modify Salary
2. Display Employees
3. Exit
Enter choice: 3
Program - 18
Write a menu driven python program to implement database connectivity with
MySQL to:
• Modify a record with new values for salary when employee number is given
by the user
• Display all records in the table
Program
import mysql.connector as c
con-c.connect(host='localhost',user='root',passwd='npol, database='exam')
cur-con.cursor()
while True:
print("1. Modify Salary")
print("2. Display Employees")
print("3. Exit")
ch=int(input("Enter choice: "))
if ch==1:
no-int(input("Enter Employee Number: "))
s=int(input("Enter new Salary: "))
cur.execute("update emp set sal={} where empno={}".format(s,no))
con.commit()
elif ch==2:
cur.execute("select * from emp")
t=cur.fetchall()
for i in t:
print(i)
else:
break
Output
1. Insert Employee
2. Display Employees
3. Exit
Enter choice: 1
Record inserted.
1. Insert Employee
2. Display Employees
3. Exit
Enter choice: 2
(1234, 'GEORGE', 'MANAGER', 7698, datetime.date(2013, 4, 12),
Decimal ('6000.00'), None, 20)
(7369, 'SMITH', 'CLERK', 7902, datetime.date(2010, 12, 17),
Decimal ('5000.00'), Decimal ('800.00'), 20)
(7499, 'ALLEN', 'SALESMAN', 7698, datetime.date(2012, 2, 10),
Decimal ('6000.00'), Decimal ('500.00'), 30)
(7521, 'WARD', 'SALESMAN', 7698, datetime.date(2015, 7, 10),
Decimal ('4000.00'), Decimal ('900.00'), 30)
(7566, 'JONES', 'MANAGER', 7698, datetime.date(2010, 8, 12),
Decimal ('8000.00'), None, 20)
(7934, 'JAMES', 'CLERK', 7698, datetime.date(2011, 12, 12),
Decimal ('3000.00'), None, 10)
1. Insert Employee
2. Display Employees
3. Exit
Enter choice: 3
Program – 19
Write a menu driven python program to implement database connectivity with MySQL
to:
• Insert a record.
• Display all records in the table.
Program
import mysql.connector as c
con=c.connect(host='localhost',user='root',passwd='npol',database='exam')
cur=con.cursor()
while True:
print("1. Insert Employee")
print("2. Display Employees")
print("3. Exit")
ch=int(input("Enter choice :"))
if ch==1:
query="insert into emp values(1234,'GEORGE','MANAGER',7698,'2013-04-12',6000,NULL,20)"
cur.execute(query)
con.commit()
print("Record inserted")
elif ch==2:
cur.execute("select * from emp")
t=cur.fetchall()
for i in t:
print(i)
else:
break
Output
1. Delete Record
2. Display Employees
3. Exit
Enter choice: 1
Enter Employee Number: 7369
1. Delete Record
2. Display Employees
3. Exit
Enter choice: 2
(7499, 'ALLEN', 'SALESMAN', 7698, datetime.date(2012, 2, 10), Decimal('345.60'),
Decimal('500.00'), 30)
(7521, 'WARD', 'SALESMAN', 7698, datetime.date(2015, 7, 10), Decimal('230.40'),
Decimal('900.00'), 30)
(7566, 'JONES', 'MANAGER', 7698, datetime.date(2010, 8, 12), Decimal('460.80'),
None, 20)
Program - 20
Write a menu driven python program to implement database connectivity with
MySQL to:
• Delete a record whose employee number is given by the user
• Display all records in the table
import mysql.connector as c
con=c.connect(host='localhost', user 'root' passwdnpol', database examl')
cur=con.cursor()
while True:
print("1. Delete Record")
print("2. Display Employees")
print("3. Exit")
ch=int(input("Enter choice: "))
if ch==1:
no=int(input("Enter Employee Number: "))
cur.execute("delete from emp where empno={}".format(no))
con.commit()
elif ch==2:
cur.execute("select * from emp")
t=cur.fetchall()
for i in t:
print(i)
else:
break
Output
1. Modify Salary
2. Display Employees
3. Exit
Enter choice: 1
1. Modify Salary
2. Display Employees
3. Exit
Enter choice: 2
(7369, 'SMITH', 'CLERK', 7902, datetime.date(2010, 12, 17), Decimal('288.00'),
Decimal('800.00'), 20)
(7499, 'ALLEN', 'SALESMAN', 7698, datetime.date(2012, 2, 10), Decimal('345.60'),
Decimal('500.00'), 30)
(7521, 'WARD', 'SALESMAN', 7698, datetime.date(2015, 7, 10), Decimal('230.40'),
Decimal('900.00'), 30)
(7566, 'JONES', 'MANAGER', 7698, datetime.date(2010, 8, 12), Decimal('460.80'),
None, 20)
Program - 21
Write a menu driven python program to implement database connectivity with
MySQL to:
• Increase the salary of all employees by 20%
• Display all records in the table
import mysql.connector as c
con=c.connect(host='localhost',user='root', passwd='npol', database='examl')
cur=con.cursor()
while True:
print("1. Modify Salary")
print("2. Display Employees")
print("3. Exit")
ch=int(input("Enter choice: "))
if ch==1:
cur.execute("update emp set sal=sal+sal*20/100")
con.commit()
elif ch==2:
cur.execute("select * from emp")
t=cur.fetchall()
for i in t:
print(i)
else:
break