You are on page 1of 17

1.

WAP to create a list of integers and arrange it's elements in


ascending order with the help of bubble sort technique.
l=[]
n=int(input("Enter size of list: "))
for i in range (n):
x=int(input("Enter element: "))
l.append(x)
print("List you entered is",l)
for i in range (n):
for j in range (0,n-i-1):
if l[j]>l[j+1]:
l[j], l[j+1] = l[j+1], l[j]
print("Sorted list is",l)

OUTPUT

>>>
Enter size of list: 5
Enter element: 5
Enter element: 9
Enter element: 7
Enter element: 2
Enter element: 8
List you entered is [5, 9, 7, 2, 8]
Sorted list is [2, 5, 7, 8, 9]
>>>

2.WAP to create a list of integers and arrange it's elements in


ascending order with the help of insertion sort technique.
l=[]
n=int(input("Enter size of list: "))
for i in range (n):
x=int(input("Enter element: "))
l.append(x)
print("List you have entered",l)
for i in range (1,n):
k=l[i]
j=i-1
while j>=0 and k<l[j]:
l[j+1] = l[j]
j=j-1
else:
l[j+1] = k
print("Sorted list is",l)

OUTPUT

>>>
Enter size of list: 5
Enter element: 4
Enter element: 9
Enter element: 3
Enter element: 7
Enter element: 1
List you have entered [4, 9, 3, 7, 1]
Sorted list is [1, 3, 4, 7, 9]
>>>

3. WAP to create a list of integers and search any elemnents in it


with linear search.

l=[]
n=int(input("Enter no. of elements:"))
for i in range (n):
x=int(input("Enter any elements:"))
l.append(x)
r=int(input("Enter element you want to search:"))
a=0
print("Elements in list are",l)
for i in range (n):
if l[i]==r:
print("Elements found at location number",i+1)
a=1
if a==0:
print("Element not found")

OUTPUT

>>>
Enter no. of elements:5
Enter any elements:4
Enter any elements:9
Enter any elements:3
Enter any elements:7
Enter any elements:2
Enter element you want to search:7
Elements in list are [4, 9, 3, 7, 2]
Elements found at location number 4
>>>

4.WAP to create a list of integers and search any element in it


with binary search.

l=[]
n=int(input("Enter size of list:"))
for i in range (n):
x=int(input("Enter element:"))
l.append(x)
r=int(input("Enter element you want to search:"))
print(a)
a=beg=0
last=n-1
while beg<=last:
mid=(beg+last)//2
if a[mid]==r:
print("Element found at location number:",mid+1)
a=1
break
elif a[mid]<r:
beg=mid+1
else:
last=mid-1
if a==0
print("Element not found")

OUTPUT
>>>
Enter size of list:5
Enter element:9
Enter element:3
Enter element:7
Enter element:8
Enter element:5
Enter element you want to search:7
Element found at location number:3
>>>

5.WAP to create a dictionary with roll number as key and name


as value. input and display details of N students.

d={}
n=int(input("Enter no. of records:"))
for i in range (n):
x=int(input("Enter roll no.:"))
nm=input("Enter name:")
d[x]=nm
print("Data from dictionary is",d)

OUTPUT

>>>
Enter no. of records:5
Enter roll no.:1
Enter name:saif
Enter roll no.:2
Enter name:zaid
Enter roll no.:3
Enter name:faiz
Enter roll no.:4
Enter name:rahul
Enter roll no.:5
Enter name:ram
Data from dictionary is {1: ‘saif’, 2: ‘zaid’, 3: ‘faiz’, 4: ’rahul’, 5: ‘ram’}
>>>

6.WAP to create a list of elements with consist of product no.,


name and price. Input details of N products and display detail of
a product whose product no. is entered by user.

d={}
n=int(input("Enter how many records:"))
for i in range (n):
x=int(input("Enter product no.:"))
pm=input("Enter product name:")
d[x]=pm
x=int(input("Enter product no. to search name:"))
for i in d:
if i==x:
print("product no. found")
print("name=",d[x])
break
else:
print("product no. not found")

OUTPUT

>>>
Enter how many records:5
Enter product no.:1
Enter product name:milk
Enter product no.:2
Enter product name:shampoo
Enter product no.:3
Enter product name:biscuit
Enter product no.:4
Enter product name:chips
Enter product no.:5
Enter product name:chocolate
>>>

