Screenshot 2024-01-23 at 6.17.57 AM
Screenshot 2024-01-23 at 6.17.57 AM
Screenshot 2024-01-23 at 6.17.57 AM
1. Write the PYTHON program to read name of your friends to process PUSH
and POP operation by using STACK Data structure.
2. Write the PYTHON program to read a list Num with 5 numbers and search
the particular element in the list then print the position of an element
otherwise display the message “Element not found”.
4. Read a text file XII.txt line by line and display each word separated by a
#
5. Read the lines that contain the character 'a' in a file A.txt and write it to
another file B.txt.
6. Write the PYTHON program to which should read each character of a text
file REMARKS.EXT. Should count and display the occurrence of
alphabets A and E ( including small cases a and e too )
def disp():
if n==[]:
print("stack empty")
else:
print(n)
def pop():
if n==[]:
print("stack empty")
else:
print(n.pop())
n=[]
while 1:
print("choose the option")
CH=int(input('''
1.TO ADD THE NAME
2.TO DISPLAY THE NAME
3.TO DELETE THE NAME
4.TO EXIT'''))
if CH==1:
push()
if CH==2:
disp()
if CH==3:
pop()
if CH==4:
print('BYE BYE SEE YOU SOON ')
break
#2.Write the PYTHON program to read a list Num with 5 numbers and search the particular
element in the list then print the position of an element otherwise display the message “Element
not found”.
def list1():
for i in range(5):
a=int(input("enter the number"))
n.append(a)
def disp():
if n==[]:
print("List empty")
else:
print(n)
def pos(b):
if n==[]:
print("list empty")
for i in range(len(n)):
if n[i]==b:
print("the position of the",b,"is",i)
else:
print("the searched number",b,"is not exist")
n=[]
while 1:
print("choose the option")
CH=int(input('''
1.TO ADD THE NUMBER
2.TO DISPLAY THE NUMBER
3.TO SEARCH THE NUMBER
4.TO EXIT'''))
if CH==1:
list1()
if CH==2:
disp()
if CH==3:
b=int(input("ENTER THE NUMBER TO BE SEARCHED"))
pos(b)
if CH==4:
print('BYE BYE SEE YOU SOON ')
break
#3.Write a Python Program to count and display the number of Alphabets/ Digits/ uppercase /
lowercase characters by using String functions.
def pro():
C1=0
C2=0
C3=0
C4=0
for i in A:
if i.isalpha():
C1+=1
if i.isdigit():
C2+=1
if i.isupper():
C3+=1
if i.islower():
C4+=1
print('THE NO.OF ALPHABETS=',C1)
print('THE NO.OF DIGITS=',C2)
print('THE NO.OF UPPERCASE=',C3)
print('THE NO.OF LOWERCASE=',C4)
#4.Read a text file XII.txt line by line and display each word separated by a #
def read():
fb=open("XII.txt","r")
while 1:
l=fb.readline()
if not l:
break
if len(l)>0:
print(l,sep="#")
fb.close()
read()
#5.Read the lines that contain the character 'a' in a file A.txt and write it to another
file B.txt.
def read():
fb=open("a.txt","r")
fb1=open("b.txt","w")
while 1:
l=fb.readline()
if not l:
break
for i in l:
if i=="a" or i=="A":
fb1.write(i)
fb.close()
fb1.close()
read()
#6.Write the PYTHON program to which should read each character of a text file
REMARKS.TXT.
#Should count and display the occurrence of alphabets A and E ( including small
cases a and e too )
def purple():
f=open('d:\\remarks.txt','w' )
s="Positive thinking will let you do everything better than negative thinking."
f.write(s)
f.close()
def count_a():
f=open('d:\\remarks.txt','r')
c=c1=0
while 1:
l=f.readline()
print('The line is:\n',l)
if not l:
break
for i in l:
if i=='a'or i=='A':
c+=1
if i=='e' or i=='E':
c1+=1
print('No of A or a is:',c)
print('No of E or e is:',c1)
purple()
count_a()
#8. A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
#Write a user defined function CreateFile() to input data for a record and add
to“Book.dat” .
#Write a function countRec (Author) in Python which accepts the Author name
as parameter
#and count and return number of books by the given Author are stored in the
binary file “Book.dat”
import pickle
def createfile():
f=open('book.dat','wb')
s=[]
while 1:
bookno=int(input('enter bookno'))
book_name=input('enter name')
author=input('enter author')
price=int(input('enter price'))
s=[bookno,book_name,author,price]
pickle.dump(s,f)
ch=input('do u continue(y/n)')
if ch=='n':
break
f.close()
def countrec(author):
f=open('book.dat','rb')
s=[]
c=0
try:
while 1:
a=[]
a=pickle.load(f)
s.append(a)
except EOFError:
pass
for i in s:
if i[2]==l:
c+=1
return c
f.close()
createfile()
l=input('enter author name to be seached')
print("Number of books by the Author = ",countrec(l))
#9.Write a python program to search and display the record of the student from
#a CSVfile “Student.csv” containing students records (Rollno, Name and Marks).
#Roll number of the student to be searched will be entered by the user.
import csv
def disp():
f=open('school.csv','w',newline='')
w=csv.writer(f)
s=[]
while 1:
roll_num=int(input('enter any rollno'))
name=input('enter any name')
percentage=int(input('enter any percent'))
s=[roll_num,name,percentage]
w.writerow(s)
ch=input('do you continue(y/n)')
if ch=='n':
break
f.close()
disp()
def countrec():
f=open('school.csv','r')
s=[]
A=int(input('enter the roll no of the student'))
s=csv.reader(f)
for i in s:
if int(i[0]) ==A:
print(i)
f.close()
countrec()
#10. A CSV file “SCHOOL.CSV” has structure [Roll_Num, Name, Percentage]
#Write a function Count_Rec() in Python that would read contents of the file
“SCHOOL.CSV”
#and display the details of those students whose percentage is below 33 .
import csv
def disp():
f=open('school.csv','w',newline='')
w=csv.writer(f)
s=[]
while 1:
roll_num=int(input('enter any rollno'))
name=input('enter any name')
percentage=int(input('enter any percent'))
s=[roll_num,name,percentage]
w.writerow(s)
ch=input('do you continue(y/n)')
if ch=='n':
break
f.close()
disp()
def count_rec():
f=open('school.csv','r',newline='')
s=csv.reader(f)
for i in s:
if int(i[2])<33:
print(i)
f.close()
count_rec()