12CS-083-Practical_ File(2023-24)
12CS-083-Practical_ File(2023-24)
12CS-083-Practical_ File(2023-24)
OUTPUT:
2. Write a Python program to check the given number is Armstrong number or not.
OUTPUT:
AIM:
To write a Python program to check the given number is Armstrong number or not.
PROGRAM CODE:
3. Write a Python program to find the Fibonacci series of the given number.
OUTPUT:
AIM:
To write a Python program to find the Fibonacci series of the given number.
PROGRAM CODE:
AIM:
To Write a menu driven python program to perform following using functions –
listchange , Lshift and Rshift.
PROGRAM CODE:
print(" 1.Listchange()\n 2.Lshift() \n 3.RShift() \n 4.Exit")
def listchange():
L=eval(input("Enter the list:"))
for i in range(len(L)):
if L[i]%5==0:
L[i]=10
else:
L[i]=L[i]*5
print(L)
def Lshift():
L=eval(input("Enter the list:"))
n=int(input("Enter the no of times to shift left side:"))
L=L[n:]+L[:n]
print(L)
def Rshift():
L=eval(input("Enter the list:"))
n=int(input("Enter the no of times to shift right side:"))
L=L[-n:]+L[:-n]
print(L)
OUTPUT:
while True:
ch=int(input("Enter your choice:"))
if ch==1:
listchange()
elif ch==2:
Lshift()
elif ch==3:
Rshift()
elif ch==4:
break
5. Write a function that takes a number ‘n’ and then returns a randomly generated
number having exactly ‘n’ digits.
e-g:
Enter the no.of.digits: 3
3 digit number is :767
OUTPUT:
AIM:
To write a function that takes a number ‘n’ and then returns a randomly generated
number having exactly ‘n’ digits.
PROGRAM CODE:
import random
def random_n_digits(x):
if x>0:
rno=random.randint(10**(x-1),10**x)
print("The randomly generated",x,"digit no is:",rno)
else:
print("Invalid input")
6.Write a Python script to read a file line by line and print it.
TEXT FILE:
OUTPUT:
AIM:
To write a Python script to read a file line by line and print it.
PROGRAM CODE:
7.Write a python script to remove all the lines that contain the character 'a' in a file
and write it to another file.
TEXT FILE BEFORE EXECUTION:
OUTPUT:
TEXT FILES AFTER EXECUTION:
Ex.No:7
REMOVE THE LINES- CONTAINS 'A'- WRITE INTO ANOTHER
FILE
AIM:
To write a python script to remove all the lines that contain the character 'a' in a file
and write it to another file.
PROGRAM CODE:
import os
fin=open(r'E:\XII Python\Report File\source.txt','r')
fout=open(r'E:\XII Python\Report File\target.txt','w')
temp=open(r'E:\XII Python\Report File\t.txt','w')
line=fin.readlines()
for l in line:
if 'a' in l:
fout.writelines(l)
print("Removed all the lines contains 'a' from this file and write into another file")
else:
temp.writelines(l)
fin.close()
fout.close()
temp.close()
os.remove('E:\\XII Python\\Report File\\source.txt')
os.rename('E:\\XII Python\\Report File\\t.txt','E:\\XII Python\\Report File\\source.txt')
8.Write a Python script to read a text file line by line and display each word
separated by a #.
OUTPUT:
Ex.No:8
READ A TEXT FILE – DISPLAY EACH WORD SEPARATED BY A #
AIM:
To Write a Python script to read a text file line by line and display each word
separated by a #.
PROGRAM CODE:
fh=open("poem.txt","r")
data=fh.read().split()
for i in data:
print(i+str('#'),end='')
fh.close()
9.Write a Python program to Read a text file and display the number of vowels/
consonants/ uppercase/ lowercase characters in the file.
OUTPUT:
Ex.No:9
TEXT FILE - NO OF VOWELS / CONSONANTS/ UPPER / LOWER CASE
AIM:
To write a Python program to Read a text file and display the number of
vowels/ consonants/ uppercase/ lowercase characters in the file.
PROGRAM CODE:
fh=open("poem.txt","r")
data=fh.read()
vowels=['a','e','i','o','u','A','E','I','O','U']
V=C=U=L=0
for i in data:
if i in vowels:
V+=1
if i not in vowels:
C+=1
if i.isupper():
U+=1
if i.islower():
L+=1
print("No.of.Vowels:",V)
print("No.of.Consonants:",C)
print("No.of.Upper Case:",U)
print("No.of.Lower Case:",L)
fh.close()
10.Write a Python script to create a binary file with name and roll number. Search
for a given roll number and display the name, if not found display appropriate
message.
Ex.No:10
BINARY FILE - SEARCH
AIM:
To write a Python script to create a binary file with name and roll number. Search
for a given roll number and display the name, if not found display appropriate message.
PROGRAM CODE:
import pickle
def insert():
fh=open("stu.dat","wb")
L=[]
ch='y'
while ch=='y' or ch=='Y':
rno=int(input("Enter the student rollno:"))
name=input("Enter the student name:")
cs_m=int(input("Enter the student CS mark:"))
L=[rno,name,cs_m]
pickle.dump(L,fh)
ch=input("Do you want to continue?Press y/n:")
fh.close()
def search():
fh=open("stu.dat","rb")
rno=int(input("Enter the roll no to be search:"))
found=0
try:
while True and found==0:
r=pickle.load(fh)
if r[0]==rno:
print("Record Found")
print(r)
BVM GLOBAL – BHS
23
OUTPUT:
found+=1
except EOFError:
print("Record not found")
fh.close()
insert()
search()
11.Write a Python program to Create a binary file with roll number, name and
marks. Input a roll number and update the marks.
Ex.No:11
BINARY FILE - UPDATE
AIM:
To write a Python program to Create a binary file with roll number, name and
marks. Input a roll number and update the marks.
PROGRAM CODE:
import pickle
def write():
fh=open("Binaryfile","wb")
while True:
rno=int(input("Enter the roll no:"))
name=input("Enter the student name:")
per=int(input("Enter the percentage:"))
L=[rno,name,per]
pickle.dump(L,fh)
ch=input("more inputs press y/n:")
if ch=='n' or ch=='N':
break
fh.close()
def read():
f=open("Binaryfile","rb")
try:
while True:
r=pickle.load(f)
print(r)
except EOFError:
f.close()
BVM GLOBAL – BHS
27
OUTPUT:
def update():
with open("Binaryfile","r+b") as f:
a=int(input("Enter the roll no to be updated:"))
found=0
pos=0
try:
while found==0:
r=pickle.load(f)
if r[0]==a:
f.seek(pos)
per=int(input("Enter the new percentage:"))
r[2]=per
pickle.dump(r,f)
found+=1
else:
pos=f.tell()
except EOFError:
print("Record not found")
write()
read()
update()
read()
OUTPUT:
Ex.No:12
CSV FILE - WRITE
AIM:
To write a Python program to create a emp.csv file with employee number,
employee name and salary.
PROGRAM CODE:
import csv
fh=open("empw.csv","w")
s=csv.writer(fh,lineterminator="\n")
n=int(input("Enter the no.of.records:"))
header=["Eno","Ename","Salary"]
s.writerow(header)
for i in range(n):
eno=int(input("Enter the employee number:"))
ename=input("Enter the employee name:")
salary=int(input("Enter the employee salary:"))
r=[eno,ename,salary]
s.writerow(r)
print("File is created")
fh.close()
13.Write a Python function csvcopy() to take sourcefile , target file as parameter and
create a target file and copy the content of sourcefile to target file , if employee
salary is more than 3000.
OUTPUT:
Ex.No:13
CSV FILE - COPY
AIM:
To write a Python function csvcopy() to take sourcefile , target file as parameter
and create a target file and copy the content of sourcefile to target file , if employee salary
is more than 3000.
PROGRAM CODE:
import csv
def csvcopy(sf,tf):
f1=open(sf,"r")
f2=open(tf,"w")
ro_f1=csv.reader(f1)
wo_f2=csv.writer(f2,lineterminator="\n")
for r in ro_f1:
if int(r[2])>3000:
wo_f2.writerow(r)
print("File copied sucessfully")
f1.close()
f2.close()
source_file=input("Enter the source file with extension(.csv):")
target_file=input("Enter the target file with extension(.csv):")
csvcopy(source_file,target_file)
14.Write a menu driven Python program to implement a Stack for these Book-
Details(Book No, Book Name).
PROGRAM CODE:
def isempty(stackf):
if stackf==[]:
return True
else:
return False
def push(stackf,item,top):
stackf.append(item)
top=len(stackf)-1
def pop(stackf):
if isempty(stackf):
print("Stack Empty")
else:
print("Deleted element is:",stackf.pop())
def display(stackf):
if isempty(stackf):
print("Stack Empty")
else:
for a in stack[::-1]:
print(a)
stack=[]
top=None
OUTPUT:
while True:
print("STACK OPERATIONS")
print("1.PUSH")
print("2.POP")
print("3.DISPLAY STACK")
print("4.EXIT")
ch=int(input("Enter your choice(1-4):"))
if ch==1:
bno=int(input("Enter Book.No to be inserted:"))
bname=input("Enter Book Name to be inserted:")
item=[bno,bname]
push(stack,item,top)
input()
elif ch==2:
pop(stack)
elif ch==3:
display(stack)
input()
elif ch==4:
break
else:
print("Invalid Choice!!!")
input()
15.Write a menu driven python program to perform stack operation to insert list of
integers that are divisible by 5.
PROGRAM CODE:
def push(L1):
for i in L1:
if i%5==0:
stack.append(i)
def pop():
return stack.pop()
def display():
if stack==[]:
print("Stack is empty")
else:
for i in stack[::=1]:
print(i)
OUTPUT:
stack=[]
print(" 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT")
while True:
ch=int(input("Enter your choice:"))
if ch==1:
L=eval(input("Enter the list:"))
push(L)
elif ch==2:
if stack==[]:
print("Stack is empty")
else:
print("Popped value:",pop())
elif ch==3:
display()
elif ch==4:
break