Computer Science Practical File AISSCE 2024
Computer Science Practical File AISSCE 2024
School, Bhopal
CERTIFICATE
This is to certify that ___________________, a student of Class XII has
successfully completed all Python and SQL Assignments, under the guidance
Delhi.
Signature of Principal
Page | 2
ACKNOWLEDGMENT
Name : ______________________
Page | 3
TABLE OF CONTENT
Page | 4
Q.1. TEXT FILE WORDS DISPLAYED WITH #
Q.1. Read a text file line by line and display each word
separated by a #.
Program :-
myfile=open("[Link]","r")
while line:
words=[Link]()
for x in words:
print(x,end="#")
Output :-
Aldebaran#is#the#brightest#star#in#the#zodiac#constellatio
n#of#Taurus.#It#is#located#at#a#distance#of#approximately#
65#light-
years#from#the#Sun.#The#star#lies#along#the#line#of#sight#
to#the#nearby#Hyades#cluster.#
Page | 5
Q.2. TEXT FILE STATISTICS
[Link] a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the
file.
Program :-
myfile=open("[Link]","r")
content=[Link]()
up=low=vow=cons=0
for ch in content:
if [Link]():
up+=1
elif [Link]():
low+=1
if [Link]() in 'aeiou':
vow+=1
else:
cons+=1
print("Vowels=",vow)
print("Consonants=",cons)
print("Uppercase=",up)
print("Lowercase=",low)
[Link]()
Output :-
Consonants= 153
Uppercase= 6
Lowercase= 164
Page | 6
Q.3. TEXT FILE STORE ITEM RECORDS
[Link] a text file programmatically which stores the
records (itemid,item_description,price) of 5 items.
Program :-
myfile=open("[Link]","w")
n=int(input("How many items?"))
item_records=""
for i in range(n):
itemid=input("ItemID>>")
item_name=input("Item description>>")
price=float(input("Price>>"))
item_records+=itemid+"\t"+item_name+"\t"+str(price)+"\n"
[Link](item_records)
[Link]()
Output :-
Price>>45 ItemID>>I5
Page | 7
Q.4. REMOVE ALL LINES THAT CONTAIN ‘A’
Q.4. Remove all the lines that contain the character 'a'
in a file and write it to another file.
Program :-
fin=open("[Link]","r") # read
fout=open("poem_no_a.txt","w") # write
content=[Link]()
no_a_lines=list()
no_a_lines.append(line)
[Link](no_a_lines)
[Link]()
[Link]()
Output :-
[Link]
poem_no_a.txt
Page | 9
Q.5. BINARY FILE – SEARCH NAME WITH ROLLNO.
Q.5. Create a binary file with name and roll number.
Search for a given roll number and display the name, if
not found display appropriate message.
Program :-
import pickle
myfile=open("stud_info.dat","ab+")
for i in range(n):
rollno=int(input("Enter rollno.:"))
srecord=[rollno,sname]
[Link](srecord,myfile)
[Link]()
[Link](0)
try:
while True:
srec=[Link](myfile)
if srec[0]==rollno:
Page | 10
print("Rollno.=",srec[0],"\nStudent
name=",srec[1])
break
except EOFError:
[Link]()
Output :-
Enter rollno.:65
Enter rollno.:47
Rollno.= 65
Page | 11
Q.6. BINARY FILE – ENTER ROLLNO AND UPDATE MARKS
Q.6. Create a binary file with roll number, name and
marks. Input a roll number and update the marks.
Program :-
import pickle
myfile=open("student_details.dat","ab+")
for i in range(n):
rollno=int(input("Enter rollno.:"))
marks=float(input("Enter marks:"))
srec=[rollno,sname,marks]
[Link](srec,myfile)
[Link]()
[Link](0)
stable=list()
try:
while True:
srec=[Link](myfile)
[Link](srec)
except EOFError:
pass
Page | 12
[Link]()
n=len(stable)
print("Rollno\tStudent name\tMarks")
for i in range(n):
print(stable[i][0],"\t",stable[i][1],"\t",stable[i][2])
found=False
for i in range(n):
if rollno==stable[i][0]:
print("Record found!")
print(stable[i][0],"\t",stable[i][1],"\t",stable[i][2])
stable[i][2]=new_marks
found=True
break
else:
Page | 13
# Write updated table to file
if found:
myfile=open("student_details.dat","wb")
for i in range(n):
print(stable[i])
[Link](stable[i],myfile)
[Link]()
print("Record updated!")
Output :-
Enter rollno.:1
Enter marks:65.5
Enter rollno.:2
Enter marks:54
Enter rollno.:3
Enter marks:87
Enter rollno.:4
Enter marks:90.5
Enter rollno.:5
Page | 14
Enter name :Rajesh Kumar
Enter marks:67.3
2 Raghuvindra K 54.0
Record found!
2 Raghuvindra K 54.0
Record updated!
Page | 15
Q.7. CSV FILE – SEARCH PASSWORD WITH USERID
Q.7. Create a CSV file by entering user-id and password,
read and search the password for given userid.
Program :-
import csv
header=['user_id','password']
row=list()
login_writer=[Link](myfile)
for i in range(n):
user_id=input("Enter user-id:")
password=input("Enter password:")
row=[user_id,password]
login_writer.writerow(row)
login_reader=[Link](myfile)
if uid==row[0]:
print("User_id=",row[0],", password=",row[1])
Output :-
Page | 16
How many user-ids?5
Enter user-id:abigaila407
Enter password:mccabe
Enter user-id:jacobc1041
Enter password:jocy54
Enter user-id:galvang405
Enter password:mocy47
Enter user-id:vixtor57
Enter password:pls74
Enter user-id:melanie78
Enter password:785k
Page | 17
Q.8. ADD LIST ELEMENTS
Q.8. Write a program that takes any two lists L and M of
the same size and adds their elements together to form a
new list L whose elements are sums of the corresponding
elements in L and M. For instance, if L=[3,1,4] and
M=[1,5,9], then N should equal [4,6,13].
Program :-
L=list()
M=list()
N=list()
for i in range(n):
[Link](x)
[Link](y)
[Link](x+y)
print("L=",L)
print("M=",M)
print("N=",N)
Output :-
Page | 19
Q.9. STACK OF BOOKS
Q.9. Write a Python program to implement a stack of books
named “book_stack” and push book items to this stack. Each
book item consists of BookID,BookTitle and Price. Also pop the
book elements and display them. Display “Stack Empty” if the
stack is empty.
Program :-
book_stack=list()
book_item=list()
ans='y'
while ans=='y':
bookid=int(input("Enter book-id:"))
book_item=[bookid,btitle,price]
book_stack.append(book_item)
while book_stack:
print(book_stack.pop(),end="\t")
else:
print("Stack Empty")
Output :-
Page | 20
Enter book-id:1
Enter book-id:2
Enter book-id:3
Enter book-id:4
Enter book-id:5
Page | 21
Q.10. PUSH ELEMENTS FROM LIST TO STACK
Q.10. Write a Python program to push all the elements at odd
locations (index) to a stack from the list
colors=['black','cyan','magenta','yellow','purple','green','re
d','blue'].
Program :-
stack=list()
colors=['black','cyan','magenta','yellow','purple','green','re
d','blue']
for i in range(len(colors)):
if i%2!=0:
[Link](colors[i])
print("List=",colors)
Output :-
Page | 22
Q.11. STACK AS LIST
[Link] a Python program to push some elements to a stack
implemented as a list and display them. Also pop the elements
from the stack and display them. If no more elements are left,
then display “Stack Empty”.
Program :-
stack=list()
# display stack
print(stack)
# pop elements
print("Elements popped=")
while stack:
print([Link]())
else:
print("Stack empty")
Output :-
Page | 23
Enter an element to push:Pasta Sause
Elements popped=
Pasta Sause
Salsa
Frozen Fruit
Stack empty
Page | 24
Q.12. PUSH ELEMENTS FROM TUPLE
[Link] a Python program to read a tuple from the user
which contains some numeric elements. Now define a function
named push_even() which will push all the even numbers in this
tuple to a stack. Also define a function pop_even() which will
pop the elements from this stack. For example, if the tuple is
(23,45,12,11,7,5,9,8,34,32,77,78), then the stack should
contain [12,8,34,32,78] and while popping the output should be
78 32 34 8 12 StackEmpty.
Program :-
def push_even(x):
for element in x:
if element%2==0:
stack_even.append(element)
def pop_even():
print("Popping elements from stack=>")
while stack_even:
print(stack_even.pop())
else:
print("StackEmpty")
# _main_
stack_even=list()
# display stack
print(stack_even)
Output :-
Page | 25
[46, 8, 12, 36, 34, 14, 8, 72]
72
14
34
36
12
46
StackEmpty
Page | 26
Q.13. RANDOM NO. GENERATOR
Q.13. Write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
Program :-
import random
ans='y'
while ans=='y':
num=[Link](1,6)
print("Dice rolled=",num)
Output :-
Dice rolled= 3
Dice rolled= 5
Dice rolled= 2
Dice rolled= 4
Page | 27
Q.14. PY-MYSQL CONNECTIVITY PROGRAM
[Link] a Python-MySQL connectivity program to do the
following :-
a. Insert new records into the Student table
(rollno,sname,marks).
b. Display records.
c. Search for a student based on rollno.
d. Display the student record with highest marks.
Program :-
import [Link]
con=[Link](host="localhost",user="root",pas
swd="1234",database="cbseexamdb")
scursor=[Link]()
for i in range(n):
rollno=int(input("Enter rollno.:"))
sname=input("Enter name of student:")
marks=float(input("Enter marks:"))
sql="insert into Student
values({},'{}',{})".format(rollno,sname,marks)
[Link](sql)
[Link]()
Page | 28
else:
print("Record not found!")
# close connection
[Link]()
Output :-
Enter rollno.:6
Enter marks:63.5
Enter rollno.:7
Enter marks:78
1 Seema 45.50
2 Caren 65.50
3 Devansh 70.00
4 Mohan 75.50
5 Reema 80.50
6 Perlin 63.50
Page | 29
7 S Jeeva 78.00
5 Reema 80.50
5 Reema 80.50
Page | 30
Q.15. PYTHON-MySQL CONNECTIVITY : EMPLOYEE
Q.15. Write a Python-MySQL connectivity program to do the
following :-
a. Insert new records into the Employee table
(empno,ename,dept,salary).
b. Ask from the user any department and display all
employee records belonging to that department.
c. Search for an employee based on empno and update his
salary.
d. Display all the employee records.
Program :-
import [Link]
con=[Link](host="localhost",user="root",passw
d="1234",database="cbseexamdb")
ecursor=[Link]()
for i in range(n):
dept=input("Enter department:")
salary=float(input("Enter salary:"))
[Link](sql)
[Link]()
[Link](sql)
student_set=[Link]()
Page | 31
print("Empno\tEmployee name\t\tDepartment\t\tSalary") # header
row
print(record[0],"\t",record[1],"\t\t\t",record[2],"\t\t",recor
d[3])
# ask from user any dept and display all employee records in
that department
[Link](sql)
emp_set=[Link]()
print(record[0],"\t",record[1],"\t\t",record[2],"\t\t",record[
3])
if not emp_set:
[Link](sql)
Page | 32
[Link]()
print("Salary updated!")
# close connection
[Link]()
Output :-
Enter department:IT
Enter salary:50000
Enter department:Production
Enter salary:40000
Enter department:Purchase
Enter salary:23000
Page | 33
105 Milan Singh Purchase 12000
Salary updated!
Page | 34
Q.16. PYTHON-MySQL CONNECTIVITY : PRODUCT
Q.16. Write a Python-MySQL connectivity program to do the
following :-
a. Insert new records into the Product table
(prodID,item_name,price).
b. Display the average price of all items.
c. Display the items whose price is more than 500.
d. Update the price of an item after searching based on
prodID.
Program :-
import [Link]
con=[Link](host="localhost",user="root",passw
d="1234",database="cbseexamdb")
pcursor=[Link]()
for i in range(n):
price=float(input("Enter price:"))
[Link](sql)
[Link]()
[Link](sql)
prod_set=[Link]()
Page | 35
for record in prod_set:
print(record[0],"\t",record[1],"\t\t",record[2])
[Link](sql)
prod_set=[Link]()
print("Average price=",prod_set[0])
[Link](sql)
prod_set=[Link]()
print(record[0],"\t",record[1],"\t\t",record[2])
if not prod_set:
Page | 36
[Link](sql)
[Link]()
print("Price updated!")
# close connection
[Link]()
Output :-
Enter price:80
Enter price:65
Enter price:60
1 Flour 150.00
3 Pepper 45.00
4 Seasoning 90.00
5 Cheese 560.00
Page | 37
6 Bread 10.00
9 Jam 120.00
10 Mayonnaise 99.00
5 Cheese 560.00
Price updated!
Page | 38
Q.17. PYTHON-MySQL CONNECTIVITY : GARMENT
Q.17. Write a Python-MySQL connectivity program to do the
following :-
a. Insert new records into the Garment table
(Gcode,Gname,size,colour,price).
b. Display those garment details whose price is in the
range 1000.00 to 1500.00.
c. Display names of those garments that are available in
‘XL’ size.
d. Update the colour of garment whose code is given. Ask
colour and gcode from user.
Program :-
import [Link]
con=[Link](host="localhost",user="root",passwd="12
34",database="cbseexamdb")
gcursor=[Link]()
for i in range(n):
gcode=int(input("Enter Garment code:"))
gname=input("Enter Garment name:")
size=input("Enter size (S/M/L/XL/XXL/UXL) :")
color=input("Enter color:")
price=float(input("Enter price:"))
sql="insert into Garment
values({},'{}','{}','{}',{})".format(gcode,gname,size,color,price)
[Link](sql)
[Link]()
print(record[0],"\t",record[1],"\t\t",record[2],"\t",record[3],"\t\
t",record[4])
Page | 39
# Display those garment details whose price is in the range 1000.00
to 1500.00
sql="select * from Garment where price between 1000 and 1500"
[Link](sql)
garment_set=[Link]()
print("garment details whose price is in the range 1000.00 to
1500.00=>")
print("Gcode\tGarment\t\tSize\tColour\t\tPrice")
for record in garment_set:
print(record[0],"\t",record[1],"\t\t",record[2],"\t",record[3],"\t\
t",record[4])
if not garment_set:
print("No record found!")
# Update the colour of garment whose code is given. Ask colour and
gcode from user
gcode=int(input("Enter garment code to update colour:"))
color=input("Enter new garment color:")
sql="update Garment set colour='{}' where
gcode={}".format(color,gcode)
[Link](sql)
[Link]()
print("Colour of garment updated!")
# close connection
[Link]()
Output :-
How many records to be inserted?3
Enter Garment code:118
Enter Garment name:Skirt
Enter size (S/M/L/XL/XXL/UXL) :XXL
Page | 40
Enter color:Red
Enter price:800
Enter Garment code:119
Enter Garment name:Sweater
Enter size (S/M/L/XL/XXL/UXL) :XL
Enter color:Grey
Enter price:400
Enter Garment code:120
Enter Garment name:Waistcoat
Enter size (S/M/L/XL/XXL/UXL) :L
Enter color:Silver
Enter price:500
Gcode Garment Size Colour Price
111 Tshirt XL Red 1400.00
112 Jeans L Blue 1600.00
113 Skirt M Black 1100.00
114 Ladies Jacket XL Blue 4000.00
115 Trousers L Brown 1500.00
116 Ladies Top L Pink 1200.00
117 Suit XL Maroon 1500.00
118 Skirt XXL Red 800.00
119 Sweater XL Grey 400.00
120 Waistcoat L Silver 500.00
garment details whose price is in the range 1000.00 to 1500.00=>
Gcode Garment Size Colour Price
111 Tshirt XL Red 1400.00
113 Skirt M Black 1100.00
115 Trousers L Brown 1500.00
116 Ladies Top L Pink 1200.00
117 Suit XL Maroon 1500.00
names of those garments that are available in ‘XL’ size=>
Tshirt
Ladies Jacket
Suit
Sweater
Enter garment code to update colour:115
Enter new garment color:Black
Colour of garment updated!
Page | 41
Q.18. SQL Queries - COMPANY and CUSTOMER tables
Q.18. Write SQL queries for (i) to (iv) and find outputs for SQL
queries (v) to (viii), which are based on the tables COMPANY and
CUSTOMER.
1. To display those company name which are having prize less than
30000.
+-------+-------+
| name | price |
+-------+-------+
| Onida | 20000 |
| Sony | 25000 |
+-------+-------+
Page | 42
+------------+
| name |
+------------+
| Sony |
| Sony |
| Onida |
| Nokia |
| Dell |
| Blackberry |
+------------+
+--------+----------------+-------+------+------+
+--------+----------------+-------+------+------+
Page | 43
| 106 | Sonal Aggarwal | 21000 | 5 | 333 |
+--------+----------------+-------+------+------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+------------+---------------+------+-----+---------+-------+
+----------+--------+
| COUNT(*) | CITY |
+----------+--------+
| 3 | Delhi |
Page | 44
| 1 | Madras |
| 2 | Mumbai |
+----------+--------+
+------------+------------+
| MIN(PRICE) | MAX(PRICE) |
+------------+------------+
| 50000 | 70000 |
+------------+------------+
+----------+
| AVG(QTY) |
+----------+
| 11.0000 |
+----------+
+-------------+--------+-------+
+-------------+--------+-------+
+-------------+--------+-------+
Page | 45
Q.19. SQL Queries - ITEMS and TRADERS tables
Write SQL queries for (a) to (g) and write the output for the SQL
queries mentioned shown in (hi) to (h4) parts on the basis of table
ITEMS and TRADERS :
Page | 46
+-------+----------+
| T01 | 2 |
| T02 | 2 |
| T03 | 1 |
+-------+----------+
3 rows in set (0.00 sec)
4. To display the price, item name and quantity (i.e.,
qty) of those items which have quantity more than 150.
+------------+------------+
| MAX(PRICE) | MIN(PRICE) |
+------------+------------+
| 38000 | 1200 |
Page | 47
+------------+------------+
1 row in set (0.00 sec)
+-----------+
| PRICE*QTY |
+-----------+
| 1075000 |
+-----------+
1 row in set (0.00 sec)
+-------+
| TCODE |
+-------+
| T01 |
| T02 |
| T03 |
+-------+
3 rows in set (0.00 sec)
+----------------+------------------+
| INAME | TNAME |
+----------------+------------------+
| Car GPS System | Electronic Sales |
| LED Screen 40 | Disp House Inc |
+----------------+------------------+
2 rows in set (0.00 sec)
Page | 48
Q.20 : SQL Queries - SHOP and ACCESSORIES tables
Write SQL queries for (i) to (iv) and find outputs for SQL queries
(v) to (viii), which are based on the tables SHOP and ACCESSORIES.
Page | 49
10 rows in set (0.00 sec)
+--------------+
| NAME |
+--------------+
Page | 50
| Mother Board |
| Hard Disk |
| LCD |
+--------------+
3 rows in set (0.00 sec)
+-------------+----------+
| AREA | COUNT(*) |
+-------------+----------+
| CP | 2 |
| GK II | 1 |
| Nehru Place | 2 |
+-------------+----------+
3 rows in set (0.00 sec)
+----------------------+
| COUNT(distinct area) |
+----------------------+
| 3 |
+----------------------+
1 row in set (0.00 sec)
+--------------+----------+
| NAME | DISCOUNT |
+--------------+----------+
| Keyboard | 25.00 |
| Mother Board | 650.00 |
| Keyboard | 20.00 |
| Hard Disk | 225.00 |
+--------------+----------+
4 rows in set (0.00 sec)
Page | 51
Q.21: SQL Queries : VEHICLE and TRAVEL tables
Write SQL queries for (i) to (iv) and find outputs for SQL queries
(v) to (viii), which are based on the tables VEHICLE and TRAVEL.
Table : VEHICLE
Note:
SQL >
OUTPUT >
+------+--------------+------------+
+------+--------------+------------+
Page | 52
| 106 | Ramesh Jaya | 2016-04-06 |
+------+--------------+------------+
2. To display the CNAME of all customers from the table TRAVEL who
are travelling by vehicle with code V01 or V02.
SQL >
OUTPUT >
+-------------+-------+
| cname | vcode |
+-------------+-------+
| [Link] | V01 |
+-------------+-------+
3. To display the CNO and CNAME of those customers from the table
TRAVEL who travelled between ‘2015-12-31’ and ‘2015-05-01’.
SQL >
OUTPUT >
+------+---------+------------+
Page | 53
+------+---------+------------+
+------+---------+------------+
SQL >
OUTPUT >
+-------------+---------------+------+------+
+-------------+---------------+------+------+
+-------------+---------------+------+------+
OUTPUT >
+----------+-------+
| COUNT(*) | VCODE |
+----------+-------+
| 2 | V01 |
| 2 | V02 |
+----------+-------+
OUTPUT >
+-------+
Page | 54
| VCODE |
+-------+
| V01 |
| V03 |
| V02 |
| V04 |
| V05 |
+-------+
OUTPUT >
+-------+-------------+---------------+
+-------+-------------+---------------+
+-------+-------------+---------------+
OUTPUT >
+-------------+----------+
| CNAME | KM*PERKM |
+-------------+----------+
| Sahanubhuti | 1620 |
+-------------+----------+
Page | 55
Q.22: SQL Queries : SCHOOL and ADMIN tables
Write SQL queries for (i) to (iv) and find outputs for SQL queries
(v) to (viii), which are based on the tables SCHOOL and ADMIN.
OUTPUT >
+------------+---------+
| teacher | periods |
+------------+---------+
| Priya Rai | 26 |
| Lisa Anand | 27 |
| Ganan | 28 |
| Harish B | 27 |
+------------+---------+
Page | 56
4 rows in set (0.00 sec)
OUTPUT >
+------+--------------+-----------+------------+---------+------------+
+------+--------------+-----------+------------+---------+------------+
+------+--------------+-----------+------------+---------+------------+
OUTPUT >
+----------------+
| designation |
+----------------+
| Vice Principal |
| Coordinator |
| HOD |
| Senior Teacher |
Page | 57
+----------------+
OUTPUT >
+--------------+------+----------------+
+--------------+------+----------------+
+--------------+------+----------------+
OUTPUT >
+----------------+----------+
| Designation | Count(*) |
+----------------+----------+
| Vice Principal | 1 |
+----------------+----------+
OUTPUT >
Page | 58
+-----------------+
| max(EXPERIENCE) |
+-----------------+
| 16 |
+-----------------+
OUTPUT >
+---------+
| TEACHER |
+---------+
| Umesh |
| Yashraj |
+---------+
OUTPUT >
+----------+--------+
| COUNT(*) | GENDER |
+----------+--------+
| 2 | Female |
| 5 | Male |
+----------+--------+
----xxx----
Page | 59