Shreyapro
Shreyapro
Class –XII
Session: 2021-22
COMPUTER(083)
REPORT FILE
CLASS : XII
def display_list(alist):
length = len(alist)
for i in range(0,length):
if alist[i]%2!=0:
print(alist[i],'\t',end='')
#_main_
alist=[]
for i in range(0,10):
value=int(input('enter the value:'))
alist.append(value)
display_list(alist)
OUTPUT
OUTPUT
enter a number to checked
13 13 is a prime number
OUTPUT
enter a number 5
factorial of 5 =
120
enter a number 0
factorial of 0 = 1
Q4. Write a Program Using User Defined Function to Create a text
file “Para.txt”. Write another Function to Display each Words
present in the file”Para.txt” , also print number of words present in
the file.
def create_file():
f1=open("para.txt","w")
f1.write("Take umbrella and raincoat\
when it rains\n")
f1.write("We all should enjoy
the\
monsoon")
f1.close()
def read_file():
f1=open("para.txt","r")
data=f1.read()
word=data.split()
count=0
for i in word:
print(i)
count+=1
print('No of words present in the file:',count)
f1.close()
#_main_
create_file()
read_file()
OUTPUT
Take
umbrella
and
raincoat
when
it
rains
We
all
should
enjoy
the
monsoon
No of words present in the file: 13
Q5.Write the functions for the following:
a) Create a file “alphabets.txt”,and write data into it.
b) Read the data from file and count the number of vowels
and consonants present in each word.
def create_file():
f1= open("alphabets.txt","w")
f1.write("Take umbrella and raincoats\
when it rains")
f1.close()
def count():
f1=open("alphabets.txt","r")
data=f1.read()
word=data.split()
print("WORDS\tNO.OF VOWELS\tNO.OF
CONSONANTS")
for i in word:
c_count,v_count=0,0
for j in i:
if j in"AEIOU" or j in"aeiou":
v_count+=1
else:
c_count+=1 print(i,"\
t",v_count,"\t\t",c_count)
f1.close()
#_main_
create_file()
count()
OUTPUT
WORDS NO.OF VOWELS NO.OF CONSONANTS
Take 2 2
umbrella 3 5
and 1 2
raincoats 4 5
when 1 3
it 1 1
rains 2 3
Q6.Write a user defined function to arrange the list in ascending
order by method of bubble sort.
def bubble_sorting(list):
length=len(list)
for i in range(0,length):
for j in range(0,length-i-1):
if list[j]>list[j+1]:
temp=list[j]
list[j]=list[j+1]
list[j+1]=temp
print('SORTED LIST')
print(list)
#_main_
num=[]
N=int(input('ENTER NO. OF ELEMENTS TO\
BE ENTERED IN THE LIST'))
for i in range(0,N):
value=int(input('ENTER VALUE TO
BE\
ENTERED IN THE LIST'))
num.append(value)
bubble_sorting(num)
OUTPUT
ENTER NO. OF ELEMENTS TO BE ENTERED IN THE LIST7
ENTER VALUE TO BE ENTERED IN THE LIST2
ENTER VALUE TO BE ENTERED IN THE LIST256
ENTER VALUE TO BE ENTERED IN THE LIST95
ENTER VALUE TO BE ENTERED IN THE LIST451
ENTER VALUE TO BE ENTERED IN THE LIST95
ENTER VALUE TO BE ENTERED IN THE LIST25
ENTER VALUE TO BE ENTERED IN THE LIST58
SORTED LIST
[2, 25, 58, 95, 95, 256, 451]
Q7.Write a program for insertion sorting.
def insertionsort(arr):
l=len(arr)
for i in range(1,l):
value=arr[i]
j=i-1
while j>=0 and value<arr[j]:
arr[j+1]=arr[j]
j-=1
arr[j+1]=value
#_main_
num=[10,25,75,4,94,28,5]
insertionsort(num)
print('sorted list')
for i in range(0,len(num)):
print(num[i],end=' ')
OUTPUT
sorted list
4 5 10 25 28 75 94
Q8. Write a Program using Function to Create a List of Sorted
Numbers. Enter a Number check that the Number is Present in the
list or not using method of Binary search.
def bin_search(ar,key):
low=0
high=len(ar)-1
while low<=
high:
mid=int((low+high)/2)
if key==ar[mid]:
return mid
elif key<ar[mid]:
high=mid-1
else:
low=mid+1
else:
return -1
#_main_
ar=[]
n=int(input('enter no of elements'))
for i in range (0,n):
value=int(input('enter value'))
ar.append(value)
item=int(input('enter search item:'))
res=bin_search(ar,item)
if res>=0:
print(item,'found at index',res)
else:
print(item,'not found in array')
OUTPUT
enter no of elements5
enter value13
enter value56
enter value65
enter value78
enter value87
enter search item:65
65 found at index 2
enter no of elements5
enter value12
enter value34
enter value45
enter value54
enter value67
enter search item:60
60 not found in array
Q9.Write a user defined function to create a binary file “stud.dat”,
add the records in the file depending on the user’s choice.
import pickle
def writefile():
f1=open("stud.dat","wb")
ch='y'
rec=[]
while ch=='y':
roll=int(input("enter roll no"))
name=input("enter name")
mark=int(input("enter marks"))
rec=[roll,name,mark]
pickle.dump(rec,f1)
ch=input("Do you want to add any \
more record(y/n"))
f1.close()
#_main_
writefile()
OUTPUT
enter roll no1
enter nameAYUSH
enter marks98
Do you want to add any more recordy
enter roll no2
enter nameDEEPAK
enter marks87
Do you want to add any more recordy
enter roll no3
enter nameHARSHIT
enter marks85
Do you want to add any more record (y/n) n
Q10.Write a user defined function to read the content of binary file
“stud.dat” and print the data present in the file.
import pickle
def readfile():
f1=open("stud.dat","rb")
rec=[]
try:
while True:
rec=pickle.load(f1)
print(rec)
except EOFError:
f1.close()
#_main_
readfile()
OUTPUT
[1, 'AYUSH', 98]
[2, 'DEEPAK', 87]
[3, 'HARSHIT', 85]
Q11.Write a user defined function to search and display the record
of a given roll number from binary file “stud.dat”.
import pickle
def search_file():
found=False
f1=open("stud.dat","rb")
rec=[]
sroll=int(input("enter roll no to be \
searched"))
try:
while True:
rec=pickle.load(f1)
if rec[0]==sroll:
print(rec)
found=True
except EOFError:
if found==True:
print("Record found")
else:
print("record not found")
f1.close()
#_main_
search_file()
OUTPUT
import pickle
def read_file():
f1=open("lib.dat","rb")
book=[]
bns=int(input("Enter book no to be\
search"))
found=False
try:
while True:
book=pickle.load(f1)
if bns==book[0]:
print(book)
found=True
except EOFError:
if found==True:
print("Record found")
else:
print("Record not found")
f1.close()
#_main_
read_file()
OUTPUT
Enter book no to be search587
[587, 'MODERN ABC', 1200.0]
Record found
Enter book no to be search290
Record not found
Q14. Write the functions for the following:
a) To add record in the csv file “exam.csv”.
b) To display the record from csv file “exam.csv” if mark
is more than 90.
import csv
def write_file():
with open('EXAM.CSV','w',) as f1:
filewriter=csv.writer(f1)
filewriter.writerow(['Roll','Name',\
'Marks
ch='y'
while ch=='y' or ch=='Y':
roll=int(input('enter roll no'))
name=input('enter name')
marks=int(input('enter marks'))
record=[roll,name,marks]
filewriter.writerow(record)
ch=input('Do you want to add \
anymore record:')
def read_file():
with open('EXAM.CSV','r',newline='\n')\
as f1:
filereader=csv.reader(f1)
next(filereader)
for row in filereader:
if int(row[2])>=90:
print(row)
f1.close()
#_main_
write_file()
read_file()
OUTPUT
enter roll no1
enter nameARPIT
enter marks95
Do you want to add anymore record:y
enter roll no2
enter nameSURAJ
enter marks90
Do you want to add anymore record:N
OUTPUT
[‘1’,’ARPIT’,’95’]
Q16. Program showing,adding,deleting elements according to
user's choice in a stack?
stack=[]
top=-1
def choice_input():
print("enter your choice")
ch=int(input("enter choice"))
return ch
def push_ele(stack,top):
rollno=int(input("enter roll no"))
marks=int(input("enter marks"))
element=(rollno,marks)
stack.append(element)
top=top+1
return top
def show_ele(stack,top):
i=top
while(i>=0):
print(stack[i])
i=i-1
def pop_ele(stack,top):
element=stack.pop()
top=top-1
print("value deleted from stack",element)
return top
while True:
print("1=add a new element")
print("2=display the elements")
print("3=delete an element")
print("4=quit")
choice=choice_input()
if choice==1:
while True:
top=push_ele(stack,top)
print("do u want to add more element")
cho=input("enter choice y/n")
if cho=="n":
break
if choice==2:
show_ele(stack,top)
if choice==3:
top=pop_ele(stack,top)
if choice==4:
break
print("do u want more")
OUTPUT
1=add a new element
2=display the elements
3=delete an element
4=quit
enter your choice
enter choice1
enter roll no25
enter marks94
do u want to add more element
enter choice y/nn
1=add a new element
2=display the elements
3=delete an element
4=quit
enter your choice
enter choice4
do u want more
Q17.Create table club by using interface between python and
mysql.
OUTPUT
Successfully Connected to Database
(1, 'AJAY SWAROOP', 35, 'KARATE', datetime.date(1997, 2, 25),
1000.0, 'M')
(2, 'RAVINA', 34, 'KARATE', datetime.date(1996, 2, 20), 1200.0, 'F')
(3, 'ROHIT', 25, 'SWIMMING', datetime.date(2000, 10, 5), 1500.0,
'M')
(4, 'VIRAT', 32, 'CRICKET', datetime.date(1998, 8, 10), 2000.0, 'M')
(5, 'SINDHU', 30, 'BADMINTON', datetime.date(1995, 12, 14), 1750.0,
'F')
Q19.Write a program to print all records from the table club where
pay>=1500.
OUTPUT
Successfully Connected to Database
(3, 'ROHIT', 25, 'SWIMMING', datetime.date(2000, 10, 5), 1500.0,
'M')
(4, 'VIRAT', 32, 'CRICKET', datetime.date(1998, 8, 10), 2000.0, 'M')
(5, 'SINDHU', 30, 'BADMINTON', datetime.date(1995, 12, 14), 1750.0,
'F')
Q20.Write a program using python and mysql interface to update
record in the table club.
OUTPUT
Successfully Connected to Database
(1, 'AJAY SWAROOP', 35, 'KARATE', datetime.date(1997, 2, 25),
1000.0, 'M')
(2, 'RAVINA', 34, 'KARATE', datetime.date(1996, 2, 20), 1200.0, 'F')
(3, 'ROHIT', 25, 'SWIMMING', datetime.date(2000, 10, 5), 1500.0,
'M')
(5, 'SINDHU', 30, 'BADMINTON', datetime.date(1995, 12, 14), 1750.0,
'F')
(4, 'VIRAT', 32, 'CRICKET', datetime.date(1998, 8, 10), 2000.0, 'M')