You are on page 1of 34

COMPUTER

SCIENCE
PRACTICAL FILE

ARYAN VARMA
XII C 07
RAJHANS VIDYALAYA
RAJHANS
VIDYALAYA
ANDHERI-WEST
MUMBAI

CERTIFICATE

This is to certify that this computer practical file has been completed
by
of class and board roll
number , in
partial fulfillment of the curriculum of the CENTRAL BOARD OF
SECONDARY
EDUCATION leading to the award of All India Senior School
Certificate for the
year 2022-2023.

External Examiner Internal Examiner


Date: Date:

SCHOOL SEAL PRINCIPAL


Date:
INDEX
SR.NO PROGRAM QUESTIONS
1 LINE STATISTICS
2 SECOND LARGEST NO. OF LIST
3 FREQUENCY OF GIVEN ELEMENT IN A LIST
4 SECOND LARGEST NO OF TUPLE
5 FUNCTION FOR STATS OF LINE
6 MY_BUZZ FUNCTION
7 MASS CONVERSION FUNCTION
8 LENGTH AND MASS CONVERSION FUNCTIONS
9 TEXT FILE FOR EMPLOYEE DETAILS
10 REPLICATION OF TEXT FILE
11 BINARY FILE FOR EMPLOYEE DETAILS
12 SEARCHING FOR CUSTOMER NUMBER IN BINARY FILE
13 CSV FILE FOR EMPLOYEE DETAILS
14 COUNTING NUMBER OF RECORDS IN CSV FILE
15 STACK FOR BOOK DETAILS
16 QUERIES IN MYSQL
17 RECORDS WHERE CUSTOMER NAME STANDS WITH R
18 UPDATING RECORDS IN MYSQL
19 SEARCHING A RECORD
20 DELETING A RECORD
1) line=input("Enter a
line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount +=1
elif a.isupper():
uppercount +=1
elif a.isdigit() :
digitcount+=1
if a.isalpha():
alphacount
+=1

print("Number of uppercase letters:",uppercount)


print("Number of lowercase letters:",lowercount)
print("Number of alphabets letters:",alphacount)
print("Number of digits letters:",digitcount)
2)
def second_largest(list):
list.sort()
return list[-2]

li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)

print("second largest in ",li,"is")


print(second_largest(li))
3)

lst=eval(input("Enter list elements"))

length=len(lst)

element=int(input("enter element to count its frequency"))

count=0

for i in range(0,length):

if element==lst[i]:

count+=1

if count==0:

print(element,"not

found")

else:

print(element, "has frequency as",count,"in given list")


4)
T=(23,45,34,66,77,67,11)
maxvalue=max(T)
length=len(T)
secmax=0
for a in range(length):
if secmax<T[a]<maxvalue:
secmax=T[a]
print("second largest value is",secmax)
5)
def statementl(line):

lowercase=uppercase=0
digit=alphabet=0
for a in line:
if a.islower():
lowercase+=1
elif a.isupper():
uppercase+=1
elif a.isdigit():
digit+=1
elif a.isalpha():
alphabet+=1
else:
pass
return lowercase+uppercase,digit,uppercase,lowercase
line=input("enter string")
a,b,c,d=statementl(line)

print("Number of uppercase:",c)
print("Number of lowercase:",d)
print("Number of digits:",b)
print("Number of alphabet:",a)
6)
def mybuzz(n):
if n%3==0:
return "Fizz"
elif n%5==0:
return "Buzz"
elif n%3==0 and n%5==0:
return "Fizzbuzz"
else:
return n

no=int(input("enter your number"))


x=mybuzz(no)
print(x)
7)
def kgtotonne(a):
kg=0.001
print(a,"kg is equal to",a*kg,"tonnes")

def tonnetokg(b):
kg=0.001
print(b,"tonnes is equal to",b/kg,"kg")

def kgtopound(c):
kg=2.20462
print(c,"kg is equal to",c*kg,"pounds")

