12CS-083-Practical_ File(2023-24)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

1

1. Write a menu driven python program to generate the simple calculator.

OUTPUT:

BVM GLOBAL – BHS


2

Ex.No:1 SIMPLE CALCULATOR


AIM:
To write a menu driven python program to generate the simple calculator.
PROGRAM CODE:
print("Simple Calculator")
print(" 1.+\n 2.-\n 3.*\n 4./\n 5.//\n 6.%\n 7.**")
c='y'
while c=='y' or c=='Y':
ch=int(input("Enter your choice:"))
n1=int(input("Enter the no1:"))
n2=int(input("Enter the no2:"))
if ch==1:
print("Addition=",n1+n2)
elif ch==2:
print("Subtraction=",n1-n2)
elif ch==3:
print("Multiplication=",n1*n2)
elif ch==4:
print("Division=",n1/n2)
elif ch==5:
print("Floor Division=",n1//n2)
elif ch==6:
print("Modulus=",n1%n2)
elif ch==7:
print("Exponent=",n1**n2)
else:
print("Invalid Choice")
c=input("Do you want to continue? Press y/n:")

BVM GLOBAL – BHS


3

2. Write a Python program to check the given number is Armstrong number or not.

OUTPUT:

BVM GLOBAL – BHS


4

Ex.No:2 ARMSTRONG NUMBER OR NOT

AIM:
To write a Python program to check the given number is Armstrong number or not.

PROGRAM CODE:

no=int(input("Enter the number:"))


temp=no
s=0
while no>0:
r=no%10
s+=(r*r*r)
no=no//10
if s==temp:
print("The given number",temp,"is an armstrong number")
else:
print("The given number",temp,"is not an armstrong number")

BVM GLOBAL – BHS


5

3. Write a Python program to find the Fibonacci series of the given number.

OUTPUT:

BVM GLOBAL – BHS


6

Ex.No:3 FIBONACCI SERIES

AIM:
To write a Python program to find the Fibonacci series of the given number.

PROGRAM CODE:

no=int(input("Enter the number:"))


c=0
a=-1
b=1
print("The fibonacci series:\n")
for i in range(1,no+1):
c=a+b
print(c)
a=b
b=c

BVM GLOBAL – BHS


7

4. Write a menu driven python program to perform following using functions.


i) Listchange( ) – accepts the list from user , if the element of the list divisible by 5
replace 10 or else multiply by 5.
ii) Lshift( ) - accepts a list List of numbers and n is a numeric value by which all
elements of the list are shifted to left.
iii) Rshift( ) - accepts a list List of numbers and n is a numeric value by which all
elements of the list are shifted to right.

BVM GLOBAL – BHS


8

Ex.No:4 LIST MANIPULATION – USING FUNCTIONS

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)

BVM GLOBAL – BHS


9

OUTPUT:

BVM GLOBAL – BHS


10

while True:
ch=int(input("Enter your choice:"))
if ch==1:
listchange()
elif ch==2:
Lshift()
elif ch==3:
Rshift()
elif ch==4:
break

BVM GLOBAL – BHS


11

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:

BVM GLOBAL – BHS


12

Ex.No:5 RANDOMLY GENERATED ‘N’ DIGIT NUMBER

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

n=int(input("Enter the number of digits:"))


random_n_digits(n)

BVM GLOBAL – BHS


13

6.Write a Python script to read a file line by line and print it.

TEXT FILE:

OUTPUT:

BVM GLOBAL – BHS


14

Ex.No:6 READ A FILE – LINE BY LINE AND PRINT

AIM:
To write a Python script to read a file line by line and print it.

PROGRAM CODE:

myfile=open(r'E:\XII Python\Report File\myfile.txt','r')


line=myfile.readlines()
lno=1
for l in line:
print("Line",lno,l)
lno+=1
myfile.close()

BVM GLOBAL – BHS


15

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:

BVM GLOBAL – BHS


16

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

BVM GLOBAL – BHS


17

8.Write a Python script to read a text file line by line and display each word
separated by a #.

OUTPUT:

BVM GLOBAL – BHS


18

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

BVM GLOBAL – BHS


19

9.Write a Python program to Read a text file and display the number of vowels/
consonants/ uppercase/ lowercase characters in the file.

OUTPUT:

BVM GLOBAL – BHS


20

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

BVM GLOBAL – BHS


21

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.

BVM GLOBAL – BHS


22

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:

BVM GLOBAL – BHS


24

found+=1
except EOFError:
print("Record not found")
fh.close()
insert()
search()

BVM GLOBAL – BHS


25

11.Write a Python program to Create a binary file with roll number, name and
marks. Input a roll number and update the marks.

BVM GLOBAL – BHS


26

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:

BVM GLOBAL – BHS


28

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

BVM GLOBAL – BHS


29

12.Write a Python program to create a emp.csv file with employee number ,


employee name and salary.

OUTPUT:

BVM GLOBAL – BHS


30

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

BVM GLOBAL – BHS


31

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:

BVM GLOBAL – BHS


32

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)

BVM GLOBAL – BHS


33

14.Write a menu driven Python program to implement a Stack for these Book-
Details(Book No, Book Name).

BVM GLOBAL – BHS


34

Ex.No:14 STACK – BOOK DETAILS


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

BVM GLOBAL – BHS


35

OUTPUT:

BVM GLOBAL – BHS


36

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

BVM GLOBAL – BHS


37

15.Write a menu driven python program to perform stack operation to insert list of
integers that are divisible by 5.

BVM GLOBAL – BHS


38

Ex.No.15 STACK -DIVISIBLE BY 5


AIM:
To 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)

BVM GLOBAL – BHS


39

OUTPUT:

BVM GLOBAL – BHS


40

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

BVM GLOBAL – BHS

You might also like