You are on page 1of 22

INDEX

Q1)WAP to count the numbers of words starting with letter ‘T’.


Q2)WAP to create a text file and display it’s contents.
Q3) WAP to count how many words in a file are having more than 5
characters.
Q4)WAP count how many lowercase and uppercase letters are present in a
file.
Q5) WAP to create a binary file . Display detail of a student whose roll no
is entered by the user.
Q6)Create a CSV file with product no ,name& price of n products &
display it’s content
Q7)Program on Bubble sort.
Q8)Program for list arrangement with insertion sort
Q9)WAP to count how many words in a file are having starting and ending
same character
Q10) Program on binary search
Q11) WAP to count how many times letter ‘A’ or ‘a’ is found in a text file
Q12)WAP to create copy of a text file in another file such a way that all the
words starting with lowercase are copied only
Q14)WAP to create a dictionary with name and price of products . Count
products having price more than 100.
Q15)WAP to create a CSV file by writing roll no, name and age of 20
students.
Q16)WAP to read contents from a text file from byte no 10.
Q17) WAP to sort a list.Q18) WAP to count how many lower case vowels
are found in a text file.
Q18) WAP to count how many lower case vowels are found in a text file.
Q19) WAP to copy one file to another in such a way that all words having
length more than 5 characters are copied only.
Q20) WAP which picks out 3 lucky winner for draw from 10,000 people.
Q1)WAP to read contents of a text file and count the
numbers of words starting with letter ‘T’.
PROGRAM
f=open(“WORDS.TXT”,”r”)
w=s.split()
c=0
for x in w:
if x[0]=”T”
c=c+1
print(“no of words”,c)
f.close()

OUTPUT
no of words 5
Q2)WAP to create a text file and display it’s
contents.
PROGRAM
f=open(“ABC.TXT”,”w”)
ch=”y”
while ch==”y”:
s=input(“enter data to write”)
f.write(s)
ch=input(“enter y to write more”)
f.close()
f=open(“ABC.TXT”,”r”)
x=f.read()
print(“data from file”,x)
f.close()
OUTPUT
enter data to writemy name is
enter y to write morey
enter data to writeshreshth
enter y to write more
data from file my name is shreshth
Q3) WAP to read contents of a file and count how
many words in a file are having more than 5
characters.
PROGRAM
F=open(“words.txt”,”r”)
s=f.read()
w=s.split()
c=0
for x in w:
if len(x)>5
c=c+1
print(“no of word having length more than 5
character:”,c)
f.close()

OUTPUT
No of words having length more than 5 character:7
Q4)WAP count how many lowercase and uppercase
letters are present in a file.
PROGRAM
f=open(“letters.txt”,”r”)
s=f.read()
c1=c2=0
w=s.split()
for x in w :
for y in x:
if y.islower():
c1=c1+1
elif y.isupper():
c2=c2+1
print(“no of lowercase letter “,c1)
print(“no of uppercase letter”,c2)
f.close()
OUTPUT
no of lowercase letter 15
no of uppercase letter 9
Q5) WAP to create a binary file by writing a dictionary consists of
record with roll no, name and marks. Display detail of a student
whose roll no is entered by the user.
PROGRAM
import pickle
f=open(“STUD.DAT”,”wb”)
ch=”y”
while ch==”y”:
D={}
R=int(input(“enter roll no”))
Nm=input(“enter name”)
M=float(input(“enter marks”))
D[“roll no”]=R
D[“name”]=Nm
D[“marks”]=M
pickle.dump(D,F)
ch=input(“enter y/n for more data”)
f.close()
f=open(“stud.DAT”,”rb”)
try:
while true
x=pickle.load(F)
print(x)
except EOFError:
f.close()

OUTPUT
Enter roll no3
Enter namejohn
Enter marks99 Enter y/n for more datan [3,john,99]
Q6)Create a CSV file with product no ,name& price of n
products & display it’s content
PROGRAM
import CSV
f=open(“PROD.CSV”,”w”)
r=csv.writer(f)
r.writerow([‘Pno’,’Name’,’Prize’])
for i in range(10):
Pno=int(input(“enter product no”))
Nm=input(“enter name”))
P=int(input(“enter price”))
Rec=input[Pno,Nm,P]
r.writerow(rec)
f.close()
withopen(“STUD.CSV”,”r”, r\n\) as(f)
r=csv.reader(f)
for y in x:
print y
OUTPUT
Enter product no4
Enter nameCOOKIE
Enter price25 [4,COOKIE,25]
Q7) Program on Bubble sort.
PROGRAM
def bubblesort(A):
N=len(A)
for i in range (N-1):
for j in range(0,N-1-i):
if A[j]>A[j+1]:
A[j],A[j+i]=A[j+i],A[j]
print(“list after sorting”,A)
A=[]
N=int(input(“enter size of list”))
for i in range(N):
x=int(input(“enter element”))
A.append(x)
print(“list elements are”,A)
Bubble(A)
OUTPUT
Enter size of list 3
Enter element3
Enter element2
Enter element1
List elements are [3,1,2]
List after sorting [1,2,3]
Q8)Program for list arrangement with insertion sort
PROGRAM
L=[]
n=int(input(“enter size of list”))
for i in range(n):
x=int(input(“enter any no”))
L.append(x)
print(“elements in list are”,L)
for i range(1,n):
M=L[i]
H=i-1
while H>=0 and M<L[H]:
L[H+1]=L[H]
H=H-1
else:
L[H+1]=M
print(“elements in list after sorting”,L)
OUTPUT
Enter size of list 3
Enter any no3
Enter any no1
Enter any no9
Elements in list are [3,1,9]
Elements in list after sorting[1,3,9]
Q9)WAP to count how many words in a file are having
starting and ending same character
PROGRAM
f=open(“words.txt”,”r”)
s=f.read()
c=0
w=s.split()
for x in w:
if x[0]==x[-1]:
c=c+1
print(“no of words”,c)
f.close()