def
poundtokg(d):
kg=2.20462
print(d,"pounds is equal to",d/kg,"kg")
choice=input("enter y for help")
if choice=='y':
help()
n=int(input("enter
choice")) if n==1:
x=int(input("enter weight in kg"))
kgtotonne(x)
elif n==2:
x=int(input("enter weight in tonne"))
tonnetokg(x)
elif n==3:
x=int(input("enter weight in kg"))
kgtopound(x)
elif n==4:
x=int(input("enter weight in pound"))
poundtokg(x)
else:
print("invalid choice")
else:
print("invalid choice")
8)
def milestokm(a):
'''Returns the converted length from miles to km'''
return a*MtoK
def kmtomiles(a):
'''Returns the converted length from km to miles'''
return a/MtoK
def feettoinches(a):
'''Returns the converted length from feet to inches'''
return a*FtoI
def inchestofeet(a):
'''Returns the converted length from inches to feet'''
return a/FtoI
# constants
MtoK = 1.609344 #constant value of 1 mile in
km FtoI = 12 #constant value of 1 feet in inches

# MassConversion.py
'''Conversion between units of mass'''
# functions
def kgtotonne(x):
'''Returns the converted mass from kg to tonnes'''
return x*KtoT
def tonnetokg(x):
'''Returns the converted mass from tonne to kgs'''
return x/KtoT
def kgtopound(x):
'''Returns the converted mass from kg to pounds'''
return x*KtoP
def poundtokg(x):
'''Returns the converted mass from pounds to kgs'''
return x/KtoP
# constants
KtoT = 0.001 #constant value of 1 kg in tonnes
KtoP = 2.20462 #constant value of 1 kg in
pounds

import UnitConverter.lengthconversion as lc
import UnitConverter.MassConversion as
mc length = 10
mass = 30
print(lc.feettoinches(length))
print(mc.kgtopound(mass))
9)
import pickle
with open("emp.txt","wb") as fh:
stu={}
for i in range(0,2):
eno=int(input("enter employee no"))
n=input("enter name")
salary1=int(input("salary"))
stu['empno']=eno
stu['name']=n
stu['salary']=salary1
pickle.dump(stu,fh)

fh.close()
try:
with open("emp.txt","rb") as fh:

while True:

stu=pickle.load(fh)
print("this is record no",i)
print(stu)
i=i+1
except EOFError:
fh.close()
10)
f1=open("story.txt","r")
f2=open("copy.txt","w")
l=f1.readlines()
for line in l:
t=line.split()
for x in t:
f2.write(x)
f2.write('-'
)
f2.write('\n')

f1.close()
f2.close()
11)
import pickle
a=open("Employee.dat","wb")
ch=int(input("Enter no of records you want to enter:"))
for i in range (ch):
eno=int(input("Enter employee no:"))
enam=input("Enter employee name:")
age=int(input("Enter employee age:"))
sal=int(input("Enter employee
salary:"))
dict={"Employee no":
eno,"Name":enam,"Age":age,"Salary":sal} pickle.dump(dict,a)
a.close()
a=open("Employee.dat","rb")

try:
while a:
inf=pickle.load(a)
if inf["Salary"]>=25000 and inf["Salary"]<=30000:
print(inf)
except EOFError:
print("End of
file")
12)
import pickle
a=open("Cust.txt","rb")
inp=input("Enter customer no")
cou=0
try:
while a:
inf=pickle.load(a)
if inf['Customer number']== inp:
print("Found!!!")
cou+=1
break
except EOFError:
print("End of
file")
if cou==0:
print("Not found")
13)
import csv
a=open("employee.csv","w",newline="")
w=csv.writer(a)
ch=int(input("Enter no of records you want to enter:"))
w.writerow(["Employee no","Employee
name","Department","salary"])
for i in range(ch):
eno=int(input("Enter employee no:"))
enam=input("Enter employee name:")
dept=(input("Enter employee department:"))
sal=int(input("Enter employee salary:"))
ls=[eno,enam,dept,sal]
w.writerow(ls)
a.close()

a=open("employee.csv","r",newline="")
w=csv.reader(a)
for i in w:
print(i)

a.close()
14)
import csv
a=open("employee.csv","r",newline="")
w=csv.reader(a)
count=0
for i in w:
print(i)
count+=1
print("No fo records:",count-1)
15)
def isempty(stk):
if stk==[]:
return True
else:
return False

def push(stk,item):
stk.append(item
) top=len(stk)-1

def display(stk):
if
isempty(stk):
print("empty stack")
else:
top=len(stk)-1
print(stk[top],"<---top")
for a in range(top-1,-1,-1):
print(stk[a])

stk=[]
top=None
ans='y'
while ans=='y':

ch=int(input("1-push,2-display"))
if ch==1:
bno=int(input("enter bookno"))
bname=input("enter
bookname") item=[bno,bname]
push(stk,item)

elif ch==2:
display(stk)
else:
print("invalid choice")

ans=input("y to

continue")
16) A
i) mysql> select * from club where sex='f';
+ + + + + + +
+
| coach_ID | coachname | age | sports | dateofapp | pay |
sex |
+ + + + + + +
+
| 2 | ravina | 34 | squash | 1998-02-19 | 1200 | f
|
+ + + + + + +
+
1 row in set (0.00 sec

ii) mysql> select * from club where pay>1000 order by dateofapp desc;
+ + + + + + + +
| coach_ID | coachname | age | sports | dateofapp | pay | sex |
+ + + + + + + +
| 2 | ravina | 34 | squash | 1998-02-19 | 1200 | f |
| 4 | shailya | 41 | basketball | 1998-02-19 | 1700 | m |
+ + + + + + +
+ 2 rows in set (0.10 sec)

iii) mysql> update club

-> set pay=pay+2000


-> ;
Query OK, 4 rows affected (0.12 sec)
Rows matched: 4 Changed: 4 Warnings: 0

mysql> select * from club


-> ;
+ + + + + + + +
| coach_ID | coachname | age | sports | dateofapp | pay | sex |
+ + + + + + + +
| 1 | kukreja | 35 | karate | 1996-03-27 | 3000 | m |
| 2 | ravina | 34 | squash | 1998-02-19 | 3200 | f |
| 3 | zubin | 36 | swimming | 1998-01-12 | 2750 | m |
| 4 | shailya | 41 | basketball | 1998-02-19 | 3700 | m |
+ + + + + + +
+ 4 rows in set (0.00 sec)

iv)mysql> select * from club where coachname like '%ya%';


