Ranjeet Practical Project File
Ranjeet Practical Project File
COMPUER SCIENCE
PRACTICAL FILE
BASED
ON
(Session -2023-24)
SUBMITTED by : RANJEET
CLASS : 12th
ROLL NO :A
ACKNOWLEDGEMENT
I wish to express my deep sense of gratitude and indebtedness to our learned teacher Mr, Mahinder Aggrawal ,
PGT COMPUTER SCIENCE, (HERMANN GMEINER SCHOOL) for his invaluable help, advice and guidance in the
preparation of this project.
I am also greatly indebted to our principal and school authorities for providing me with the facilities and requisite
laboratory conditions for making this practical file.
I also extend my thanks to a number of teachers ,my classmates and friends who helped me to complete this
practical file successfully.
[AARYAN]
Page 1
CERTIFICATE
This is to certify that [RANJEET] , student of Class XII A, [HERMANN GMEINER SCHOOL] has completed
the PRACTICAL FILE during the academic year 2023-24 towards partial fulfillment of credit for the Computer Science
practical evaluation of CBSE SSCE-2024 and submitted satisfactory report, as compiled in the following pages, under my
supervision.
Index
SNo. Practical Date Signature
1 Write a python program using a function to print factorial number series from n to m numbers.
2 Write a python program to accept the username "Admin" as the default argument and password 123
3 Write a python program to demonstrate the concept of variable length argument to calculate the product
4 Create a text file "intro.txt" in python and ask the user to write a single line of text by user input.
5 Write a program to count a total number of lines and count the total number of lines starting with 'A', 'B',
6 Write a program to replace all spaces from text with - (dash) from the file intro.txt.
7 Write a program to know the cursor position and print the text according to the below-given
specifications:
Page 2
Move the cursor to the next 10 characters
●
8 Write a program to store customer data into a binary file cust.dat using a dictionary and print them on
screen after reading them. The customer data contains ID as key, and name, city as values.
9 Write a program to update a record from student.dat file by its rollno. Display the updated record on
screen.
10 Write a program to write data into binary file marks.dat and display the records of students who scored
11 Read a CSV file top5.csv and print the contents in a proper format. The data for top5.csv file are as
following:
12 Read a CSV file top5.csv and print them with tab delimiter. Ignore first row header to print in tabular
form.
13 Read a CSV file students.csv and print them with tab delimiter. Ignore first row header to print in tabular
form.
StudentID Integer
StudentName String
Score Integer
Page 3
3 Data Structure - Stack
14 Write a program to implement a stack for the employee details (empno, name).
15 Write a python program to check whether a string is a palindrome or not using stack.
5 Python-MySQL Connectivity
● Create a table students with the specifications - ROLLNO integer, STNAME character(10) in
22 Perform all the operations with reference to table ‘students’ through MySQL-Python connectivity.
23 Write a menu-driven program to store data into a MySQL database named shop and table customer as
following:
1. Add customer details
2. Update customer details
3. Delete customer details
4.
Display all customer details
24 Modify the above program and display the customer details based on the following menu:
1. Display customer details by city
2. Display customer details by bill amount
3. Display customer details by name
4. Display customer details by category
1. Write a python program using a function to print factorial number series from n to m numbers.
def facto():
f=1
Page 4
for i in range(1,n+1):
f*=i
facto()
2. Write a python program to accept username "Admin" as default argument and password 123 entered by user to allow login into the system.
def user_pass(password,username="Admin"):
if password=='123':
else:
print("Password is incorrect!!!!!!")
user_pass(password)
3. Write a python program to demonstrate the concept of variable length argument to calculate product and power of the first 10 numbers.
def sum10(*n):
total=0
for i in n:
total=total + i
sum10(1,2,3,4,5,6,7,8,9,10)
Page 5
def product10(*n):
pr=1
for i in n:
pr=pr * i
product10(1,2,3,4,5,6,7,8,9,10)
def program4():
f = open("intro.txt","w")
f.write(text)
f.close(
program4()
5. Write a program to count a total number of lines and count the total number of lines starting with 'A', 'B', and 'C' from the file myfile.txt.
def program5():
data=f1.readlines()
if lines[0]=='A':
cnt_A+=1
if lines[0]=='B':
cnt_B+=1
if lines[0]=='C':
cnt_C+=1
Page 6
print("Total Number of lines strating with A are:",cnt_A)
program5()
6. Write a program to replace all spaces from text with - (dash) from the file intro.txt.
def program6():
cnt = 0
data = f1.read()
data=data.replace(' ','-')
f1.write(data)
print(f1.read())
program6()
7. Write a program to know the cursor position and print the text according to the below-given specifications:
b. Move the cursor to 4th position e. Print the current cursor position
def program7():
f = open("intro.txt","r")
print(f.tell())
f.seek(4,0)
print(f.read(5))
f.seek(10,0)
print(f.tell())
print(f.seek(7,0))
print(f.read(10))
program7()
Page 7
8. Create a binary file client.dat to hold records like ClientID, Client name, and Address using the dictionary. Write functions to write data,
import pickle
rec={}
def file_create():
f=open("client.dat","wb")
rec={cno:[cname,address]}
pickle.dump(rec,f)
def read_data():
f = open("client.dat","rb")
print("*"*78)
rec=pickle.load(f)
for i in rec:
print(rec[i])
file_create()
read_data()
9. Write a program to update a record from student.dat file by its rollno. Display the updated record on screen.
import pickle as p
Page 8
rec=[]
found=0
f=open("student.dat","rb+")
r=p.load(f)
print("Existing Record:",r)
for i in r:
if ro==i[0]:
found=1
break
if found==0:
else:
f.seek(0)
p.dump(r,f)
print('Record Updated...')
f.close()
f1=open('student.dat','rb+')
r=p.load(f1)
print("Updated Record:",r)
f1.close()
10. Write a program to write data into binary file marks.dat and display the records of students who scored more than 95 marks.
import pickle
def search_95plus():
f = open("marks.dat","ab")
while True:
Page 9
rn=int(input("Enter the rollno:"))
rec=[]
data=[rn,sname,marks]
rec.append(data)
pickle.dump(rec,f)
break
f.close()
f = open("marks.dat","rb")
cnt=0
try:
while True:
data = pickle.load(f)
for s in data:
if s[2]>95:
cnt+=1
print("Record:",cnt)
print("RollNO:",s[0])
print("Name:",s[1])
print("Marks:",s[2])
except Exception:
f.close()
search_95plus()
11. Read a CSV file top5.csv and print the contents in a proper format. The data for top5.csv file are as following:
Page 10
from csv import reader
def pro1():
with open("top5.csv","r") as f:
d = reader(f)
data=list(d)
for i in data:
print(i)
pro1()
12. Read a CSV file top5.csv and print them with tab delimiter. Ignore first row header to print in tabular form.
def pro12():
f = open("e:\\top5.csv","r")
dt = reader(f,delimiter=',')
headr_row=next(dt)
data = list(dt)
f.close()
for i in data:
for j in i:
print(j,"\t",end=" ")
print()
pro12()
13. Read a CSV file students.csv and print them with tab delimiter. Ignore first row header to print in tabular form.
Score Integer
#Create Header First
f = open("result.csv","w",newline='\n')
dt = writer(f)
dt.writerow(['Student_ID','StudentName','Score'])
f.close()
Page 11
#Insert Data
f = open("result.csv","a",newline='\n')
while True:
dt = writer(f)
dt.writerow([st_id,st_name,st_score])
ch=ch.lower()
if ch !='y':
break
f.close()
pro13()
def check_stack_isEmpty(stk):
if stk==[]:
return True
else:
return False
s=[]
def main_menu():
while True:
Page 12
print("Stack Implementation")
print("1 - Push")
print("2 - Pop")
print("3 - Peek")
print("4 - Display")
print("5 - Exit")
if ch==1:
push(s,el)
elif ch==2:
e=pop_stack(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
print("Element popped:",e)
elif ch==3:
e=pop_stack(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
elif ch==4:
display(s)
elif ch==5:
break
else:
print("Sorry,invalid option")
Page 13
def push(stk,e):
stk.append(e)
top = len(stk)-1
def display(stk):
if check_stack_isEmpty(stk):
print("Stack is Empty")
else:
top = len(stk)-1
print(stk[top],"-Top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_stack(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
e = stk.pop()
if len(stk)==0:
top = None
else:
top = len(stk)-1
return e
def peek(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
top = len(stk)-1
return stk[top]
Page 14
15. Write a program to implement a stack for the employee details (empno, name).
stk=[]
top=-1
def line():
print('~'*100)
def isEmpty():
global stk
if stk==[]:
print("Stack is empty!!!")
else:
None
def push():
global stk
global top
stk.append([empno,ename])
top=len(stk)-1
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_ele():
Page 15
global stk
global top
if top==-1:
isEmpty()
else:
stk.pop()
top=top-1
def main():
while True:
line()
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
if ch==1:nm
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
elif ch==4:
break
else:
print("Invalid Choice")
16. Write a python program to check whether a string is a palindrome or not using stack.
stack = []
Page 16
top = -1
# push function
def push(ele):
global top
top += 1
stack[top] = ele
# pop function
def pop():
global top
ele = stack[top]
top -= 1
return ele
def isPalindrome(string):
global stack
length = len(string)
mid = length // 2
i=0
push(string[i])
i += 1
# Checking if the length of the string is odd then neglect the middle character
if length % 2 != 0:
i += 1
Page 17
while i < length:
ele = pop()
if ele != string[i]:
return False
i += 1
return True
if isPalindrome(string):
else:
Page 18
M006 Gehraiyaan Romance 2022/02/11 150000 120000
c) Display movieid, moviename, total_eraning by showing the business done by the movies. Claculate the business done by movie using the sum of
d) Display movieid, moviename and productioncost for all movies with productioncost greater thatn 150000 and less than 1000000.
f) Display the list of movies which are going to release in February, 2022.
Answers:
d) select movie_id,moviename, productioncost from movie where producst is >150000 and <1000000;
Page 19
e) select moviename from movie where type ='action' or type='romance';
Page 20
Answers:
students of Class XI and Class XII. Students of each class are asked to join any one of the
four teams – Team Titan, Team Rockers, Team Magnet and Team Hurricane. During summer
vacations, various matches will be conducted between these teams. Help your sports teacher
to do the following:
a. It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique identification of a team.
b. Each TeamID should have its associated name (TeamName), which should be a string of length not less than 10 characters.
d) As per the preferences of the students four teams were formed as given below. Insert these four rows in TEAM table:
Page 21
e) Show the contents of the table TEAM using a DML statement.
f) Now create another table MATCH_DETAILS and insert data as shown below. Choose appropriate data types and constraints for each attribute.
M1 2021/12/20 1 2 107 93
M3 2021/12/22 1 3 86 81
M4 2021/12/23 2 4 65 67
M5 2021/12/24 1 4 52 88
M6 2021/12/25 2 3 97 68
Answers:
(teamid int(1),
c) desc team;
Page 22
Inserting data:
●
Page 23
Creating another table:
●
Page 24
4. Write following queries:
a) Display the matchid, teamid, teamscore whoscored more than 70 in first ining along with team name.
Answers:
Page 25
c) select matchid, teamname, firstteamid, secondteamid, matchdate from match_details, team where
match_details.firstteamid = team.teamid;
team.teamname in ('Aandhi','Shailab');
Page 26
S005 Ballpen 102 100 10 2018/04/22
b) Display maximum price of items for each dealer individually as per dcode from stock.
d) Display average price of items for each dealer individually as per doce from stock which avergae price is more than 5.
Answers:
Page 27
c) select * from stock order by item desc;
● Create a table students with the specifications - ROLLNO integer, STNAME character(10) in MySQL and perform the following operations:
o
Insert two records in it
Page 28
o
Display the contents of the table
2. Perform all the operations with reference to table ‘students’ through MySQL-Python connectivity.
Answers:
import pymysql as ms
def c_database():
try:
c.execute("use {}".format(dn))
except Exception as a:
print("Database Error",a)
def d_database():
try:
except Exception as a:
Page 29
#Function to create Table
def c_table():
try:
rollno int(3),
stname varchar(20)
);
''')
except Exception as a:
def e_data():
try:
while True:
c.execute("use {}".format('school'))
db.commit()
if choice in "Nn":
break
except Exception as a:
Page 30
def d_data():
try:
data=c.fetchall()
for i in data:
print(i)
except Exception as a:
db=ms.connect(host="localhost",user="root",password="root")
c=db.cursor()
while True:
print("MENU\n1. Create Database\n2. Drop Database \n3. Create Table\n4. Insert Record \n5. Display Entire Data\n6. Exit")
if choice==1:
c_database()
elif choice==2:
d_database()
elif choice==3:
c_table()
elif choice==4:
e_data()
elif choice==5:
d_data()
elif choice==6:
break
else:
Output:
Page 31
2. using mysqlconnector
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='school')
cn=db.cursor()
def insert_rec():
try:
while True:
sname=input("Enter name:")
marks=float(input("Enter marks:"))
gr=input("Enter grade:")
db.commit()
if ch in 'Nn':
break
except Exception as e:
print("Error", e)
Page 32
def update_rec():
try:
gr=input("Enter Grade:")
db.commit()
except Exception as e:
print("Error",e)
def delete_rec():
try:
db.commit()
except Exception as e:
print("Error",e)
def view_rec():
try:
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='school')
cn=db.cursor()
def insert_rec():
try:
while True:
Page 33
rn=int(input("Enter roll number:"))
sname=input("Enter name:")
marks=float(input("Enter marks:"))
gr=input("Enter grade:")
db.commit()
if ch in 'Nn':
break
except Exception as e:
print("Error", e)
def update_rec():
try:
gr=input("Enter Grade:")
db.commit()
except Exception as e:
print("Error",e)
def delete_rec():
Page 34
try:
db.commit()
except Exception as e:
print("Error",e)
def view_rec():
try:
data=c.fetchall()
for i in data:
print(i)
except Exception as e:
print("Error",e)
while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5. Exit")
if ch==1:
insert_rec()
elif ch==2:
update_rec()
Page 35
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
except Exception as e:
print("Error",e)
while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5. Exit")
if ch==1:
insert_rec()
elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
Page 36
else:
Output:
Page 37
3. Write a menu-driven program to store data into a MySQL database named shop and table customer as following:
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='mydb')
cn=db.cursor()
def insert_rec():
try:
while True:
cname=input("Enter name:")
city=input("Enter city:")
cat=input("Enter category:")
db.commit()
if ch in 'Nn':
break
except Exception as e:
print("Error", e)
def update_rec():
try:
data=cn.fetchall()
for i in data:
ci=i[0]
Page 39
cna=i[1]
ct=i[2]
b=i[3]
c=i[4]
if cid==ci:
if ch_cname.lower()=='y':
else:
cname=cna
if ch_city.lower()=='y':
else:
city=ct
if ch.lower()=='y':
else:
bill_amt=b
if ch_cat.lower()=='y':
else:
cat=c
db.commit()
Page 40
else:
except Exception as e:
print("Error",e)
def delete_rec():
try:
db.commit()
except Exception as e:
print("Error",e)
def view_rec():
try:
data=cn.fetchall()
cnt=0
for i in data:
cnt=cnt+1
print("Record:",cnt)
print('~'*50)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
print('~'*50)
except Exception as e:
print("Error",e)
Page 41
while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5. Exit")
if ch==1:
insert_rec()
elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
Output:
Page 42
Page 43
4. Modify the above program and display the customer details based on the following menu:
Page 44
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='mydb')
cn=db.cursor()
def byCity():
try:
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
except Exception as e:
print("Error",e)
def byBillAmt():
try:
Page 45
ba=input("Enter the bill amount:")
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
except Exception as e:
print("Error",e)
def byName():
try:
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
Page 46
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
except Exception as e:
print("Error",e)
def byCat():
try:
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
Page 47
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
except Exception as e:
print("Error",e)
while True:
print('''
MENU
5.Exit
''')
if ch==1:
byCity()
elif ch==2:
byBillAmt()
elif ch==3:
byName()
elif ch==4:
byCat()
elif ch==5:
Page 48
break
else:
Output:
Page 49
Page 50