GOVERNMENT PU COLLEGE, POLICE COLONY
1. Write a python program using a function to print fibonacci series up to n
numbers
def fibo(n):
n1=0
n2=1
f=n2
count=2
print(n1," ",n2,end=" ")
while count<n:
print(f,end=" ")
count+=1
n1,n2=n2,f
f=n1+n2
n=int(input("enter the limit:"))
fibo(n)
print()
CS Department(Bhagya.V) Page 1
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 2
GOVERNMENT PU COLLEGE, POLICE COLONY
A2. Write a Menu driven program in python to find factorial, and sum of
natural Numbers using a function
def fact(n):
if(n==1 or n==0):
return 1
else:
return n*fact(n-1)
def sums(n):
if(n==0):
return 0
else:
return n+sums(n-1)
print("factorial=1,sum of natural no=2")
opt=int(input("enter your option="))
if(opt==1):
num=int(input("enter your number="))
print("factorial=",fact(num))
elif(opt==2):
num=int(input("enter your number="))
print("sum of natural no=",sums(num))
else:
print("invalid option")
CS Department(Bhagya.V) Page 3
GOVERNMENT PU COLLEGE, POLICE COLONY
output
CS Department(Bhagya.V) Page 4
GOVERNMENT PU COLLEGE, POLICE COLONY
A3. Write a python program using user defined function to calculate interest
amount using simple interest method and compound interest method and
find the difference of interest amount between the two methods
def simpleint(p,t,r):
si=(p*t*r/100)
return si
def compint(p,t,r):
ci=(p*(1+r/100)**t-1)
return ci
p=float(input("enter principle amount="))
t=float(input("enter time amount="))
r=float(input("enter rate amount="))
si=simpleint(p,t,r)
ci=compint(p,t,r)
print("simple interest=",si)
print("compound intertest=",ci)
diff=ci-si
print("differece=",diff)
CS Department(Bhagya.V) Page 5
GOVERNMENT PU COLLEGE, POLICE COLONY
output
CS Department(Bhagya.V) Page 6
GOVERNMENT PU COLLEGE, POLICE COLONY
A4. Write a Python Program to read a text file and display the number of
vowels, consonants, uppercase and lowercase characters in the file
def createfile(puc,words):
with open(puc,"w")as file:
[Link](words)
def countwords(puc):
vowels="aeiouAEIOU"
V=0
C=0
U=0
L=0
with open(puc,"r")as file:
text=[Link]()
for char in text:
if [Link]():
if char in vowels:
V+=1
else:
C+=1
if [Link]():
U+=1
elif [Link]():
L+=1
print("vowels=",V)
print("consonants=",C)
CS Department(Bhagya.V) Page 7
GOVERNMENT PU COLLEGE, POLICE COLONY
print("uppercase=",U)
print("lowercase=",L)
puc="[Link]"
words=input("enter your text=")
createfile(puc,words)
countwords(puc)
output
CS Department(Bhagya.V) Page 8
GOVERNMENT PU COLLEGE, POLICE COLONY
A5) Write a python code to count the number of lines, number of words and
number of characters in a text file.
def countfile(f):
l=0
w=0
c=0
with open(f,"r") as file:
for line in file:
l+=1
w+=len([Link]())
c+=len(line)
print("lines=",l)
print("words=",w)
print("char=",c)
def createfile(f,content):
with open(f,"w")as file:
[Link](content)
f="[Link]"
content="""fgncgb b,
xf hjhj ghfg chfg.
xcb nngff fghfg gnfg
vbvbc bgbg"""
createfile(f,content)
countfile(f)
CS Department(Bhagya.V) Page 9
GOVERNMENT PU COLLEGE, POLICE COLONY
output
CS Department(Bhagya.V) Page 10
GOVERNMENT PU COLLEGE, POLICE COLONY
A6) Write a python program to create and to read records in binary file
with student name and marks of six subjects.
import pickle
while True:
opt=int(input("enter your choice::"))
if opt==1:
f=open('[Link]','wb')
n=int(input("how many studentto register:"))
for i in range(n):
name=input('name:')
english=int(input('english marks'))
kannada=int(input('kannada marks'))
cs=int(input('cs marks'))
acc=int(input('acc marks'))
bus=int(input('bus marks'))
eco=int(input('eco'))
t=[name,english,kannada,cs,acc,bus,eco]
[Link](t,f)
[Link]()
elif opt == 2:
f=open('[Link]','rb')
try:
while True:
p=[Link](f)
print(p)
CS Department(Bhagya.V) Page 11
GOVERNMENT PU COLLEGE, POLICE COLONY
except:
[Link]()
if opt>2:
break
Output:
CS Department(Bhagya.V) Page 12
GOVERNMENT PU COLLEGE, POLICE COLONY
A7. Write a python program to copy the records of the students having
percentage 90 and above from the binary file into another file.
import pickle
while True:
opt=int(input("enter your choice::"))
if opt==1:
f=open('[Link]','wb')
o=open('[Link]','wb')
n=int(input("how many studentto register:"))
for i in range(n):
name=input('name:')
english=int(input('english marks'))
kannada=int(input('kannada marks'))
cs=int(input('cs marks'))
acc=int(input('acc marks'))
bus=int(input('bus marks'))
eco=int(input('eco'))
total=english+kannada+cs+acc+bus+eco
per=(total/600)*100
t=[name,english,kannada,cs,acc,bus,eco]
g=[name,english,kannada,cs,acc,bus,eco]
[Link](t,f)
if per>90:
[Link](g,o)
[Link]()
CS Department(Bhagya.V) Page 13
GOVERNMENT PU COLLEGE, POLICE COLONY
[Link]()
elif opt == 2:
f=open('[Link]','rb')
try:
while True:
p=[Link](f)
print(p)
except:
[Link]()
elif opt == 3:
o=open('[Link]','rb')
try:
while True:
p=[Link](o)
print(p)
except:
[Link]()
if opt>3:
break
CS Department(Bhagya.V) Page 14
GOVERNMENT PU COLLEGE, POLICE COLONY
Output:
CS Department(Bhagya.V) Page 15
GOVERNMENT PU COLLEGE, POLICE COLONY
A8. Write a python program using function to sort the elements of a list
using bubble sort method
def bubble(a):
n=len(a)
for i in range(n):
for j in range(0,n-i-1):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
def input1():
a=[]
n=int(input("enter your number:"))
for i in range(n):
ele=int(input("enter the element:"))
[Link](ele)
return a
a=input1()
print("before sorting elements:",a)
bubble(a)
print("after sorting elements:",a)
CS Department(Bhagya.V) Page 16
GOVERNMENT PU COLLEGE, POLICE COLONY
CS Department(Bhagya.V) Page 17
GOVERNMENT PU COLLEGE, POLICE COLONY
A9. Write a python program using function to sort the elements of a list
using selection sort method
def selection(a):
n=len(a)
for i in range(n):
mini=i
for j in range(i+1,n):
if a[j]<a[mini]:
mini=j
a[i],a[mini]=a[mini],a[i]
def input1():
a=[]
n=int(input("enter your number:"))
for i in range(n):
ele=int(input("enter the element:"))
[Link](ele)
return a
a=input1()
print("before sorting elements:",a)
selection(a)
print("after sorting elements:",a)
CS Department(Bhagya.V) Page 18
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 19
GOVERNMENT PU COLLEGE, POLICE COLONY
A10. Write a python program using function to sort the elements of a list
using insertion sort method
def insertion(a):
n=len(a)
for i in range(1,n):
key=a[i]
j=i-1
while j>=0 and key<a[j]:
a[j+1]=a[j]
j-=1
a[j+1]=key
def input1():
a=[]
n=int(input("enter your number:"))
for i in range(n):
ele=int(input("enter the element:"))
[Link](ele)
return a
a=input1()
print("before sorting elements:",a)
insertion(a)
print("after sorting elements:",a)
CS Department(Bhagya.V) Page 20
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 21
GOVERNMENT PU COLLEGE, POLICE COLONY
A11. Write a python program using function to search an element in a list
using linear search method
def linear(a, se):
for index, ele in enumerate(a):
if ele == se:
return index+1
return -1
def input1():
a = []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
ele = int(input("Enter element "))
[Link](ele)
return a
a = input1()
se = int(input("Enter the element to search for: "))
result = linear(a,se)
if result != -1:
print(se, " Element found at index ", result)
else:
print(se, " Element not found in the list.")
CS Department(Bhagya.V) Page 22
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 23
GOVERNMENT PU COLLEGE, POLICE COLONY
A12. Write a python program using function to search an element in a list
using binary search method.
def binary(a,se):
l,h=0, len(a)-1
while l<= h:
mid = (l+ h) // 2
if a[mid] == se:
return mid+1
elif a[mid] < se:
l= mid + 1
else:
h= mid – 1
return -1
def input1():
a = []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
ele = int(input("Enter the elements in ascending order "))
[Link](ele)
return a
a= input1()
se = int(input("Enter the element to search for: "))
result = binary(a,se)
if result != -1:
print(se, " is found at index ", result)
else:
CS Department(Bhagya.V) Page 24
GOVERNMENT PU COLLEGE, POLICE COLONY
print(se, " is not found in the list.")
Output
CS Department(Bhagya.V) Page 25
GOVERNMENT PU COLLEGE, POLICE COLONY
A13. Write a python program to add and display elements from a stack
using list
stack = []
print("Initially stack is empty :",stack)
[Link]('x')
[Link]('y')
[Link]('z')
print("After PUSHING stack is :")
print(stack)
print('Elements POPped from stack: ')
print([Link]())
print([Link]())
print([Link]())
print('\n My stack after elements are popped:')
print(stack)
CS Department(Bhagya.V) Page 26
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 27
GOVERNMENT PU COLLEGE, POLICE COLONY
A14. Write a python program to add and display elements from a queue
using list
import queue
def display_queue(q):
print("Queue elements are:", end=" ")
while not [Link]():
element = [Link]()
print(element, end=" ")
print('\n Queue size after REMOVE is ', [Link]())
q = [Link]()
[Link](10)
[Link](20)
[Link](30)
print('Queue size after INSERT is ', [Link]())
display_queue(q)
CS Department(Bhagya.V) Page 28
GOVERNMENT PU COLLEGE, POLICE COLONY
Output
CS Department(Bhagya.V) Page 29