OUTPUT
No of words4
Q10) Program on binary search
PROGRAM
L=[]
N=int (input(“enter size of list”))
for I in range(N):
x=int(input (“enter any element”))
L.append(x)
print (“elements in list are:\n”,L)
L.sort()
r=int(input (“enter element you want to search”))
h=0
beg=0
last=N-1
while beg<=last:
mid=(beg+last)//2
if L[mid]==r:
print (“element found at location”,mid+1)
h+=1 break if h==0: print (“No such element found”)

OUTPUT
enter size of list 3
enter any element6
enter any element69
enter any element85
elements in list are:
[6,69,85]
Enter element you want to search69 Element found at 2
Q11) WAP to count how many times letter ‘A’ or ‘a’ is
found in a text file
PROGRAM
f=open(“letters.txt”,”r”)
S=f.read()
c=0
w=s.split()
for x in w:
for y in x:
if y==’A’ or y==’a’
c=c+1
print(“no of times letter A or a found”,c)
f.close()

OUTPUT
No of times letter A or a found75
Q12)WAP to create copy of a text file in another file
such a way that all the words starting with lowercase
are copied only
PROGRAM
f1=(“text1.txt”,”r”)
f2=(“text2.txt”,”r”)
s=f1.read()
w=s.split()
for x in w:
if x[0].islower():
f2.write(x)
print(“file copied successfully”)
f1.close()
f2.close()

OUTPUT
File copied successfully
Q13) WAP to create a dictionary with roll no and name
of n students and display
PROGRAM
D={}
N=int(input(“enter no of students”))
for i in range(N):
R=int(input(“enter roll no”))
Nm=input(“enter name”))\
D[R]=Nm
print(“data on dictionary”,D)

OUTPUT
Enter no of students2
Enter roll no1
enter nameginny
enter roll no2
enter namejohny
data on dictionary {1: ’ginny’,2: ’johny’}
Q14)WAP to create a dictionary with name and price of
products . Count products having price more than 100.
PROGRAM
D={}
c=0
N=int(input(“enter no of records”))
for i in range(N):
P=int(input(“enter product no”))
Nm=input(“enter name of product”)
D[Nm]=Price
for i in D:
if D[i]>100:
c=c+1
print(“no of products having price more than 100”,c)
OUTPUT
Enter no of records2
Enter price250
Enter name of productdetergent
Enter price60
Enter name of productpen
No of products having price more than 100
{250: ‘detergent’}
Q15)WAP to create a CSV file by writing roll no, name and
age of 20 students.
PROGRAM
import CSV
f=open(“stud.csv”,”w”)
r=csv.writer(f)
r=writerow([‘ roll no’, ‘name’, ‘age’])
for i in range(20):
R=int(input(“enter roll no”))
Nm=input(“enter name”)
A=int(input(“enter age”))
Rec=[‘R’, ‘Nm’, ‘A’]
r.writerow(Rec)
f.close()
withopen(“stud.csv”, “r” , newline= ‘\r\n’) as(f):
x=csv.reader.(f)
for y in x:
print(y)
OUTPUT
Enter roll no3
Enter nameadrian
Enter age18
[3, Adrian, 18]
Q16)WAP to read contents from a text file from byte
no 10.
PROGRAM
f=open(“ABC.TXT”,”r”)
f.seek(10)
s=f.read()
print(“contents from byte no10 “,x)
f.close()

OUTPUT
Contents from byte no10z
Q17) WAP to sort a list.
PROGRAM
lst=[“hello”, “this”, “is”, “a”, “list”]
print(“before sorting the list:”)
print(lst)
lst.sort()
print(“after sorting the list:”)
print(lst)

OUTPUT
Before sorting the list :
['hello', 'this', 'is', ‘a', ‘list’]
after sorting the list :
['a', 'hello', 'is', 'list', ‘this’]
Q18) WAP to count how many lower case vowels
are found in a text file.

PROGRAM
F=open(“words.txt”, “r”)
s=f.read()
c=0
w=s.split()
for x in w:
for y in x:
if y in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]:
c=c+1
print(“no of vowels”,c)
f.close()

OUTPUT
No of vowels69
Q19) WAP to copy one file to another in such a
way that all words having length more than 5
characters are copied only.

PROGRAM
f1=open(“text.txt”, “r”)
f2=open(“text2.txt”, “r”)
s=f1.read()
w=s.split()
for x in w:
if len(x)>5:
f2.write(x)
print(“file copied successfully”)
f1.close()
f2.close()

OUTPUT
File copied successfully
Q20) WAP which picks out 3 lucky winner for
draw from 10,000 people.

PROGRAM
import random
person1=random.randint(1,10000)
person2=random.randint(1,10000)
person3=random.randint(1,10000)
print("3 lucky winnners are")
print(person1,person2,person3)

OUTPUT
3 lucky winnners are
5560 1158 1042

You might also like