Xii CSC Practicals
Xii CSC Practicals
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)
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]
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()
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()
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()
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()
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()
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