Report File Computer 240102 232550
Report File Computer 240102 232550
REPORT FILE
Computer Science(CS-083)
CLASS XII
SESSION 2023-24
Submitted By Submitted To
Board Roll No: MV Singh
Name : DISHIKA (PGT Computer Science)
CERTIFICATE
This is to certify that Dishika of class 12th
of Mukat Public School, Rajpura, has
successfully completed his Computer
Science Project File for AISSCE as
prescribed by CBSE in the year 2023-24.
Mr. MV Singh
(PGT Computer Science)
Signature of Signature of
________________ ________________
1
ACKNOWLEDGEMENT
I would like to express my deep sense of
thanks and gratitude to my guide Mr. MV
Singh for guiding me immensely through the
course of completion of this project. I also
thank my parents for their motivation and
support. I thank my classmates for their
timely help and support for compilation of
this project.
Name: Dishika
Class: 12th A
2
TABLE OF CONTENTS
S.No Name of the Exercises Page.No
1. Creating a menu driven program to perform arithmetic operations. 4
10. Creating a python program to copy particular lines of a text file into another 15
text file.
11. Creating a python program to create and search records in binary file. 16
12. Creating a python program to create and update/modify records in binary file. 18
13. Creating a python program to create and search employee's record in csv file. 20
3
PROGRAM 1: CREATING A MENU DRIVEN PROGRAM TO
PERFORM ARITHMETIC OPERATIONS
print("1.Addition")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")
c=int(input("Enter your choice:"))
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
if c==1:
ans=a+b
print(ans)
elif c==2:
ans=a-b
print(ans)
elif c==3:
ans=a*b
print(ans)
elif c==4:
if b==0:
print("Enter any other number except 0")
else:
ans=a/b
print(ans)
else:
print("Invalid")
SAMPLE OUTPUT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice:3
Enter first number:50
Enter second number:42
2100
4
PROGRAM 2: CREATING A PYTHON PROGRAM TO DISPLAY
FIBONACCI SERIES
first=0
second=1
num=int(input("how many fibonacci numbers you
want to display?"))
if num<=0:
print("enter a positive integer")
else:
print(first)
print(second)
for i in range(2,num):
third=first+second
first=second
second=third
print(third)
SAMPLE OUTPUT:
how many fibonacci numbers you want to display?7
0
1
1
2
3
5
8
5
PROGRAM 3: CREATING A MENU DRIVEN PROGRAM TO
FIND FACTORIAL AND SUM OF LIST OF NUMBERS USING
FUNCTION
def factorial(num):
f=1
if num<0:
print("number must be positive")
elif num==0:
print("factorial of 0 is 1")
else:
for i in range(1,num+1):
f=f*i
print("the factorial is:" ,f)
def sumlist(l):
ln=len(l)
sum=0
for i in range(ln):
sum=sum+l[i]
print("sum of list:",sum)
print("1. To find factorial of a number")
print("2. To find sum of list elements")
c=int(input("enter your choice:"))
if c==1:
n=int(input("enter a number to find its factorial:"))
factorial(n)
elif c==2:
list=eval(input("enter a list to find sum of elements of
list:"))
sumlist(list)
6
SAMPLE OUTPUT 1:
1. To find factorial of a number
2. To find sum of list elements
enter your choice:1
enter a number to find its factorial:6
the factorial is: 720
SAMPLE OUTPUT 2:
1. To find factorial of a number
2. To find sum of list elements
enter your choice:2
enter a list to find sum of elements of
list:[12,43,23,54,29]
sum of list: 161
7
PROGRAM 4: CREATING A PYTHON PROGRAM TO
IMPLEMENT RETURNING VALUE(S) FROM FUNCTION
def check(num1,num2):
if(num1*5)<(num2*5):
return num1
else:
return num2
a=int(input("enter first number:"))
b=int(input("enter second number:"))
r=check(a,b)
print(r)
SAMPLE OUTPUT:
enter first number:54
enter second number:87
54
8
PROGRAM 5: CREATING A PYTHON PROGRAM TO
IMPLEMENT MATHEMATICAL FUNCTIONS
import math
def square(num):
s=math.pow(num,2)
return s
def log(num):
s=math.log10(num)
return s
def quad(x,y):
s=math.sqrt(x**2+y**2)
return s
print("the square of number
is:",square(7))
print("the log of number is:",log(10))
print("the quad of number is:",quad(5,2))
SAMPLE OUTPUT:
the square of number is: 49.0
the log of number is: 1.0
the quad of number is: 5.385164807134504
9
PROGRAM 6: CREATING A PYTHON PROGRAM TO
GENERATE RANDOM NUMBER BETWEEN 1 TO 6
import random
while True:
Choice=input("\nDo you want to roll the
dice (y/n):”)
num=random.randint(1,6)
if Choice=='y':
print("\nYour Number is:",num)
else:
break
SAMPLE OUTPUT:
Do you want to roll the dice (y/n): y
Your Number is: 5
Do you want to roll the dice (y/n): y
Your Number is: 6
Do you want to roll the dice (y/n): y
Your Number is: 6
Do you want to roll the dice (y/n): n
10
PROGRAM 7: CREATING A PYTHON PROGRAM
TO READ A TEXT FILE LINE BY LINE AND DISPLAY
EACH WORD SEPARATED BY '#'
f=open("story.txt",'r')
file=f.readlines()
for line in file:
words=line.split()
for i in words:
print(i+'#',end='')
print("")
f.close()
SAMPLE OUTPUT:
Story.txt:
With#a#blink#of#eye,time#flew#
The#naughty#groups#replaced#with#mature#cre
w#
Hearts#full#of#innocence#and#love#now#hold#
Just#work#and#talk#mode#
11
PROGRAM 8: CREATING A PYTHON PROGRAM TO READ A
TEXT FILE AND DISPLAY THE NUMBER OF
VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE
CHARACTERS.
f=open("story.txt",'r')
Contents=f.read()
Vowels=0
Consonants=0
Lower_case=0
Upper_case=0
for ch in Contents:
if ch in 'aeiouAEIOU':
Vowels=Vowels+1
if ch in
'bodfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
Consonants=Consonants+1
if ch.islower():
Lower_case=Lower_case+1
if ch.isupper ():
Upper_case=Upper_case+1
f.close()
print("The total numbers of vowels in the file:", Vowels)
print("The total numbers of consonants in the file:",
Consonants)
print("The total numbers of uppercase in the file:",
Upper_case)
print("The total numbers of lowercase in the file:",
Lower_case)
12
SAMPLE OUTPUT:
Story.txt:
13
PROGRAM 9: CREATING PYTHON PROGRAM TO DISPLAY
SHORT WORDS FROM A TEXT FILE
f=open("story.txt")
s=f.read()
w=s.split()
print("The follwoing words are less than 5
characters")
for i in w:
if len(i)<5:
print(i,end=" ")
f.close()
SAMPLE OUTPUT:
story.txt:
14
PROGRAM 10: CREATING A PYTHON PROGRAM TO COPY
PARTICULAR LINES OF A TEXT FILE INTO AN ANOTHER TEXT
FILE
f1=open("story.txt",'r')
f2=open("new.txt","w")
s=f1.readlines()
for i in s:
if i[0]=='A' or i[0]=='a':
f2.write(i)
print("Written Successfully")
f1.close()
f2.close()
SAMPLE OUTPUT:
story.txt:
Program output:
new.txt:
15
PROGRAM 11: CREATING A PYTHON PROGRAM TO CREATE
AND SEARCH RECORDS IN BINARY FILE
import pickle
def Create() :
f=open ( "students.dat",'ab')
opt='y'
while opt =='y':
RollNo=int(input("Enter roll number:"))
Name=input("Enter Name:")
l=[RollNo,Name]
pickle.dump(l,f)
opt=input("do u want to add another student
detail(y/n):")
f.close()
def Search():
f=open("students.dat",'rb')
no=int(input("enter roll no of student to search:"))
found=0
try:
while True:
s=pickle.load(f)
if s[0]==no:
print("the details are:",s)
found=1
break
except:
f.close()
if found==0:
print("not found")
Create()
Search()
16
SAMPLE OUTPUT:
Enter roll number:1
Enter Name:Arya
do u want to add another student detail(y/n):y
Enter roll number:2
Enter Name:Angel
do u want to add another student detail(y/n):y
Enter roll number:3
Enter Name:Dishika
do u want to add another student detail(y/n):y
Enter roll number:4
Enter Name:Khushi
do u want to add another student detail(y/n):n
enter roll no of student to search:3
the details are: [3, 'Dishika']
17
PROGRAM 12: CREATING A PYTHON PROGRAM TO CREATE
AND UPDATE/MODIFY RECORDS IN BINARY FILE
import pickle
def Create():
f=open("marks.dat",'ab')
opt='y'
while opt=='y':
Rollno=int(input("enter roll number:"))
name=input("enter name:")
mark=int(input("enter marks:"))
l=[Rollno,name,mark]
pickle.dump(l,f)
opt=input("do u want to add more details(y/n):")
f.close()
def Update():
f=open("marks.dat",'rb+')
no=int(input("enter student's roll number to modify marks:"))
found=0
try:
while True:
pos=f.tell()
s=pickle.load(f)
if s[0]==no:
print("the details are:",s)
s[2]=int(input("enter new mark to be update:"))
f.seek(pos)
pickle.dump(s,f)
found=1
f.seek(pos)
print("marks updated succesfully and details are:",s)
break
except:
f.close()
if found==0:
print("not found")
Create()
Update()
18
SAMPLE OUTPUT:
enter roll number:1
enter name:Arya
enter marks:70
do u want to add more details(y/n):y
enter roll number:2
enter name:Angel
enter marks:80
do u want to add more details(y/n):y
enter roll number:3
enter name:Dishika
enter marks:95
do u want to add more details(y/n):y
enter roll number:4
enter name:Khushi
enter marks:95
do u want to add more details(y/n):n
enter student's roll number to modify marks:2
the details are: [2, 'Angel', 80]
enter new mark to be update:85
marks updated succesfully and details are: [2, 'Angel', 85]
19
PROGRAM 13:CREATING A PYTHON PROGRAM TO CREATE
AND SEARCH EMPLOYEE’S RECORD IN CSV FILE
import csv
def Create():
f=open("emp.csv",'a',newline='')
w=csv.writer(f)
opt='y'
while opt=='y':
no=int(input("enter employee number:"))
name=input("enter employee name:")
sal=float(input("enter employee salary:"))
l=[no,name,sal]
w.writerow(l)
opt=input("do u want to continue(y/n):")
f.close()
def Search():
f=open("emp.csv",'r',newline='\r\n')
no=int(input("enter employee number to search"))
found=0
row=csv.reader(f)
for data in row:
if data[0]==str(no):
print("\nemployee details are:")
print("name:",data[1])
print("salary",data[2])
found=1
break
if found==0:
print("not found")
f.close()
Create()
Search()
20
SAMPLE OUTPUT:
enter employee number:1
enter employee name:Akash
enter employee salary:25000
do u want to continue(y/n):y
enter employee number:2
enter employee name:Akshay
enter employee salary:30000
do u want to continue(y/n):y
enter employee number:3
enter employee name:Rohan
enter employee salary:35000
do u want to continue(y/n):y
enter employee number:4
enter employee name:Rehan
enter employee salary:36000
do u want to continue(y/n):n
enter employee number to search3
21
PROGRAM 14: CREATING A PYTHON TO IMPLEMENT STACK
OPERATIONS(LIST)
def Push():
docid=int(input("enter the doctor id:"))
docname=input("enter name of doctor:")
mob=int(input("enter mobile number of the
doctor:"))
special=input("enter the specialization:")
if special=='ENT':
stack.append([docid,docname])
def Pop():
if stack==[]:
print("stack is empty")
else:
print("the deleted doctor detail is:",stack.pop())
def Peek():
if stack==[]:
print("stack is empty")
else:
top=len(stack)-1
print("the top of stack is:",stack[top])
def Disp():
if stack==[]:
print("stack is empty")
else:
22
top=len(stack)-1
for i in range(top,-1,-1):
print(stack[i])
stack=[]
ch='y'
print("performing stack operation using list")
while ch=='y' or ch=='Y':
print()
print("1.PUSH")
print("2.POP")
print("3.PEEK")
print("4.DISP")
opt=int(input("enter your choice:"))
if opt==1:
Push()
elif opt==2:
Pop()
elif opt==3:
Peek()
elif opt==4:
Disp()
else:
print("invalid choice")
ch=input("\nDo you want to perform another
operation(y/n):")
23
SAMPLE OUTPUT:
performing stack operation using list
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:1
enter the doctor id:1
enter name of doctor:Akash
enter mobile number of the doctor:9988
enter the specialization:Cardio
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:1
enter the doctor id:2
enter name of doctor:Arun
enter mobile number of the doctor:9879
enter the specialization:ENT
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:1
enter the doctor id:3
enter name of doctor:Akshay
24
enter mobile number of the doctor:9887
enter the specialization:ENT
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:4
[3, 'Akshay']
[2, 'Arun']
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:3
the top of stack is: [3, 'Akshay']
1.PUSH
2.POP
3.PEEK
4.DISP
enter your choice:2
the deleted doctor detail is: [3, 'Akshay']
25
PROGRAM 15:CREATING A PYTHON PROGRAM TO
IMPLEMENT STACK OPERATIONS(DICTIONARY)
def Push(stk,d):
for i in d:
if d[i]>70:
stk.append(i)
def Pop(stk):
if stk==[]:
return "Stack is empty"
else:
print("the deleted element is:",end='')
return stk.pop()
def Disp():
if stk==[]:
print("stack is empty")
else:
top=len(stk)-1
for i in range(top,-1,-1):
print(stk[i])
ch='y'
d={}
stk=[]
print("Performing stack opertaions using dictionary\n")
while ch=='y' or ch=='Y':
print()
print("1.PUSH")
print("2.POP")
print("3.DISP")
opt=int(input("enter your choice:"))
if opt==1:
d['Palak']=int(input("enter mark of palak:"))
d['Khushi']=int(input("enter mark of khushi:"))
d['Dishika']=int(input("enter mark of dishika:"))
d['Prachi']=int(input("enter mark of prachi:"))
d['Shweta']=int(input("enter mark of shweta:"))
Push(stk,d)
elif opt==2:
r=Pop(stk)
print(r)
elif opt==3:
Disp()
opt=input("do u want to perform another operation(y/n):")
26
SAMPLE OUTPUT:
Performing stack opertaions using dictionary
1.PUSH
2.POP
3.DISP
enter your choice:1
enter mark of palak:70
enter mark of khushi:95
enter mark of dishika:95
enter mark of prachi:69
enter mark of shweta:70
do u want to perform another operation(y/n):y
1.PUSH
2.POP
3.DISP
enter your choice:3
Dishika
Khushi
do u want to perform another operation(y/n):y
1.PUSH
2.POP
3.DISP
enter your choice:2
the deleted element is:Dishika
do u want to perform another operation(y/n):n
27
PROGRAM 16: CREATING A PYTHON PROGRAM TO
INTEGRATE MYSQL WITH PYTHON.
(CREATING DATABASE AND TABLE)
import mysql.connector
def createdb():
con=mysql.connector.connect(host='localhost',user='root',password='root')
try:
if con.is_connected():
cur=con.cursor()
q="create database employees"
cur.execute(q)
print("employees database created successfully")
except:
print("database name already exists")
con.close()
def createtable():
con=mysql.connector.connect(host='localhost',user='root',password='root',database='empl
oyees')
if con.is_connected():
cur=con.cursor()
q="create table emp(eno int primary key,ename varchar(20),gender varchar(3),salary
int)"
cur.execute(q)
print("emp table created successfully")
else:
print("table name already exists")
con.close()
ch='y'
while ch=='y' or ch=='Y':
print("\nInterfacing python with mysql")
print("1.To create database")
print("2.To create table")
opt=int(input("enter your choice:"))
if opt==1:
createdb()
elif opt==2:
createtable()
else:
print("invalid choice")
opt=input("do u want to perform another operation(y/n):")
28
SAMPLE OUTPUT:
Interfacing python with mysql
1.To create database
2.To create table
Enter your choice:1
employees database created successfully
do u want to perform another operation(y/n):y
29
PROGRAM 17: CREATING A PYTHON PROGRAM TO
INTEGRATE MYSQL WITH PYTHON.
(INSERTING RECORDS AND DISPLAYING RECORDS)
import mysql.connector
con=mysql.connector.connect(host='localhost',user='ro
ot',password='root',database='employees')
if con.is_connected():
cur=con.cursor()
opt=’y’
while opt==’y’:
no=int(input(“enter employee number:”))
name=input(“enter employee name:”)
gender=input(“enter employee gender(M/F):”)
salary=int(input(“enter employee salary:”))
query=”insert into emp
values({},’{}’,’{},{}).format(no,name,gender,salary)
cur.execute(query)
con.commit()
print(“record stored successfully”)
opt=input(“do u want to add another employee
details(y/n):”)
query=”select*from emp”;
cur.execute(query)
data=cur.fetchall()
for I in data:
print(i)
con.close()
30
SAMPLE OUTPUT:
enter employee number:1
enter employee name:Arun
enter employee gender(M/F):M
enter employee salary:20000
record stored successfully
do u want to add another employee details(y/n):y
enter employee number:1
enter employee name:Anjali
enter employee gender(M/F):F
enter employee salary:27000
record stored successfully
do u want to add another employee details(y/n):y
enter employee number:3
enter employee name:Akash
enter employee gender(M/F):M
enter employee salary:24000
record stored successfully
do u want to add another employee details(y/n):n
(1,‘Arun’,‘M’,20000)
(2,‘Anjali’,‘F’,27000)
(3,‘Akash’,‘M’,24000)
31
PROGRAM 18: CREATING A PYTHON PROGRAM TO
INTEGRATE MYSQL WITH PYTHON.
(SEARCHING AND DISPLAYING RECORDS)
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password
='root',database='employees')
if con.is_connected():
cur=con.cursor()
print(“Welcome to employee search screen”)
no=int(input(“enter the employee number to search:”))
query=“select*from emp where empid={}”.format(no)
cur.execute(query)
data=cur.fetchone()
if data!=None:
print(data)
else:
print(“record not found”)
con.close()
SAMPLE OUTPUT:
Welcome to employee search screen
enter the employee number to search:2
(2,‘Anjali’, ‘F’, 27000)
32
PROGRAM 19: CREATING A PYTHON PROGRAM TO
INTEGRATE MYSQL WITH PYTHON.
(UPDATING RECORDS)
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password
='root',database='employees')
if con.is_connected():
cur=con.cursor()
print(“Welcome to employee detail update screen”)
no=int(input(“enter the employee number to update:”))
query=“select*from emp where empid={}”.format(no)
cur.execute(query)
data=cur.fetchone()
if data!=None:
print(“Details are:”)
print(data)
ans=input(“do u want to update the salary of the above
employee(y/n):”)
if ans==’y’ or ans==’Y’:
newsal=int(input(“enter new salary of employee:”))
q1=“update emp set salary={} where
empid={}”.format(newsal,no)
cur.execute(q1)
con.commit()
print(“employee salary updated successfully”)
q2=“select*from emp”
cur.execute(q2)
data=cur.fetchall()
for i in data:
print(i)
else:
print(“record not found”)
33
SAMPLE OUTPUT:
34
PROGRAM 20: SQL COMMANDS EXERCISE 1
To write Queries for the following Questions based on the given table:
35
PROGRAM 21: SQL COMMANDS EXERCISE 2
To write Queries for the following Questions based on the given table:
(a)Write a Query to insert all the rows of above table into stu table.
INSERT INTO STU VALUES(1,‘Arun’,’M’,24,‘COMPUTER’,‘1997-01-10’,120);
INSERT INTO STU VALUES(2,‘Ankit’,’M’,21,‘HISTORY’,‘1998-03-24’,200);
INSERT INTO STU VALUES(3,‘Anu’,’F’,20,‘HINDI’,‘1996-12-12’,300);
INSERT INTO STU VALUES(4,‘Bala’,’M’,19,‘NULL’,‘1999-07-01’,400);
INSERT INTO STU VALUES(5,‘Charan’,’M’,18,‘HINDI’,‘1997-09-05’,250);
INSERT INTO STU VALUES(6,‘Deepa’,’F’,19,‘HISTORY’,‘1997-06-27’,300);
INSERT INTO STU VALUES(7,‘Dinesh’,’M’,22,‘COMPUTER’,‘1997-02-
25’,210);
INSERT INTO STU VALUES(8,‘Usha’,’F’,23,‘NULL’,‘1997-07-31’,200);
(b)Write a Query to display all the details of employees from the above
table ‘STU’.
SELECT* FROM STU;
36
(c)Write a Query to display Rollno,Name and Department of the
students from STU table.
SELECT ROLLNO,NAME,DEPT FROM STU;
37
PROGRAM 22: SQL COMMANDS EXERCISE 3
To write Queries for the following Questions based on the given table:
(b) Write a Query to list name of the students whose ages are
between 18 to 20.
SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;
38
(c) Write a Query to display the name of the students whose name
is starting with 'A'.
SELECT NAME FROM STU WHERE NAME LIKE 'A%';
(d) Write a query to list the names of those students whose name
have second alphabet 'n' in their names.
SELECT NAME FROM STU WHERE NAME LIKE '_N%';
39
PROGRAM 23: SQL COMMANDS EXERCISE 4
To write Queries for the following Questions based on the given table:
Rollno Name Gender Age Dept DOA Fees
(b) Write a Query to change the fess of Student to 170 whose Roll
number is 1, if the existing fess is less than 130.
UPDATE STU SET FEES=170 WHERE ROLLNO=I AND FEES<130;
40
(c) Write a Query to add a new column Area of type varchar in table
STU.
ALTER TABLE STU ADD AREA VARCHAR(20);
(e) Write a Query to delete Area Column from the table STU.
ALTER TABLE STU DROP AREA;
41
PROGRAM 24: SQL COMMANDS EXERCISE 5
To write Queries for the following Questions based on the given table:
TABLE: UNIFORM
Ucode Uname Ucolor StockDate
1 Shirt White 2021-03-31
2 Pant Black 2020-01-01
3 Skirt Gre 2021-02-18
4 Tie Blue 2019-01-01
5 Socks Blue 2019-03-19
6 Belt Black 2017-12-09
TABLE: COST
Ucode Size Price Company
1 M 500 Raymond
1 L 580 Mattex
2 XL 620 Mattex
2 M 810 Yasin
2 L 940 Raymond
3 M 770 Yasin
3 L 830 Galin
4 s 150 Mattex
(a)To Display the average price of all the Uniform of Raymond
Company from table COST.
SELECT AVG(PRICE) FROM COST WHERE COMPANY='RAYMOND';
42
(c)To Display max price and min price of each company.
SELECT COMPANY,MAX(PRICE),MIN(PRICE) FROM COST GROUP BY
COMPANY;
1.
+ - - - - - - -+ - - - - - - - - + - - - - - - - - - - + - - - - - - + - - - - - - - - - -+
| UCODE | UNAME | UCOLOUR | SIZE | COMPANY |
+ - - - - - - -+ - - - - - - - - + - - - - - - - - - - + - - - - - - + - - - - - - - - - -+
| 1 | Shirt | White | M | RAYMOND|
| 1 | Shirt | White | L | MATTEX |
| 2 | Pant | Black | XL | MATTEX |
| 2 | Pant | Black | M | YASIN |
| 2 | Pant | Black | L | RAYMOND|
| 3 | Skirt | Grey | M | YASIN |
| 3 | Skirt | Grey | L | GALIN |
| 4 | Tie | Blue | S | MATTEX |
+ - - - - - - -+ - - - - - - - - + - - - - - - - - - - + - - - - - - + - - - - - - - - - -+
43