Excellent PU College, Kundapura (Sunnari), Udupi (District)
Computer Science (41)
II PUC Practical Programs (2025-26)
PART – A
1. Write a python program using a function to print fibonacci series up to n numbers
def fibo(n):
n1 = 0
n2 = 1
count = 0
if(n<=0):
print("Invalid Input")
elif(n==1):
print(n1,end=' ')
else:
while count < n:
print(n1,end=' ')
n3=n1+n2
n1,n2 =n2,n3
count=count+1
n = int(input("Enter the Limit:"))
fibo(n)
print( )
2. Write a Menu driven program in python to find factorial, and sum of natural Numbers
using a function.
def fact(n):
return 1 if (n==1 or n==0) else n * fact(n - 1);
def sum(n):
return 0 if (n==0) else n+sum(n-1);
n=int(input("Enter any number : "))
print("1-To find the factorial\n2-To find the sum\n3-Exit")
op=int(input("Enter the option 1-3 : "))
if (op==1):
print("Factorial of ",n,"is : ",fact(n))
elif(op==2):
print("Sum of ",n,"natural number is : ",sum(n))
else:
print(" ")
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
3. 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 simple(principle,time,rate):
si = float(principle*time*rate/100)
return si
def comp(principle,time,rate):
ci = float(principle*((1+rate/100)**time - 1))
return ci
principle = float(input(“Enter amount: ”))
time = float(input(“Enter time: ”))
rate = float(input(“Enter rate: ”))
si=simple(principle,time,rate)
ci=comp(principle,time,rate)
print("Simple interest is Rs. %8.2f "%si)
print("Compound interest is Rs. %8.2f "%ci)
dif=ci-si;
print("Difference is Rs. %8.2f "%dif)
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
4. Write a Python Program to read a text file and display the number of vowels, consonants,
uppercase and lowercase characters in the file.
def count1(name):
vowels = "aeiouAEIOU"
vow = 0
cons = 0
upper = 0
lower=0
with open(name, "r") as file:
f= [Link]( )
for char in f:
if [Link]( ):
if char in vowels:
vow=vow+1
else:
cons=cons+1
if [Link]( ):
upper=upper+1
elif [Link]( ):
lower=lower+1
print("Vowels:" , vow)
print("Consonants: ", cons)
print("Uppercase characters: ",upper)
print("Lowercase characters: ", lower)
def create(name, content):
with open(name, "w") as file:
[Link](content)
name = "[Link]"
content = input("Enter few sentence: ")
create(name,content)
count1(name)
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
[Link] a python code to count the number of lines, number of words and number of
characters in a text file.
def count1(name):
line = 0
word= 0
alpha=0
with open(name, "r") as file:
for char in file:
line= line+1
word= word+len([Link]( ))
alpha= alpha+len(char)
print("Lines: ", line)
print("Words: ", word)
print("Characters: ", alpha)
def create(name, content):
with open(name, "w") as file:
[Link](content)
name = "[Link]"
content = """Hello Students
This is a sample text file.
It contains multiple lines."""
create(name,content)
count1(name)
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
[Link] a python program to create and to read records in binary file with student name and
marks of six subjects.
import pickle
while True:
print("[Link] a Binary File\n [Link] the File\n [Link]")
a=int(input("choose a command:"))
if a==1:
f=open('[Link]','wb')
x=int(input('How many student: '))
for i in range(x):
name=input('Name: ')
eng=int(input('English Mark: '))
lan=int(input("Language Marks"))
phy=int(input('Physics Mark: '))
chem=int(input('Chemistry Mark: '))
maths=int(input('Maths Mark: '))
cs=int(input('CS Mark: '))
t=[name,eng,lan,phy,chem,maths,cs]
[Link](t,f)
[Link]( )
elif a==2:
f=open('[Link]','rb')
try:
while True:
p=[Link](f)
print(p)
except:
[Link]( )
if a>2:
break
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
7. 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:
print("[Link] a Binary File\[Link] the main File\[Link] new file wirh >90\[Link]")
a=int(input('choose a command:'))
if a==1:
f1=open('[Link]','wb')
f2=open('[Link]','wb')
x=int(input('How many student: '))
for i in range(x):
name=input('Name: ')
eng=int(input('English Mark: '))
lan=int(input("Language Mark:s"))
phy=int(input('Physics Mark: '))
chem=int(input('Chemistry Mark: '))
maths=int(input('Maths Mark: '))
cs=int(input('CS Mark: '))
total=phy+chem+cs+maths+eng+lan
per=total / 6
t1=[name,eng,lan,phy,chem,maths,cs,total,per]
t2=[name,eng,lan,phy,chem,maths,cs,total,per]
[Link](t1,f1)
if per>=90:
[Link](t2,f2)
[Link]()
[Link]()
elif a==2:
f1=open('[Link]','rb')
try:
while True:
t1=[Link](f1)
print(t1)
except:
[Link]( )
elif a==3:
print("Studets with > 90 Marks")
f2=open('[Link]','rb')
try:
while True:
t2=[Link](f2)
print(t2)
except:
[Link]( )
else:
break
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
8. 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 list1( ):
a=[]
n = int(input("Enter the number of elements: "))
for i in range(n):
ele = int(input("Enter element: "))
[Link](ele)
return a
a = list1( )
print("Original list:", a)
bubble(a)
print("Sorted list:", a)
9. 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):
index = i
for j in range(i+1, n):
if a[j] < a[index]:
index = j
a[i], a[index] = a[index], a[i]
def list1( ):
a=[]
n = int(input("Enter the number of elements: "))
for i in range(n):
ele = int(input("Enter element: "))
[Link](ele)
return a
a= list1( )
print("Original list:", a)
selection(a)
print("Sorted list:", a)
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
10. Write a python program using function to sort the elements of a list using insertion sort
method.
def insertion(a):
for i in range(1, len(a)):
key = a[i]
j=i-1
while j >= 0 and key < a[j]:
a[j + 1] = a[j]
j = j-1
a[j + 1] = key
def list1( ):
a=[]
n = int(input("Enter the number of elements: "))
for i in range(n):
ele = int(input("Enter element:"))
[Link](ele)
return a
a = list1( )
print("Original list:", a)
insertion(a)
print("Sorted list:", a)
11. Write a python program using function to search an element in a list using linear search
method.
def linear(a, target):
for i in range(0,len(a)):
if a[i] == target:
return i
return -1
def list1( ):
a=[]
n = int(input("Enter the number of elements: "))
for i in range(n):
ele = int(input("Enter element: "))
[Link](ele)
return a
a = list1( )
target= int(input("Enter the element to search: "))
linear1 = linear(a, target)
if linear1 != -1:
print(target, " is found at index ",linear1)
else:
print(target, " is not found")
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
12. Write a python program using function to search an element in a list using binary search
method.
def binary(a, target):
low = 0
high =len(a)-1
while low<= high:
mid = (low + high) // 2
if a[mid] == target:
return mid
elif a[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def list1( ):
a=[]
n = int(input("Enter the number of elements: "))
for i in range(n):
ele = int(input("Enter the element:"))
[Link](ele)
return a
a = list1( )
target = int(input("Enter the element to search for: "))
binary1 = binary(a, target)
if binary1!= -1:
print(target, "is found at index ",binary1 )
else:
print(target, "is not found ")
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)
13. 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 Push operation :")
print(stack)
print('Elements Popped from stack: ')
print([Link]())
print([Link]())
print([Link]())
print('After pop operation:')
print(stack)
14. Write a python program to add and display elements from a queue using list.
import queue
def display(q):
print("Queue elements are:")
while not [Link]():
ele = [Link]()
print(ele, 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(q)
Ramachandra
Department of Computer Science
Excellent PU College,Sunnari
Kundapura,Udupi(District)