+ + + + + + + +
| coach_ID | coachname | age | sports | dateofapp | pay | sex |
+ + + + + + + +
| 4 | shailya | 41 | basketball | 1998-02-19 | 3700 | m |
+ + + + + + +
+ 1 row in set (0.10 sec)

v)mysql> select coachname,pay,age,(15/100)*pay as BONUS from club


-> ;
+ + + + +
| coachname | pay | age | BONUS |
+ + + + +
| kukreja | 3000 | 35 | 450.0000 |
| ravina | 3200 | 34 | 480.0000 |
| zubin | 2750 | 36 | 412.5000 |
| shailya | 3700 | 41 | 555.0000 |
+ + + +
+ 4 rows in set (0.00
sec)

mysql>9

B)
i) mysql> select * from graduate where ran=1 order by name;
+ + + + + + +
| sno | name | stipend | subject | average | ran |
+ + + + + + +
| 2 | DIVAKAR | 450 | COMPUTER SC. | 90 | 1 |
| 1 | DIVYA | 400 | PHYSICS | 68 | 1 |
| 4 | SABINA | 500 | CHEMISTRY | 57 | 1 |
+ + + + + +
+ 3 rows in set (0.02 sec)

ii) mysql> select name from graduate where name like '%na%';
+ +
| name |
+ +
| SABINA |
+ +
1 row in set (0.00 sec)
iii) mysql> update graduate
-> set stipend =stipend+500
-> ;
Query OK, 4 rows affected (0.14 sec)
Rows matched: 4 Changed: 4 Warnings: 0

mysql> select * from graduate;


+ + + + + + +
| sno | name | stipend | subject | average | ran |
+ + + + + + +
| 1 | DIVYA | 900 | PHYSICS | 68 | 1 |
| 2 | DIVAKAR | 950 | COMPUTER SC. | 90 | 1 |
| 3 | VIKAS | 850 | MATHS | 95 | 2 |
| 4 | SABINA | 1000 | CHEMISTRY | 57 | 1 |
+ + + + + +
+ 4 rows in set (0.00 sec)

iv) mysql> select * from graduate where subject='physics' and ran=1;


+ + + + + + +
| sno | name | stipend | subject | average | ran |
+ + + + + + +
| 1 | DIVYA | 900 | PHYSICS | 68 | 1 |
+ + + + + +
+ 1 row in set (0.00 sec)
v) mysql> select name,subject,average,0.15*stipend as bonus from
graduate;
+ + + + +
| name | subject | average | bonus |
+ + + + +
| DIVYA | PHYSICS | 68 | 135.00 |
| DIVAKAR | COMPUTER SC. | 90 | 142.50 |
| VIKAS | MATHS | 95 | 127.50 |
| SABINA | CHEMISTRY | 57 | 150.00 |
+ + + +
+ 4 rows in set (0.00 sec)
17)
import mysql.connector
def connection():
global mydb,cur
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="new"
)

connection()
cur=mydb.cursor()
cur.execute("Select * from customer where name like 'r%' ")
for i in cur:
print(i)
18)
import mysql.connector
def connection():
global mydb,cur
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="new"
)

connection()
cur=mydb.cursor()
cur.execute("update customer set sal=sal+2000")
mydb.commit
cur.execute("Select * from customer")
for i in cur:
print(i)
19)
import mysql.connector
def connection():
global mydb,cur
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="new"
)

connection()
cur=mydb.cursor()
cur.execute("Select * from customer where price>5000 and
price<9000")

for i in cur:
print(i)
20)
import mysql.connector
def connection():
global mydb,cur
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="new"
)

connection()
cur=mydb.cursor()
cur.execute("delete from customer where cno=5")
mydb.commit()
cur.execute("Select * from customer")

for i in cur:
print(i)

You might also like