0% found this document useful (0 votes)
20 views4 pages

Assignment Text 2 Key

jkiyg

Uploaded by

anilkoranga.pc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
20 views4 pages

Assignment Text 2 Key

jkiyg

Uploaded by

anilkoranga.pc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

In this assignment you are supposed to write functions for the following questions.

It
will not make any difference, just write the code within body of the function. No need to
call the function.
For example the question in previous assignment was :
Program-b: Count number of lines in file myfile.txt.
IF same program was: to write a function CountLines() in python to count and print number
of lines in file “myfile.txt”. Then answer would be.
def CountLines(): // the only difference
fin=open("myfile.txt",'r')
str=fin.readlines()
count_line=0
print(len(str))
for i in str:
count_line=count_line+1
print(“Number of Lines in file: “,count_line)
fin.close( )
TEST
Now Attempt following questions:
The contents of file “STORY.TXT”, used in some questions may be considered as:

“This is my website. I have displayed,\n my preferences in the CHOICE section.”

Q1. Write a function CountUcase() in python to read text file “STORY.TXT”, count and print the
number of uppercase alphabets present in the file.
The Countucase( ) function should display the output as:
Upper Case characters= 8
def Countucase( ):
f=open("STORY.TXT ",'r')
str=f.read()
c=0
for i in str:
if i.isupper():
c+=1
print("Upper Case characters = ",c)
Q2. Write a function in python to read text file “STORY.TXT”, count and print the number of
spaces and commas present in the file.
def count():
f=open("STORY.TXT ",'r')
str=f.read()
s=0
c=0
for i in str:
if i.isspace():
s+=1
elif i==',':
c+=1
print("Spaces = ",s)
print("Commas = ",c)
Q3. Write a function CountMyThe() in python to read text file “STORY.TXT”, count and print the
number of occurrences of words “my” and “the” in the file.

The CountMyThe( ) function should display the output as:


“my occurs 2 times”
“the occurs 1 times”
def CountMyThe( ):
f=open("corona.txt",'r')
str=f.read()
s=0
c=0
l=str.split()
for i in l:
if i=="my":
s+=1
elif i=='the':
c+=1
print("my occurs ", s ," times")
print("the occurs ", c ," times")
Q4. Write a method/function DISPLAtYWORDS() in python to read a text file STORY.TXT, and
display words starting with ‘T’ or ‘t’.

The function should print()


This
The
def DISPLAtYWORDS():
f=open("corona.txt",'r')
str=f.read()
s=0
c=0
l=str.split()
for i in l:
if i[0]=="T" or i[0]=="t" :
print(i)
Q5. Write a function in python to count the number of lines in a text file ‘ABC.TXT’ which are
starting with an alphabet ‘A’ or ‘a’ .
def COUNTLINES():
file=open('STORY.TXT','r')
lines = file.readlines()
count=0
for w in lines:
if w[0]=="A" or w[0]=="a":
count=count+1
print(“Total lines “,count)
file.close()

Q6. Write a method/function DISPLAYWORDS() in python to read lines from a text file
STORY.TXT, and display words having less than 4 characters.

The function should print()


is
my
I
my
in
the
def DISPLAYWORDS():
c=0
file=open(‘STORY.TXT','r')
line = file.read()
word = line.split()
for w in word:
if len(w)<4:
print( w)
file.close()

Q7. Write a python code to find the size of the file in bytes, number of lines and number of words.
# reading data from a file and find size, lines, words
f=open(‘Lines.txt’,’r’)
str=f.read( )
size=len(str)
print(‘size of file n bytes’,size)
f.seek(0)
L=f.readlines( )
word=L.split( )
print(‘Number of lines ’,len(L))
print(‘Number of words ’,len(word))
f.close( )
Q8. Write function to print just the last word of a text file “data.txt”.
Ans:
def print_last():
fin=open(“data.txt”,”r”)
S=fin.read()
L=S.split()
print(“Last word = “,L[-1])

fin.close()

Q9. Write a function countVowel() to read the file “story.txt” and print number of lower case vowels
in each word of the file.

Output should be:


This : 1 vowels
is : 1 vowels
my : 0 vowels
website. : 3 vowels
I : 0 vowels
have : 2 vowels
displayed, : 3 vowels
my : 0 vowels
preferences : 4 vowels
in : 1 vowels
the : 1 vowels
CHOICE : 0 vowels
section. : 3 vowels

def countVowel():

fin=open("myfile.txt",'r')

str=fin.read()

L=str.split()

for i in L: # # i will contain a single word at a time


p=0

for x in i: # loop for each character in i

if x in [„a‟,‟e‟,‟i‟,‟o‟,‟u‟]:

p=p+1

print(i," : ",p, “ vowels”)

fin.close( )

You might also like