Here's my Computer Science Board Practical File. I hope you find it as useful as it was to me.This file is however of CBSE class 12th 2020-2021 syllabus.
1. TEXT FILES
#1 Writing data intotext files using writelines
with open("two.txt","w") as f:
l=[]
ch='y'
x=0
while ch=='y':
l=input("enter list values")
f.writelines(l)
ch=input("do you want to
continue?")
x+=1
print("file created successfully!!!with
",x,"records entered")
enter list values"Suhani",89,"A"
do you want to continue?y
enter list values"Raunak",55,"C"
do you want to continue?y
enter list values"Cia",45,"D"
do you want to continue?n
file created successfully!!!with 3
records entered
#2 Read() method intext files
fr=open("one.txt","r")
data=fr.read()
print("Fileread successfully")
print(data)
fr.close()
File read successfully
My firstline of code
to enter data into a text file
we will do binary files later
2. #3 Read() method withargument in text files
fr=open("one.txt","r")
data=fr.read(10)
print("Fileread successfully")
print(data)
fr.close()
File read successfully
My firstl
#4 Readline() methodintext files
fr=open("one.txt",'r')
data=fr.readline()
print(data)
print("line read successfully")
fr.close()
My firstline of code
line read successfully
3. #5 Using read() method in a Text file count number of consonants ,vowels ,digits
and special characters
f=open("one.txt",'r')
data=f.read()
sp,c,d,v=0,0,0,0
L=len(data)
for i in data:
print(i,end='')
if i in ("aeiouAEIOU"):
v+=1
elif i.isalpha():
c+=1
elif i.isdigit():
d+=1
My firstline of code
to enter data into a text file
we will do binary files later
string: My firstline of code
to enter data into a text file
we will do binary files later
Total no. of characters: 86
no. of consonents 39
vowels 26
digits: 0
5. #6 Using read() method in text file count the number of words in a text file
(SPLIT FUNCTION)
#counting the number of words in a
text file
f=open("one.txt",'r')
data=f.read()
l=data.split()
w=len(l)
print("contents of the file are n",
data)
print("Number of words:", w)
Contents of the file are
My firstline of code
to enter data into a text file
we will do binary files later
Number of words: 18
6. #7 Replace eachsignby ‘#’ sign, display the string and write intoa new file
#Read method with argumentin text
file
fr=open("one.txt",'r')
fw=open("TWO.txt",'r')
nd=''
data=fr.read()
print("originaldata",data)
print("modified
data",data.replace('','@'))
for i in data:
if i=='':
nd+='#'
else:
nd=nd+i
print("Modified string",nd)
fw.write(nd)
fr.close()
fw.close()
7. #8 Count the number of occurrences of ‘the’ and ‘these’ andreplace with‘THE’
and ‘THESE”
#Count the number of 'the' and 'these'
f=open("one.txt",'r+')
data=f.read()
t1,t2=0,0
l=data.split()
New_l=''
for w in l:
if w=='file':
t1+=1
elif w=='My':
t2+=1
New_l+=w.upper()
else:
New_l+=w
New_l+=w
print("original filr:", data)
print("Modified file:",New_l)
print('no of file',t1)
print("My:",t2)
print("Total occurances:=",t1+t2)
===========
original filr: My first line of code
to enter data into a text file
we will do binary files later
Modified file:
MYMyfirstfirstlinelineofofcodeco
detotoenterenterdatadataintoin
toaatexttextfilewewewillwilldod
obinarybinaryfilesfileslaterlater
no of file 1
My: 1
Total occurances:= 2
8. BINARY FILES
#1 WAP to create a dictionary and write contents intoabinary file and display
the file contents andnumber of records
9. #2 WAP to searchfor a particular recordusing the key of a dictionary via binary
files
10. #3 WAP to update data storedina dictionary using binary files inpython
11. #4 WAP toread contents anddisplay number of bytes occupiedby it
12. #5 WAP to update data storedina dictionary using binary files inpython and
display the file
13. #6 WAP todelete records by user input froma dictionary using binary files
14.
15. FUNCTIONS
#1 Function to add two numbers
INPUT OUTPUT
def add(a,b):
sum=a+b
return sum
a=int(input("Enter First Number"))
b=int(input("Enter Second Number"))
res=add(a,b)
print("Thesum of",a,"and",b,"is",res)
Enter FirstNumber2
Enter Second Number3
The sum of 2 and 3 is 5
#2
INPUT OUTPUT
def evenodd(x):
if (x%2==0):
print("even")
else:
print("Odd")
#driver code
evenodd(2)
evenodd(3)
even
Odd
16. #3 Identifying local x and global x
INPUT OUTPUT
x=300
def myfunc():
x=200
print("localx:",x)
myfunc()
print("globalx:",x)
local x: 200
global x: 300
#4 Using a global variable in nestedfunction
INPUT OUTPUT
def food():
x=20
def bar():
global x
x=25
print("Beforecalling bar:",x)
print("Calling bar now")
bar()
print("After calling bar:",x)
food()
print("xin main:",x)
Before calling bar: 20
Calling bar now
After calling bar: 20
x in main: 25
17. RANDOM MODULE
#1 Program to generate 5 random numbers
INPUT OUTPUT
import random
l=()
for i in range(5):
x=random.random()
y=int(x*10)
print(y)
4
6
2
0
9
#2 Program to generate 5 random numbers between 10 and 20
INPUT OUTPUT
import random
l=[]
for i in range(5):
x=int(random.random()*11)+10
print(x)
17
11
15
19
10
18. #3 Program to generate 5 random numbers between 50 to 100
INPUT OUTPUT
import random
for i in range(5):
x=random.randint(50,100)
print(x)
78
94
84
52
55
#4 Program to create a list of 5 random numbers
INPUT OUTPUT
import random
l=[]
for i in range(5):
x=random.random()
y=int(x*10)
l.append(y)
print("List of random values:",l)
List of random values: [4, 1, 3, 6,
2]
19. STACKS
#1 Stack implementation: perform pop and push operations on a list
of numbers
INPUT OUTPUT
def pstack():
s=[]
ch='y'
while ch=='y' or ch=='Y':
print("1. Push")
print("2. Pop")
print("3. Display")
c=int(input("enter choice.."))
if c==1:
ele=input("Enter element to add
to stack")
s.append(ele)
elif c==2:
if(s==[]):
print("Stack Empty")
else:
1. Push
2. Pop
3. Display
enter choice..3
Invalid input
continue...y
1. Push
2. Pop
3. Display
enter choice..1
Enter element to add to stack56
1. Push
2. Pop
3. Display
enter choice..3
56 -->
Invalid input
20. print("Deleted
element",s.pop())
elif c==3:
l=len(s)
for i in range(l-1,-1,-1):
print(s[i],"-->")
else:
print("Invalid input")
ch=input("continue...")
pstack()
continue...y
1. Push
2. Pop
3. Display
enter choice..2
Deleted element 56
1. Push
2. Pop
3. Display
enter choice..
#2 Program to display vowels present inauser input word using stacks
INPUT OUTPUT
v=['a','e','i','o','u']
w=input("Enter the word")
s=[]
for letter in w:
if letter in v:
if letter not in s:
s.append(letter)
print(s)
Enter the wordtable
['a', 'e']
Enter the wordhello
['e', 'o']
21. #3 REVERSING EACH WORD IN A STRING
INPUT OUTPUT
str1="Python Programming"
l1=str1.split()
str2=''
print("REVERSING EACH WORD IN A STRING
USING STRING(CONCATENATION)METHOD")
for i in l1:
for j in range(len(i)-1,-1,-1):
str2+=i[j]
str2+=''
print("Reversed string is",str2)
print("REVERSE EACH WORD USING STACK
IMPLEMENTATION")
revs=[]
for w in l1:
for j in range(len(w)-1,-1,-1):
revs.append(w[j])
revs.append('')
frevs=''.join(revs)
print("Original String:",str1)
print("Reversed String:",frevs)
REVERSING EACH WORD INA STRING
USING
STRING(CONCATENATION)METHOD
Reversed string is nohtyPgnimmargorP
REVERSE EACH WORD USING STACK
IMPLEMENTATION
Original String: Python Programming
Reversed String: nohtyPgnimmargorP
22. CSV FILES
#1 Writing ina CSV file
INPUT OUTPUT
import csv
f=open("St.csv","a+")
sw=csv.writer(f)
sw.writerow(["Adm
No","Name","Marks"])
for i in range(2):
print("Enter st record:")
r=int(input("Enter adm no"))
n=input("Enter name.")
m=float(input("Enter marks"))
sr=[r,n,m]
sw.writerow(sr)
f.close()
Enter st record:
Enter adm no27323
Enter name.Anushka
Enter marks98
Enter st record:
Enter adm no27324
Enter name.Vanya
Enter marks99
#2 Reading a CSV file
INPUT OUTPUT
#reading and diplay the contents from
the csv file
#import c-write
import csv
with open("St.csv","r",newline='rn')
as f:
cr=csv.reader(f)
print("Contents of csv file:")
for rec in cr:
print(rec)
Contents of the CSV File St.csv
[ ' Adm No ' , ' Name ', " Marks ' ]
[ ' 6573 ' , ' No ' , ' 98.0 ' ]
[ ' 6473 ' , ' Yes ' , ' 75.0 ' ]
[ ' 6476 ', ' Maybe ' , ‘ 68.0 ’ ]
[ ' 6374 ' , ' Sure ' , ' 99.0 ' ]
[ ' 6764 ' , ' IDK ' , ‘ 58.0 ' ]
[ ' Adm No ' , ' Name ' , " Marks ‘ ]
[ ' 6523 ' , ' Nu ' , ‘ 87.0 ‘ ]
[ ' 6912 ' , ' Hu ' , ' 57.0 ‘ ]
23. SQL
TABLE – 1
Q1. Create a Table “EmpN” with appropriate data types and primary key.
Q2. Insert the values to all the columns of the table.
24. Q3. Display the structure of the given table.
Q4. Display the employee details who have state id between 1 and 5.
Q5. Display the ID and Name of employees with names ending with “v”.
Q6. Display the details of the employees with Nation ID more than 4 or living in Delhi.
25. Q7. Display the maximum National ID and minimum State IDin table “EmpN”.
Q8. Display the total number of Employees in table “EmpN”.
Q9. If total number of employees is equal to the sum of top 8 employees. Then display the total
number of employees hired by the company.
26. Q10. Add a column of Work Experience to the table.
Q11. Display the average work experience of all the employees.
Q12. Increase the Work Experience of all the employees by 1 year.
Updated File
27. Q13. List Work Experience of employees in table EmpN.
Q14. Display the name of employees grouping them in Work Experience.
Q15. Display the details of the employees in the descending order of their State IDs.
28. TABLE – 2
Q1. Create a Table “Empl” with appropriate data types and primary key.
Q2. Insert the values to all the columns of the table.
29. Q3. Display the structure of the given table.
Q4. Display the employee details who are earning between 70000 and 80000.
Q5. Display the ID and Name of employees with names ending with “n”.
30. Q6. Display the details of the employees with age more than 27 or salary between 60000 and
70000.
Q7. Display the highest and lowest salary in table “Empl”.
Q8. Display the total number of Employees in table “Empl”.
Q9. Display the total money given by the company to the employees per month.
31. Q10. Display the average salary of all the employees.
Q11. Increase the salary of all the employees by 10%.
Updated Table
32. Q12. Add a column of Attendance% to the table.
Q13. List all ages of employees in table Empl.
33. Q14. Display the name of employees grouping them in ages.
Q15. Display the details of the employees in the descending order of their salaries.