CLASS XII Computer Science Record 2024-2025
INDEX
SI No Page Number
1. Area of Basic Shapes 02
2. Date Converter 05
3. Random Number Generator 06
4. Prime and Palindrome 08
5. Search a Word in the String 12
6. Displaying Lines Starting with ‘T’ – Text File 13
7. Removing Duplicate Tuple Elements 15
8. Inserting and Search Strings – Text Files 16
9. Replace Space with ‘#’ – Text File 20
10. Character Counting – Text File 21
11. Add, Search, Display & Update – Binary File 23
12. Write and Read – Binary File 29
13. Employee Records – CSV File 31
14. Write & Read – CSV File 33
15. Stack Using List Data Structure 35
16. MySQL Command 1 40
17. MySQL Command 2 42
18. MySQL Command 3 44
19. MySQL Command 4 47
20. MySQL Command 5 49
21. Database Connectivity 1 51
22. Database Connectivity 2 52
23. Database Connectivity 3 53
24. Database Connectivity 4 54
25. Database Connectivity 5 55
26. Exception Handling 56
1|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
AREA OF BASIC SHAPES
Aim: Write a menu driven Python program to calculate and display the area of circle,
square and rectangle
Program:
def circle():
r=float(input("Enter the radius: "))
area=3.14*r*r
return area
def square():
a=float(input("Enter the side: "))
area=a*a
return area
def rectangle():
l=float(input("Enter the length: "))
b=float(input("Enter the bredth: "))
area=l*b
return area
while True:
ch=int(input("""
[1]Area of circle
[2]Area of rectangle
[3]Area of square
2|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
[4]Exit
Enter the choice: """))
if ch==1:
print("Area of circle: ",circle())
elif ch==2:
print("Area of rectangle: ",rectangle())
elif ch==3:
print("Area of square: ",square())
elif ch==4:
print("\nThanks for using! ")
break
else:
print("Command not found!! ")
Output:
3|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
4|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATE CONVERTER
Aim: Write a python program to reads a string from the user containing a date in the
form mm/dd/yyyy. It should print the date in the form March 12, 1997
Program:
def date_converter():
date=input("Enter the date in format (Month/Day/Year) : ")
month=["January","February","March","Aplril","May","June","July",
"August","September","October","November","December"]
date=date.split("/")
try:
print(month[int(date[0])-1] ,date[1] , "," ,date[2])
except:
print("Enter date correctly! ")
date_converter()
Output:
5|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
RANDOM NUMBER GENERATOR
Aim: Write a python program to generate a random number and guess the number
Program:
import random
def guess_number():
random_number=random.randrange(1,11)
print("You have 3 chances")
chances=1
while True:
if chances <=3:
guess=int(input("Enter the guess: "))
if random_number == guess:
print("You won!")
break
else:
print('You lose!')
print("The number was ",random_number)
break
chances+=1
guess_number()
6|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
Output
7|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
PRIME AND PALINDROME
Aim: Write a menu driven program perform the following:
1. Enter a string and check if it is a palindrome or not
2. Enter a number and if it is a prime or not
Program:
def primary():
n = int(input('Enter Number: '))
z=0
if n==0 or n==1:
print('Neither Positive nor Negative')
elif n<0:
print('Enter Positive Number')
else:
for i in range(2,n//2):
if n%i == 0:
z=1
if z == 0:
print(n,'is a Prime Number')
else:
print(n,'is a Composite Number')
def palintrome():
str = input('Enter a string: ')
str1 = str[::-1]
if str.lower() == str1.lower():
8|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
print(str, 'is a palindrome')
else:
print(str, 'is a not palindrome')
while True:
ch=input("""
[1]Check primary
[2]Check Palintrome
[3]Exit
Enter the choice: """)
if ch=='1':
primary()
elif ch=='2':
palintrome()
elif ch=='3':
print("\nThanks for using! ")
break
else:
print("Command not found!! ")
Output:
9|Page Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
10 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
11 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
SEARCH A WORD IN THE STRING
Aim: Write a user defined function SearchWord(S,W), in Python, to search any word in
the given string/sentence.
Program:
def searchwords(s,w):
words=s.split()
if w in words:
print(w,"is present in the string")
else:
print(w,"is not present in the string")
Str=input("Enter the string / sentence: ").lower()
word=input("Enter the word to be searched: ").lower()
searchwords(Str,word)
Output:
12 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DISPLAYING LINES STARTING WITH ‘T’-
TEXTFILE
Aim: Write a Python program to read a text file “Starts.txt” and display all those lines that
start with ‘T’.
Program:
f=open('starts.txt','r')
Lines=f.readlines()
print("Orginal content")
for i in Lines:
print(i,end=" ")
print("\n\n\n")
print("Lines starting with 'T': ")
for i in Lines:
if i[0] == 'T':
print(i)
f.close()
File: Starts.txt
13 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
Output:
14 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
REMOVING DUPLICATE TUPLE ELEMENTS
Aim: Write a Python program to read a tuple and remove the duplicate elements in it.
Program:
def remove_dup_el(T1,T2):
for i in T1:
if i not in T2:
T2=T2+(i,)
print("Orginal Tuple",T1)
print("New Tuple",T2)
T1=eval(input("Enter the element: "))
T2=()
remove_dup_el(T1,T2)
Output:
15 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
INSERTING AND SEARCH STRINGS - TEXT
FILE
Aim: Write a Python program to write a few lines to a text file and print the number of
lines and its content and then search for a particular character in the file
Program:
def Paragraph():
n = ''
print('Enter 0 to go to Menu\nPress Enter to go to next line\n\n')
with open('note.txt','w') as f:
while n!='0':
n = input('Enter a line: ')
f.write(n+'\n')
def Lines():
with open('note.txt','r') as f:
l = f.readlines()
for i in range(1,len(l)):
print(f'{i}. {l[i-1]}')
print(f'Number of Lines are {len(l)-1}')
def search():
word=input("Enter the word: ")
with open('note.txt','r') as f:
l = f.read()
if word in l:
print('Found!')
16 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
else:
print("Not Found!! ")
while True:
print('''-----------------------------------------------------
MENU
1.Enter Notes
2.Display lines with number of line
3.Search
4.Exit''')
n = input('Enter you Choice: ')
print('-----------------------------------------------------')
if n == '1':
Paragraph()
elif n == '2':
Lines()
elif n=='3':
search()
elif n == '4':
break
else:
print('Enter a Valid Choice')
17 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
Output:
18 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
19 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
REPLACE SPACE WITH ‘#’ - TEXTFILE
Aim: Write a Python program to write to a text file “Notes.txt” and display the file
content by replacing space with ‘#’.
Program:
f=open('notes.txt','w')
L=['\nGood day\n',"Welcome to Python\n","All the best\n"]
f.writelines(L)
f.close()
f=open('notes.txt','r')
Lines=f.readlines()
for i in Lines:
print(i.replace(" ","#"),end=" ")
f.close()
File: Notes.txt
Output:
20 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
CHARACTER COUNTING - TEXT FILE
Aim: Write a Python program to read a text file “Notes.txt” and display the number of
vowels, consonants, uppercase and lowercase characters in the file.
Program:
with open('notes.txt','r') as f:
v=c=up=low=0
data=f.read()
print('File content',data)
for ch in data:
if ch.islower():
low+=1
elif ch.isupper():
up+=1
if ch.isalpha():
if ch.lower() in 'aeiou':
v+=1
else:
c+=1
print('Count of vowels',v)
print('Count of consonants',c)
print('Count of lower case letters',low)
21 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
print('Count of upper case letter',up)
File: Notes.txt
Output:
22 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
ADD, SEARCH, DISPLAY & UPDATE – BINARY
FILE
Aim: Write user defined functions in Python to perform Add, Search, Update and Display
records in a binary file “Emp.dat”, using the following list:
Emprec = [EmpNo, Name, Salary]
Program:
import pickle
import os
Emprec=[]
def CreateEmp():
f=open("Emp.dat","wb")
n=int(input("Enter the number of employees: "))
for i in range(n):
Empno=int(input("Enter emp no: "))
name=input("Enter Name: ")
salary=int(input("Enter salary: "))
Emprec=[Empno,name,salary]
pickle.dump(Emprec,f)
f.close()
def DisplayEmp():
f=open("Emp.dat","rb")
try:
23 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
while True:
Emprec=pickle.load(f)
print(Emprec[0],"\t\t",Emprec[1],"\t\t\t",Emprec[2])
except EOFError:
pass
f.close()
def SearchEmp(ENo):
f=open("Emp.dat","rb")
found=False
try:
while True:
Emprec=pickle.load(f)
if Emprec[0]==ENo :
print(Emprec[0],"\t\t",Emprec[1],"\t\t\t",Emprec[2])
found=True
except EOFError:
pass
if not found:
print("No such employee exists")
f.close()
def UpdateEmp(ENo):
f=open("Emp.dat","rb")
24 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
l=[]
while True:
try:
p=pickle.load(f)
l.append(p)
except EOFError:
break
f.close()
f=open("Emp.dat","wb")
r=int(input("Enter the New Salaray: "))
for i in range(len(l)):
if l[i][0]==ENo:
l[i][2]=r
break
else:
print("No such employee exists")
for x in l:
pickle.dump(x,f)
f.close()
def Main():
while True:
print("MENU".center(50,"*"))
print("1.Add Employees")
25 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
print("2.Display")
print("3.Search")
print("4.Update")
print("5.Exit")
ch=int(input("Enter your choice: "))
if ch==1:
CreateEmp()
elif ch==2:
DisplayEmp()
elif ch==3:
ENo=int(input("Enter the employee number to be searched: "))
SearchEmp(ENo)
elif ch==4:
ENo=int
(input("Enter the employee number to be updated: "))
UpdateEmp(ENo)
elif ch==5:
break
else:
print("Invalid Input")
Main()
26 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
Output:
27 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
28 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
WRITE AND READ - BINARY FILE
Aim: Write a Python program to create a binary file “Stud.dat” with roll number, name
and mark of ‘n’ students. Write the function definitions to read and display the contents of
the binary file.
Program:
import pickle
def FileCreation():
f=open('Stud.dat','wb')
n=int(input('Enter the number of students: '))
for i in range(n):
rno=int(input('Enter Roll no: '))
name=input('Enter Name: ')
marks=int(input('Enter Marks: '))
rec=[rno,name,marks]
pickle.dump(rec,f)
f.close()
def Display():
f=open('Stud.dat','rb')
try:
print('RNo\tName\tMark')
while True:
rec=pickle.load(f)
29 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
print(rec[0],'\t',rec[1],'\t',rec[2])
except EOFError:
pass
f.close()
FileCreation()
print('Contents of the file Stud.dat')
print('*******************')
Display()
Output:
30 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
EMPLOYEE RECORDS – CSV FILE
Aim: Write a Python program to add Employee records such as employee code, name
and designation into a csv file ‘employed.csv’. Also accept a name to search and display the
record.
Program:
import csv
n=int(input("No of employees: "))
with open("employed.csv","w",newline="")as f:
wo=csv.writer(f)
wo.writerow(["Code","Name","Designation"])
for i in range(n):
Ecode=int(input('Enter employee ID: '))
Ename=input("Enter the name of employee: ")
desig=input("Enter the Designation: ")
wo.writerow([Ecode,Ename,desig])
with open("employed.csv","r")as f:
r=csv.reader(f)
for i in list(r):
print(i[0],i[1],i[2],sep=" ")
name=input("Enter the name to search: ")
with open("employed.csv","r")as f:
print("Code Name Designation")
31 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
r=csv.reader(f)
for i in list(r):
if name.lower() in i[1].lower():
print(i[0],i[1],i[2],sep=" ")
Output:
32 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
WRITE & READ – CSV FILE
Aim: Write user defined functions in Python to write and read the details of book(Book
No, Name, Author, Publisher) in a library, using the CSV file “Lib.csv”.
Program:
import csv
def addbook():
f=open("Lib.csv","w")
wrobj=csv.writer(f)
wrobj.writerow(['Book No','Book Name','Author','Publisher'])
while True:
bno=input("Enter Book No:")
bname=input("Enter Book Name:")
auth=input("Enter Author:")
pub=input("Enter Publisher:")
wrobj.writerow([bno,bname,auth,pub])
ch=input("Do you want to add more records:")
if ch in 'noNo':
break
f.close()
def dispbook():
f=open("Lib.csv","r",newline="\n")
rdobj=csv.reader(f)
33 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
heading=next(rdobj)
print("Book Details".center(50,"*"))
print(" ",heading[0],"\t",heading[1],"\t",heading[2],"\t",heading[3])
print("*********************************************************************
******")
for rec in rdobj:
print(" ",rec[0],"\t",rec[1],"\t",rec[2],"\t",rec[3])
print("*********************************************************************
******")
f.close()
addbook()
dispbook()
Output:
34 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
STACK USING LIST DATA STRUCTURE
Aim: Write a menu based program to push, pop and display the record of ITEM using list
as stack data structure in Python. Record of ITEM contains the fields: Item Number, Item
Name and Price.
Program:
def push (item):
ino=int(input("Enter item no: "))
iname=input("Enter item name: ")
price=float(input("Enter Price: "))
rec=[ino,iname,price]
item.append(rec)
def pop(item):
if item==[]:
print("No more item to be deleted")
else:
rec=item.pop()
print("Deleted Items Dtails: ")
print("******************************************************")
print("Item No: ",rec[0])
print("Item Name: ",rec[1])
print("Item Price; ",rec[2])
print("******************************************************")
35 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
def display (item):
if item==[]:
print("Stack empty")
else :
print("Item Details: ")
print("******************************************************")
print("Item No \t\t Item Name \t\t Item Price")
for rec in range(len(item)-1,-1,-1):
print(item[rec][0],'\t\t',item[rec][1],'\t\t\t',item[rec][2])
print("******************************************************")
print("END OF STOCK")
def main():
while True:
print("\n","Menu")
print("******************************************************")
ch=int(input("""
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter choice (1,2,3,4) : """))
36 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
if ch==1:
push(item)
elif ch==2:
pop(item)
elif ch==3:
display(item)
elif ch==4:
break
item=[]
main()
Output:
37 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
38 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
39 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
MYSQL COMMAND – 1
i) Create table Student with the following structure
ii) Insert the following set of data in the table Student
iii) Add a new column Stream with data type char (2).
iv) Modify the data type of the column Stream to varchar (5).
40 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
v) Remove the column mobile.
vi) Set the value of the column Stream to ‘EC’ for RollNo less than 3.
vii) Set the value of the column Stream to ‘CC’ for whom the stream is not yet assigned.
viii) Increase the mark of Deepa by 2%.
ix) Remove the details of the students whose mark is less than 70.
x) Drop the table Student.
41 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
MYSQL COMMAND - 2
i) Display the details of all the items whose rate is more than Rs.15/-;
ii) Display sum of quantity of all Ball Pen items.
42 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
iii) Display the details of all the items in descending order of LastBuy.
iv) Display Dcode and total quantity of items under each Dcode
v) Display item name and the corresponding dealer name for each item.
43 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
MYSQL COMMAND – 3
i) Display the details of all the items whose manufacturer is Dell or Acer
44 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
ii) Display the different manufacturers from the table item
iii) Display the details of all customers located in Delhi in the alphabetical order of
their names
iv) Display Manufacturer and total price of items for each Manufacturer
45 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
v) Display item name, Manufacturer and the customer’s name for all items.
46 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
MYSQL COMMAND – 4
i) Display the names of all sender from New Delhi
ii) Display the name of the city and the number of senders in each city
47 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
iii) Display the details of the recipients located in the city that ends with ‘i’;
iv) Display the details of the recipients whose SID is ND01.
v) Display sender name and the names of the recipients to whom the mails were
sent.
48 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
MYSQL COMMAND – 5
i) Display the names of the cars that do not start with ‘In’.
ii) Display the details of the cars with charges in the range 15 to 35.
49 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
iii) Display the count of distinct make FROM rentacar.
iv) Display the make and the highest capacity in each make.
v) Display the car name and customer who hired the car.
50 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATABASE CONNECTIVITY – 1
Aim: Write a Python program to retrieve the records from the Employee table
Table: Employee
Program:
import mysql.connector as mc
mydb = mc.connect(host='localhost', user = 'root', password='', charset="utf8",
database='cs2024')
cur = mydb.cursor()
cur.execute('SELECT * FROM employee')
records = cur.fetchall()
for rec in records:
print (rec [0], rec [1], rec [2], rec [3], str (rec [4]))
mydb.close()
Output:
51 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATABASE CONNECTIVITY – 2
Aim: Write a program to retrieve the details of the employees in the ‘HR’ department
Table: Employee
Program:
import mysql.connector as mc
mydb = mc.connect(host='localhost', user = 'root', password='', charset="utf8",
database='cs2024')
cur=mydb.cursor()
cur.execute('select * from Employee where Designation="HR"')
records=cur.fetchall()
for rec in records:
print (rec [0], rec [1], rec [2], rec [3], str (rec [4]))
mydb.close()
Output:
52 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATABASE CONNECTIVITY – 3
Aim: Write a program to retrieve the details of recently joined 3 employees.
Table: Employee
Program:
import mysql.connector as mc
mydb = mc.connect(host='localhost', user = 'root', password='', charset="utf8",
database='cs2024')
cur=mydb.cursor()
cur.execute('select * from Employee order by salary desc')
records=cur.fetchall()
for rec in records:
if rec[2] == records[0][2]:
print(rec[0],rec[1],rec[2],rec[3],str(rec[4]))
mydb.close()
Output:
53 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATABASE CONNECTIVITY – 4
Aim: Write a program to retrieve the details of the most highly paid employee
Table: Employee
Program:
import mysql.connector as mc
mydb = mc.connect(host='localhost', user = 'root', password='', charset="utf8",
database='cs2024')
cur=mydb.cursor()
cur.execute('select * from Employee order by salary desc')
records=cur.fetchmany(1)
for rec in records:
print(rec[0],rec[1],rec[2],rec[3],str(rec[4]))
mydb.close()
Output:
54 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
DATABASE CONNECTIVITY – 5
Aim: Write a program to increase the salary of all employees by 10%
Table: Employee
Program:
import mysql.connector as mc
mydb = mc.connect(host='localhost', user = 'root', password='', charset="utf8",
database='cs2024')
cur=mydb.cursor()
cur.execute("Update Employee set salary= salary + salary*10/1000")
mydb.commit()
cur.execute("select * from Employee")
records=cur.fetchall()
for rec in records:
print(rec[0],rec[1],rec[2],rec[3],str(rec[4]))
mydb.close()
Output:
55 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
EXCEPTION HANDLING
Aim: Write a program to divide two numbers
Program:
num1=0
while num1!=100:
try:
num1=eval(input("Enter dividend: "))
num2=eval(input("Enter divisor: "))
num3=num1//num2
print("The quotient is=",num3)
print()
except NameError:
print("Variable not present")
except ZeroDivisionError:
print("Division by Zero not defined")
except SyntaxError:
print("Syntax Error")
except TypeError:
print("Data types do not match")
else:
print("Execution is normal")
print("Done ! ! ! !")
print()
56 | P a g e Al-Ameen International Public School
CLASS XII Computer Science Record 2024-2025
Output:
57 | P a g e Al-Ameen International Public School