0% found this document useful (0 votes)
32 views6 pages

Xii CSC Practicals

Uploaded by

zimaamkhilji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Xii CSC Practicals

Uploaded by

zimaamkhilji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

GRADE XII – COMPUTER SCIENCE

PROGRAMS FOR WRITING IN PRACTICAL RECORD NB


EX.NO – 1 FUNCTIONS
1 Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple
containing length of each word of a string.

def lenWords(STRING):
t=()
a=STRING.split()
for i in a:
t=t+(len(i),)
return(t)
s=input("Enter a string")
t1=lenWords(s)
print("length of each word of a given string is",t1)

OUTPUT
Enter a string Come let us have some fun
length of each word of a given string is (4, 3, 2, 4, 4, 3)

2 Write a function CountNow(PLACES) in Python, that takes the dictionary, PLACES as an


argument and displays the names in uppercase of the places whose names are longer than 5
characters.

def CountNow(PLACES):
for i in PLACES:
if len(PLACES[i])>5:
print(PLACES[i].upper())
PLACES={1:'Delhi',2:'London',3:'Paris',4:'New York',5:'Doha'}
CountNow(PLACES)

OUTPUT
LONDON
NEW YORK
3 Write a function EOReplace() in Python, which accepts a list L of numbers. Thereafter, it
increments all even numbers by 1 and decrements all odd numbers by 1.

def EOReplace(L):
for i in range(len(L)):
if L[i]%2==0:
L[i]+=1
else:
L[i]-=1
print('List after updation',L)
L=eval(input("Enter a list"))
EOReplace(L)

OUTPUT
Enter a list[10,20,30,40,35,55]
1
List after updation [11, 21, 31, 41, 34, 54]

4 Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
function. The function returns another list named ‘indexList’ that stores the indices of all non-
Zero elements of L.

def INDEX_LIST(L):
indexList=[]
for i in range(len(L)):
if L[i]!=0:
indexList+=[i]
print("Index of non_zero elements is",indexList)
L=eval(input("Enter a list"))
INDEX_LIST(L)

OUTPUT
Enter a list[12,4,0,11,0,56]
Index of non_zero elements is [0, 1, 3, 5]

EX.NO – 2 TEXT FILES

5 Write a function, vowelCount() in Python that counts and displays the number of vowels in
the text file named india.txt.

def vowelcount():
with open("india.txt","r")as f:
vowel=0
data=f.read()
for i in data:
if i in "AaEeIiOoUu":
vowel+=1
print("Number of vowels present is",vowel)
vowelcount()

Content of text file india.txt


One, two, three, four, five
Once I caught a fish alive.
Output
number of vowels present is 19

6 Write a function, casecount() in Python that counts and displays the number of uppercase and
lowercase letters in the text file named india.txt.

def casecount():
with open("india.txt","r")as f:
upcount=0
locount=0
data=f.read()
for i in data:

2
if i.isupper():
upcount+=1
if i.islower():
locount+=1
print("Lowercase letters",upcount)
print("Uppercase letters",locount)
casecount()

Content of text file india.txt


One, two, three, four, five
Once I caught a fish alive.
Output
Lowercase letters 3
uppercase letters 37

7 Write a function count_Dwords () in Python to count the words ending with a digit in a text file
"Details.txt".

def count_Dwords():
with open("Details.txt","r")as f:
count=0
data=f.read().split()
for i in data:
if i[-1].isdigit():
count+=1
print("Number of words ending with a digit is ",count)
count_Dwords()

Content of text file Details.txt


On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output
Number of words ending with a digit is 4

8 Write a function count_4letter () in Python to count four letter words in a text file"Details.txt".

def count_4letter():
with open("Details.txt","r")as f:
count=0
data=f.read().split()
for i in data:
if len(i)==4:
count+=1
print("Number of words with 4 characters is ",count)
count_4letter()

Content of text file Details.txt


On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output Number of words with 4 characters is 3
3
9 Write a function WHstart() in python to count the number lines in a text file‘Country.txt’
which is starting with an alphabet ‘W’or‘H’.
def WHstart():
with open("Country.txt","r")as f:
wcount=0
hcount=0
data=f.readlines()
for i in data:
if i[0] in "wW":
wcount+=1
if i[0] in "hH":
hcount+=1
print("Number of lines starting with W/w is ",wcount)
print("Number of lines starting with h/H is ",hcount)
WHstart()

Content of text file Country.txt


Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Output
Number of lines starting with W/w is 1
Number of lines starting with h/H is 2

10 Write the definition of a Python function named LongLines ( ) which reads the contents of a
text file named Country.txt and displays those lines from the file which have at least 8 words
in it.

def LongLines():
with open("Country.txt","r")as f:
data=f.readlines()
for i in data:
if len(i.split())>7:
print(i)
LongLines()

Content of text file Country.txt


Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Output
Whose woods these are I think I know.
To watch his woods fill up with snow.

4
EX.NO – 3 BINARY FILES

11 Consider the binary file “Emp.dat” storing the details of employees such as
empno,name,salary,designation in a the form of a list.
Write a function write file() to input multiple records from the user and add to Emp.dat and
Write a function readfile() to the contents of the file Emp.dat and display it to the user.

import pickle
def writefile():
with open("Emp.dat","ab")as f:
while True:
empno=int(input("Enter Emp No"))
name=input("Enter Emp Name")
salary=int(input("Enter Emp salary"))
des=input("Enter Emp Designation")
data=[empno,name,salary,des]
pickle.dump(data,f)
ch=input("Want to continue??(y/n)")
if ch in "Nn":
break
def readfile():
f=open("Emp.dat","rb")
try:
while True:
print(pickle.load(f))
except:
f.close()
writefile()
readfile()

OUTPUT
Enter Emp No7369
Enter Emp NameSMITH
Enter Emp salary20000
Enter Emp DesignationCLERK
Want to continue??(y/n)Y
Enter Emp No7499
Enter Emp NameALLEN
Enter Emp salary30000
Enter Emp DesignationSALESMAN
Want to continue??(y/n)Y
Enter Emp No7521
Enter Emp NameWARD
Enter Emp salary40000
Enter Emp DesignationMANAGER
Want to continue??(y/n)N

5
***The contents of the file Emp.dat ***
[7369, 'SMITH', 20000, 'CLERK']
[7499, 'ALLEN', 30000, 'SALESMAN']
[7521, 'WARD', 40000, 'MANAGER']

12 Write a user defined function salary() to search and display empno and name from the binary
file Emp.dat, whose salary is more than 40000.

import pickle
def salary():
f=open("Emp.dat","rb")
try:
while True:
data=pickle.load(f)
if data[2]>30000:
print(data[0],data[1])
except:
f.close()
salary()

OUTPUT
7521 WARD

13 Write a function update() to update the salary of the employees in the file Emp.dat, so that
whose salary is greater than 20000 and less than 30000, add 5000 to their salary as bonus.

import pickle
def update():
f=open("Emp.dat","rb+")
try:
while True:
pos=f.tell()
data=pickle.load(f)
if data[2]<=30000 and data[2]>=20000:
data[2]-=5000
f.seek(pos)
pickle.dump(data,f)
except:
print("Updation done successfully")
f.close()
update()

OUTPUT
Updation done successfully

You might also like