7.WAP to input any string and count how many lowercase and
uppercase letters are present in it.

s=input("Enter any string: ")


c1=c2=0
for i in s:
if i.islower():
c1=c1+1
if i.isupper():
c2=c2+1
print("Number of lowercase letters present in the string is",c1)
print("Number of uppercase letters present in the string is",c2)

OUTPUT

>>>
Enter any string: Hello How You Doing
Number of lowercase letters present in the string is 12
Number of uppercase letters present in the string is 4
>>>

8.WAP to input any string and create another string by using it


in such a way that all lowercase letters are converted into
uppercase and vice-versa.

S=input("Enter any string:")


S1=0
for i in S:
if i.islower():
S1 = S+i.isupper()
elif i.isupper():
S1 = S+i.islower()
print("String you entered is:",S)
print("New string is:",S1)

OUTPUT

>>>
Enter any string:Hello How Are You
String you entered is: Hello How Are You
New string:hELLO hOW aRE yOU
>>>

9.WAP to create a text file by writing data on it. Read and


display contents of file.

f=open("Hello.txt","w")
ch="y"
while ch=="y":
s=input("Enter data to write on file:")
f.write (s)
ch=input("Enter y/n to write more data on file:")
f.close()
f1= open("Hello.txt","r")
s= f1.read()
print("Data from file is:",s)
f1.close()

OUTPUT

>>>
Enter data to write on file:Heyy Rahul how are you?
Enter y/n to write more data on file:y
Enter data to write on file:What are your plans for tomorrow?
Enter y/n to write more data on file:n
Data from file is: Heyy Rahul how are you? What are your plans for
tomorrow?
>>>

10.WAP to read contents of a text file and count no. of lines in


this file which are starting with letter T.

f=open("Hello.txt","r")
S=f.readline()
c=0
while S:
if S[0]=="T":
c=c+1
S=f.readline()
print("No. of lines starting with letter T:",c)
f.close()

OUTPUT

>>>
No. of lines starting with letter T:1
>>>

11.WAP to read contents of a text file and count how many


words are starting with letter "A".
f= open("Hello.txt","r")
S=f.read()
W=s.split()
c=0
for x in W:
if x[0]=="A":
c=c+1
print("No. of words starting with A:",c)
f.close()

OUTPUT

>>>
No. of words starting with A:1
>>>
12.WAP to read contents of a text file and display al lines
starting with uppercase vowel.

f=open("Hello.txt","r")
s=f.read()
w=s.split()
c=0
for x in w:
if x[0] in ["A","E","I","O","U"]:
c=c+1
print("No. of words starting with uppercase vowel",c)
f.close()

OUTPUT

>>>
('No. of words starting with uppercase vowel:', 0)
>>>

13.WAP to read contents of a text file and count no. of times


word 'Hello' found in file.

f=open("Hello.txt","r")
s=f.read()
w=s.split()
c=0
for x in w:
if x[0]=="Hello":
c=c+1
print("No. of times word 'Hello' found in file",c)
f.close()

OUTPUT
>>>
("No. of times word 'Hello' found in file", 0)
>>>
14.WAP to count how many lowercase and uppercase letters are
present in a text file.

f=open("Hello.txt","r")
s=f.read()
w=s.split()
c1=c2=0
for i in w:
for j in i:
if j.islower():
c1=c1+1
else:
c2=c2+1
print("No. of lowercase letters are",c1)
print("No. of uppercase letters are",c2)
f.close()

OUTPUT

>>>
('No. of lowercase letters are', 0)
('No. of uppercase letters are', 0)
>>>
15.WAP to copy contents of a text file in another text file in
such a way that words starting with uppercase are copied only.

f1=open("Hello.txt","r")
f2=open("Hello1.txt","w")
s= f1.read()
w=s.split()
for i in w:
if x[0].isupper:
f2.write(x)
f1.close()
f2.close()

OUTPUT

>>>
Hello
>>>

16.WAP to create a binary file by writing roll no., name and


