Practical 2024 25
Practical 2024 25
#3 AIM: Write a Program to enter the number and print the Floyd’s Triangle in decreasing
order.
Solution:
#Floyd's triangle
n=int(input("Enter the number :"))
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print (j,end=' ')
print('\n')
#4 AIM- Write a program to search an element in a list and display the frequency of element
present in list and their location using List.
OUTPUT
#5 AIM- Write a program to input n numbers in tuple and pass it to function to count how
many even and odd numbers are entered
OUTPUT
#6. AIM: Write a Python Program to count and display the number of Alphabets/ Digits/
uppercase / lowercase characters by using String functions.
def pro():
C1=C2=C3=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)
#7 AIM- Write a program to generator that generates random numbers between 1 and 6
using user defined function
OUTPUT
#8 AIM:Write the PYTHON program to read name of your friends to process PUSH and
POP operation by using STACK Data structure.
def push():
while 1:
m=input("enter your friend name")
n.append(m)
ch=input("do you continue yes(y) or no (n)")
if ch=="n":
break
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 2.TO DISPLAY 3.TO DELETE 4.TO EXIT'''))
if CH==1:
push()
if CH==2:
disp()
if CH==3:
pop()
if CH==4:
break
#9. Aim :Read a text file XII.txt line by line and display each word separated by a # and also
ends with ‘#’.
def write_1():
fb=open('c:\\Users\\Admin\\xii.txt','w')
q=input('enter the line')
fb.write(q)
fb.close()
def read_1():
fb=open('c:\\Users\\Admin\\xii.txt','r')
while 1:
l=fb.readline()
if not l:
break
l=l.split()
for i in l:
print(i,end="#")
fb.close()
write_1()
read_1()
#10 Aim:.Read the lines that contain the character 'a' in a file A.txt and write it to another file
B.txt.
def cs():
fb=open('c:\\Users\\Admin\\A.txt','w')
A=input('enter your line')
fb.write(A)
fb.close()
def com():
fb=open('c:\\Users\\Admin\\A.txt','r')
fb1=open('c:\\Users\\Admin\\B.txt','w')
while 1:
l=fb.readline()
if not l:
break
f=0
for i in l:
if i=="a" or i=="A":
fb1.write(l)
print('the data added successfully')
f=1
break
if f==0:
print('the charecter a or A is not in the line')
fb.close()
fb1.close()
cs()
com()
#11. Aim: 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 adddata():
f=open('d:\\remarks.txt','w' )
s=input(‘enter your line’)
f.write(s)
f.close()
def count_a():
f=open('d:\\remarks.txt','r')
c=c1=0
while 1:
l=f.readline()
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)
adddata()
count_a()
def countrec():
f=open('employee.dat','rb')
s=[]
try:
while 1:
a=[]
a=pickle.load(f)
s.append(a)
except EOFError:
pass
for i in s:
if i[2]>20000:
print(i)
f.close()
createrec()
countrec()
#13. Aim: 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))
#14 Aim.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()
#15. Aim: 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()