Professional Documents
Culture Documents
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]
>>>
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]
>>>
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
>>>
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
>>>
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’}
>>>
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.
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
>>>
OUTPUT
>>>
Enter any string:Hello How Are You
String you entered is: Hello How Are You
New string:hELLO hOW aRE yOU
>>>
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?
>>>
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
>>>
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)
>>>
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
>>>
>>>
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)
>>>
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
>>>
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’]
>>>