marks of students in it. Also search details of a student whose
roll no. is entered by user.
import pickle
f=open("Stud.DAT","wb")
d={}
n=int(input("Enter no. of records:"))
for i in range(n):
r=int(input("Enter rollno.:"))
nm=input("Enter name:")
m=int(input("Enter marks:"))
d["rollno."]=r
d["name"]=nm
d["marks"]=m
pickle.dump(d,f)
f.close()
f=open("Stud.DAT","rb")
r=int(input("Enter rollno. of student:"))
print("Details of student whose rollno. is entered by user:")
try:
while True:
x=pickle.load(f)
if x["rollno."]==r:
print(x)
except EOFError:
f.close()
OUTPUT

>>>
Enter no. of records:2
Enter rollno.:1
Enter name:saif
Enter marks:98
Enter rollno.:2
Enter name:sam
Enter marks:95
Enter rollno. of student:1
Details of student whose rollno. is entered by user:
(‘rollno.’: 1, ‘name’: saif, ‘marks’: 98)
>>>
17.WAP to update marks of a student on a binary file whose roll
no. is entered by user.

import pickle
f=open("Hello.txt","wb")
n=int(input("Enter no. of records:"))
for i in range(n):
d={}
r=int(input("Enter rollno.:"))
nm=input("Enter name:")
m=int(input("Enter marks:"))
d["rollno."]=r
d["name"]=nm
d["marks"]=m
pickle.dump(d,f)
f.close()
f=open("Hello.txt","rb+")
d=pickle.load(f)
up=int(input("Enter rollno. whose marks you want to update:"))
for i in d:
if d[1]==up:
m=int(input("Enter new marks:"))
d["marks"]=m
print(d)
else:
print("rollno. does not exist:")
break
pickle.dump(d,f)
f.close()

OUTPUT
>>>
Enter no. of records:1
Enter rollno.:1
Enter name:saif
Enter marks:95
Enter rollno. whose marks you want to update:1
Enter new marks:99
(‘rollno.’: 1, ‘name’: saif, ‘marks’: 99)
>>>

18.WAP to delete record of a student from a binary file whose


roll no. is entered by user.

n=int(input("Enter number of students:"))


d={}
i=0
while i<n:
print("collecting information of student",i+1)
rollno=int(input("Enter rollno.:"))
name=input("Enter name:")
d[rollno]=name
i+=1
print("Details of student before deletion:")
for rollno in d:
print("roll no:",rollno,",","name:",d[rollno])
rollno=input("Enter roll no. you want to delete:")
if rollno in d:
del d[rollno]
print("Details of student after deletion:")
for rollno in d:
print("roll no.",('2'),",","name:",('rahul'))
else:
print("rollno. not found")

OUTPUT
>>>
Enter number of students:2
collecting information of student 1
Enter rollno.:1
Enter name:ram
collecting information of student 2
Enter rollno.:2
Enter name:rahul
Details of student before deletion:
roll no: 1 , name: ram
roll no: 2 , name: Rahul
Enter roll no. you want to delete:2
Details of student after deletion:
roll no: 1 , name: ram
>>>

19.WAP to display details of all students from a binary file


whose marks are more than 95.
import pickle
f=open("student.DAT","rb")
print("Details of students having marks more than 95")
try:
while True:
x=pickle.load(f)
if x["Marks"]>95:
print(x)
except EOFError:
f.close()

OUTPUT
>>>
Details of students having marks more than 95
(‘rollno.’: 1, ‘name’: saif, ‘marks’: 99)
>>>
20.WAP to create a CSV file with emp no., name and salary of
employes. Also display its contents.
n=int(input("Enter the number of employees you want to enter: "))
import csv
f=open("emp.csv","w")
r=csv.writer(f)
r.writerow(["Emp no.","Name","Salary"])
for i in range(n):
empno=int(input("Enter Emp no.: "))
nm=input("Enter Name: ")
s=int(input("Enter Salary: "))
rec=[empno ,nm ,s]
r.writerow (rec)
f.close()
with open(“emp.csv”,”r”,newline=’\r\n’)as f:
rd=csv.reader(f)
for rec in rd:
print(rec)

OUTPUT

>>>
Enter the number of employees you want to enter:2
Enter Emp no.:1
Enter Name:mukesh
Enter Salary:1,00,000
Enter Emp no.:2
Enter Name:dinesh
Enter Salary:50,000
[‘Empno.’,’Name’,’Salary’]
[‘1’,’mukesh’,’1,00,000’]
[‘2’,’dinesh’,’50,000’]
>>>

You might also like