Computer Science Project
Computer Science Project
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 elements 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:srijan
Enter roll no.:2
Enter name:varad
Enter roll no.:3
Enter name:shubh
Enter roll no.:4
Enter name:shri
Enter roll no.:5
Enter name:rohit
Data from dictionary is {1: ‘srijan’, 2: ‘varad’, 3: ‘shubh’, 4:’shri’, 5:‘rohit’}
>>>
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:cookies
Enter product no.:2
Enter product name:cold drink
Enter product no.:3
Enter product name:toffee
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
>>>
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.
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 srijan 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 srijan 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
>>>
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
>>>
OUTPUT
>>>
Enter no. of records:2
Enter rollno.:1
Enter name:srijan
Enter marks:98
Enter rollno.:2
Enter name:shri
Enter marks:95
Enter rollno. of student:1
Details of student whose rollno. is entered by user:
(‘rollno.’: 1, ‘name’: srijan, ‘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:srijan
Enter marks:95
Enter rollno. whose marks you want to update:1
Enter new marks:99
(‘rollno.’: 1, ‘name’: srijan, ‘marks’: 99)
>>>
OUTPUT
>>>
Details of students having marks more than 95
(‘rollno.’: 1, ‘name’: srijan, ‘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:suresh
Enter Salary:1,00,000
Enter Emp no.:2
Enter Name:mahesh
Enter Salary:50,000
[‘Empno.’,’Name’,’Salary’]
[‘1’,’suresh’,’1,00,000’]
[‘2’,’mahesh’,’50,000’]
>>>