You are on page 1of 32

Happy School/2023-2024

INDEX
S no. Topic T.Sign

1. Displaying the content of the file


2. Read only first 10 characters from the file
3. Count Number of lines starting with a
4. Print number of words in each line in file
5. Write data into the file
6. Create a file and count total number of words in
it
7. Display odd number of line from the file
8. Count occurnece of each word
9. Read content of the file
10. Count total number of records in file
11. Write data into a csv file
12. Storing multiple integers in binary file and
displaying them
13. Insert/push/pop/display and transverse the stack
14. Menu driven programme for read, search and
write into a csv file
15. Print the pattern using functions
16. Print palindrome using functions
17. Calculate area of rectangle using function
18. Add and subtract two numbers using functions
19. Calculate area of circle, rectangle, triangle using
user define function
20. Find largest number from the list using user
define functions
21. Calculate average of even numbers using user
define functions
22. Count total number of upper &lower case & digit

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

23. Find occurrence of each element in the


form of dictionary
24. Search an element from the list

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-1 WAP to display the content of file stud.txt.


CODE:-
f=open("stud.txt","r")
data=f.read()
print(data)
f.close
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

Q-5 WAP to write data in the file test2.txt.

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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.

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

CODE:-
f=open("stud.txt","r")
d=f.read()
n=d.split()
print(len(n))
f.close()
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-8 WAP to count occurrence of each word in the file stud.txt.


CODE:-
d={}
f=open("stud.txt","r")
a=f.read()
for i in a:
b=a.count(i)
d[i]=b
for j in d:
print("Occurrence of each word:",j,"is",d[j])
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-9 WAP to read the content of file “student.csv”.


CODE:-
import csv
f=open("student.csv","r")
rows=csv.reader(f)
for r in rows:
print(r)
f.close()

OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-10 WAP to count the total number of record present in file


“student.csv”
CODE:-
import csv
f=open("student.csv","r")
rows=csv.reader(f)
c=0
for r in rows:
c+=1
print("Total Number of records:",c)
f.close()

OUTPUT:-

Q-11 WAP to write students data onto a csv file.


SARTHAK GUPTA XII-S 42 /CS Practical File
Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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()

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

delete()
elif ch==3:
display()
elif ch==4:
break
else:
print("Invalid Choice!!")
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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)

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

print("Enter 1 to read data, 2 to write data and 3 for searching data:")


b=int(input())
if b==1:
readcsv()
elif b==2:
writecsv()
elif b==3:
search()
else:
print("Invalid Value")

OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-15 WAP to print the following pattern using functions.


*
**
***
****
*****
CODE:-
def pattern():
for i in range(1,6):
print()
for j in range(1,i+1):
print("*",end=" ")
pattern()
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-16 WAP to print palindrome using function.


CODE:-
def palindrome(n):
num=n
rev=0
while n>0:
d=n%10
rev=rev*10+d
n=n//10
if num==rev:
return 1
else:
return 0
n=int(input("Enter a Number:"))
x=palindrome(n)
if x==1:
print("It is a palindrome")
else:
print("It is not a palindrome")

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-17 WAP to calculate area of rectangle using function.


CODE:-
def area_rectangle(length,breadth):
area=length*breadth
return area
length=int(input("Enter length:"))
breadth=int(input("Enter breadth:"))
print("Area of rectangle is:",area_rectangle(length,breadth))
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q-19 WAP to calculate area of circle,rectangle,triangle using a user


define function.
CODE:-
def area(a,b,d):
circle=3.14*d**2
rec=a*b
tri=0.5*a*b
return circle,rec,tri
x=int(input("Enter length:"))
y=int(input("Enter breadth:"))
z=int(input("Enter radius:"))
c,r,t=area(x,y,z)
print("circle:",c,"\t Rectangle:",r,"\t triangle:",t)
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

Q23 WAP to pass a dictionary to function with list of elements as keys


and frequency of its occurrence as value and return a dictionary.
CODE:-
d=dict()
def dic(a):
for i in a:
x=a.count(i)
d[i]=x
return d
x=eval(input("Enter a list:"))
print(dic(x))
OUTPUT:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

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:-

SARTHAK GUPTA XII-S 42 /CS Practical File


Happy School/2023-2024

SARTHAK GUPTA XII-S 42 /CS Practical File

You might also like