Professional Documents
Culture Documents
CS File
CS File
INDEX
S no. Topic T.Sign
Q-2 WAP to read only first 10 characters from the file stud.txt .
CODE:-
f=open("stud.txt","r")
data=f.read(10)
print(data)
f.close
OUTPUT:-
Q-3 WAP to count number of lines in text file story.txt which is starting
with a.
CODE:-
f=open("story.txt","r")
c=0
lines=f.readlines()
for l in lines:
if l[0]=='A' or l[0]=='a':
c=c+1
print("Total number of lines starting with A or a:",c)
f.close()
OUTPUT:-
Q-4 WAP to print number of words in each line in file stud txt.
CODE:-
f=open("stud.txt","r")
p=f.readlines()
n=1
for i in p:
s=i.split(" ")
print("Number of words in line:",n,len(s))
n+=1
OUTPUT:-
CODE:-
f=open("test.txt","w")
line1=input("Enter a line to add:")
f.write(line1)
line2=input("Enter a line to add:")
f.write(line2)
f.close()
OUTPUT:-
Q-6 WAP to create a file stud.txt and count total number of words in it.
CODE:-
f=open("stud.txt","r")
d=f.read()
n=d.split()
print(len(n))
f.close()
OUTPUT:-
Q-7 WAP to display odd lines() to display odd number of lines from the
text file and also display total number of lines .
CODE:-
def oddlines(f):
d=list(f)
for i in range (0,len(d)):
i%2==0
print(d[i])
print ("total number of lines are:",len(d))
z=open("read.txt","r")
oddlines(z)
OUTPUT:-
OUTPUT:-
OUTPUT:-
CODE:-
import csv
fields=['Name','Class','Year','Percent']
row=[['Mohit','Xll-S','2023','89.5'],
['Aaina','Xll-S','2023','90'],
["Rehan","Xll-S","2024","92"]]
with open("Marks.csv","w") as f:
csv_w=csv.writer(f,delimiter =' ')
csv_w.writerows(fields)
csv_w.writerow(row)
for r in row:
csv_w.writerow(r)
OUTPUT:-
Q-12 WAP to store multiple integers in binary file and read it display
content on the screen.
CODE:-
import pickle
def readbin():
f=open("Number.dat","rb")
try:
while True:
y=pickle.load(f)
print(y)
except EOFError:
pass
f.close()
def writebin():
f=open("Number.dat","ab")
while True:
x=int(input("Enter the integer:"))
pickle.dump(x,f)
ask=input("Do you want to continue Y/N:")
if ask.upper()=="N":
break
f.close()
writebin()
readbin()
OUTPUT:-
Q-13 WAP to insert/ push/delete/pop and traverse the stack using list.
CODE:-
s=[]
def insert(N):
s.append(N)
def delete():
if s==[]:
print("Stack Underflow")
else:
x=s.pop()
print("Deleted element from stack top:",x)
def display():
if s==[]:
print("Empty Stack")
else:
print("Current stack states:")
for i in range(len(s)-1,-1,-1):
print(s[i],end=" ")
while True:
print("1.PUSH \n 2.POP \n 3.DISPLAY\n 4.EXIT")
ch=int(input("Enter your choice:"))
if ch==1:
N=input("Enter element to be inserted:")
insert(N)
elif ch==2:
delete()
elif ch==3:
display()
elif ch==4:
break
else:
print("Invalid Choice!!")
OUTPUT:-
Q-14 Write a menu driven programme to add, search and display data
from csv file, searching should be done according to the roll number
imputed by the user. Name of the file is “student.csv” .
CODE:-
import csv
def search():
f=open("student.csv","r")
a=csv.reader(f)
h=input("Enter Roll number:")
for i in a:
if i[0]==h:
print(i)
def readcsv():
with open("student.csv","r") as f:
d=csv.reader(f)
for r in d:
print(r)
def writecsv():
r=[]
rollno=int(input("Enter roll number:"))
name=input("Enter name:")
stream=input("Enter stream:")
marks=int(input("Enter marks:"))
data=[rollno,name,stream,marks]
r.append(data)
with open("student.csv","a",newline="")as f1:
csv_w=csv.writer(f1,delimiter=",")
csv_w.writerow(r)
OUTPUT:-
OUTPUT:-
Q-18 WAP to add and subtract two numbers and return numbers.
CODE:-
def calc(a,b):
sub=a-b
add=a+b
return sub,add
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
s,a=calc(x,y)
print("Subtraction:",s,"\t Addition:",a)
OUTPUT:-
Q-20 WAP to find largest element from the list using user define
function which will take list as input in parameter and return largest
value in the function.
CODE:-
def lst(a):
lar=a[0]
for i in a:
if i>lar:
lar=i
return lar
x=eval(input("Enter a list:"))
print("Largest Number is:",lst(x))
OUTPUT:-
Q21 WAP to define a user define function which will calculate average
of even number and then return the result in the calling place.
CODE:-
def avg(l):
n=[]
for i in l:
if i%2==0:
n.append(i)
a=0
for i in n:
a=a+i
i+=1
return(a/len(n))
l=eval(input("Enter a list:"))
v=avg(l)
print(v)
OUTPUT:-
Q22 WAP to count total number of upper case, lower case and digits
in the given string using user define function which will take strings
as input .
CODE:-
e=[]
r=[]
t=[]
def str():
for i in s:
if i.isupper():
e.append(i)
elif i.islower():
r.append(i)
elif i.isdigit():
t.append(i)
print("Upper Case:",len(e))
print("Lower Case:",len(r))
print("Digits:",len(t))
s=input("Enter a string:")
str()
OUTPUT:-
Q24 WAP to input a list and pass to a function parameter which will
search an item from the list.
CODE:-
def lst(a,b):
pos=0
for i in a:
if i==b:
return pos
else:
pos+=1
a=eval(input("Enter a list:"))
b=int(input("Enter a number:"))
print("Position:",lst(a,b))
OUTPUT:-