0% found this document useful (0 votes)
5 views18 pages

Python Programs for PUC 2nd Year

Uploaded by

kpparveentaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Python Programs for PUC 2nd Year

Uploaded by

kpparveentaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PUC 2ND YEAR PYTHON

1)Write a python program using a function to print n


Fibonacci numbers.
def fibo(n):
num1 = 0
num2 = 1
next_number=num2
count = 2
print(num1,"",num2,end=" ")
while count <n:
print(next_number,end=" ")
count += 1
num1,num2=num2,next_number
next_number=num1 + num2
n=int(input("enter the Limit:"))
fibo(n)
print( )

SAMPLE-OUTPUT

enter the Limit:11


0 1 1 2 3 5 8 13 21 34 55

enter the Limit:15


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

2) Write a menu driven program in python to find factorial and


sum of natural of n Numbers using 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);
num=int(input("Enter any number:"))
print("1-To find thefactorial,2-To find the sum,3-Exit")
opt=int(input("Enter the option 1-3 : "))
if (opt==1):
print("Factorial of", num, "is:",fact(num))
elif(opt==2):
print("Sum of", num, "is:",sum(num))
else:
print(" ")

SAMPLE-OUTPUT

Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit


(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================== RESTART: C:\Users\ACER\Desktop\BHANDE\[Link] =================


Enter any number:5
1-To find thefactorial,2-To find the sum,3-Exit
Enter the option 1-3 : 1
Factorial of 5 is: 120

================== RESTART: C:\Users\ACER\Desktop\BHANDE\[Link] =================


Enter any number:5
1-To find thefactorial,2-To find the sum,3-Exit
Enter the option 1-3 : 2
Sum of 5 is: 15

================== RESTART: C:\Users\ACER\Desktop\BHANDE\[Link] =================


Enter any number:5
1-To find thefactorial,2-To find the sum,3-Exit
Enter the option 1-3 : 3

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

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 simpint(principle, time, rate):


si=float(principle*time*rate/100)
return si
def compint(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=simpint(principle, time, rate)
ci=compint(principle, time, rate)
print("simple interest is Rs. %8.2f" % si)
print("Compound interest is Rs.%8.2f" % ci)
diffint=ci-si;
print("Difference is Rs. %8.2f" % diffint)

SAMPLE-OUTPUT

Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit


(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================


Enter amount:25000
Enter time: 2
Enter rate: 3
simple interest is Rs. 1500.00
Compound interest is Rs. 1522.50
Difference is Rs. 22.50

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

4)Write a Python Program to create a text file and to read a text file and display the

number of vowels, consonants, uppercase and lowercase characters in the file.

def count_characters(file_name):
vowels = "aeiouAEIOU"
v = c = u = l = 0
with open(file_name, "r") as f:
text = [Link]()
for ch in text:
if [Link]():
if ch in vowels:
v += 1
else:
c += 1
if [Link]():
u += 1
elif [Link]():
l += 1
print("Vowels:", v)
print("Consonants:", c)
print("Uppercase:", u)
print("Lowercase:", l)

def create_text_file(file_name, content):


with open(file_name, "w") as f:
[Link](content)
filename = "[Link]"
content = input("Enter content: ")
create_text_file(filename, content)
count_characters(filename)

SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================


Enter content: Hi, i am science student studying in MDRPUc K 348
Vowels: 12
Consonants: 24
Uppercase: 7
Lowercase: 29

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

5) Write a python code to count the number of lines, number of words and number of
characters in a text file.
def count_text_file(file_name):
line_count = 0
word_count = 0
char_count = 0

with open(file_name, "r") as file:


for line in file:
line_count += 1
word_count += len([Link]())
char_count += len(line)

print("Lines: ", line_count)


print("Words: ", word_count)
print("Characters: ", char_count)

def create_text_file(file_name, content):


with open(file_name, "w") as file:
[Link](content)

file_name = "[Link]"
content = """Hello Students This is a sample text file.
It contains multiple lines."""
create_text_file(file_name, content)

count_text_file(file_name)

SAMPLE-OUTPUT

Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit


(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================


Lines: 2
Words: 12
Characters: 70

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

6) 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:
print("""
1. Create Binary File
2. Display the File
3. Exit
""")

a = int(input("Choose a command (1, 2, 3 to exit): "))

if a == 1:
with open("[Link]", "wb") as f:
x = int(input("How many students: "))
for i in range(x):
name = input("Name: ")
english = 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, english, lan, phy, chem, maths,
cs]
[Link](t, f)

elif a == 2:
try:
with open("[Link]", "rb") as f:
while True:
t = [Link](f)
print(t)
except EOFError:
# This stops the loop when the end of the file
is reached
print("--- End of File ---")
except FileNotFoundError:

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

print("Error: The file '[Link]' does not


exist yet.")

elif a == 3:
print("Exiting program...")
break
else:
print("Invalid choice, please try again.")
SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================

1. Create Binary File


2. Display the File
3. Exit

Choose a command (1, 2, 3 to exit): 1


How many students: 2
Name: SAMBA
English Mark: 89
Language Marks: 78
Physics Mark: 79
Chemistry Mark: 82
Maths Mark: 84
CS Mark: 95
Name: KOTE
English Mark: 88
Language Marks: 95
Physics Mark: 80
Chemistry Mark: 75
Maths Mark: 82
CS Mark: 90

1. Create Binary File


2. Display the File
3. Exit

Choose a command (1, 2, 3 to exit): 2


['SAMBA', 89, 78, 79, 82, 84, 95]
['KOTE', 88, 95, 80, 75, 82, 90]
--- End of File ---

1. Create Binary File


2. Display the File
3. Exit

Choose a command (1, 2, 3 to exit): 3


Exiting program...

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

7)Write a python program to copy the records of the students having percentage 90 and
above from the binary file into another file.
# program to create and copy records to copy records with percentage 90 and above into
another file.

import pickle
while True:
print('''
1. Create Binary File
2. Display the Main File
3. Create/Display students with >90%
4. Exit''')
a = int(input('Choose a command (1-4): '))
if a == 1:
f = open('[Link]', 'wb')
o = open('[Link]', 'wb')
x = int(input('How many students: '))
for i in range(x):
name = input('Name: ')
english = 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: '))
total = phy + chem + cs + maths + english + lan
per = (total / 600) * 100
t = [name, english, lan, phy, chem, maths, cs, total, per]
[Link](t, f)
if per >= 90:
[Link](t, o)
# Close files AFTER the loop finishes
[Link]()
[Link]()
print("Files created successfully.")

elif a == 2:
try:
f = open('[Link]', 'rb')
while True:
p = [Link](f)
print(p)
except EOFError:
[Link]()
except FileNotFoundError:
print("Main file not found.")

elif a == 3:

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

print("Students with > 90% Marks:")


try:
o = open('[Link]', 'rb')
while True:
data = [Link](o)
print(data)
except EOFError:
[Link]()
except FileNotFoundError:
print("No students with >90% found.")

elif a == 4:
break
else:
print("Invalid choice.")

SAMPLE-OUTPUT

Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit


(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================

1. Create Binary File


2. Display the Main File
3. Create/Display students with >90%
4. Exit
Choose a command (1-4): 1
How many students: 2
Name: SINDHU
English Mark: 94
Language Marks: 98
Physics Mark: 84
Chemistry Mark: 82
Maths Mark: 80
CS Mark: 98
Name: ABC
English Mark: 98
Language Marks: 99
Physics Mark: 92
Chemistry Mark: 91
Maths Mark: 87
CS Mark: 98
Files created successfully.

1. Create Binary File


2. Display the Main File
3. Create/Display students with >90%
4. Exit

Choose a command (1-4): 2

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

['SINDHU', 94, 98, 84, 82, 80, 98, 536, 89.33333333333333]


['ABC', 98, 99, 92, 91, 87, 98, 565, 94.16666666666667]

1. Create Binary File


2. Display the Main File
3. Create/Display students with >90%
4. Exit
Choose a command (1-4): 3
Students with > 90% Marks:
['ABC', 98, 99, 92, 91, 87, 98, 565, 94.16666666666667]

1. Create Binary File


2. Display the Main File
3. Create/Display students with >90%
4. Exit
Choose a command (1-4): 4

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

8) Write a python program using function to sort the elements of a list using bubble sort
method
# program using function to sort the elements of list using bubble sort method def

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

def input_list():
arr = []
n = int(input("Enter number of elements: "))
for _ in range(n):
[Link](int(input("Enter element: ")))
return arr

arr = input_list()
print("Original list:", arr)
bubble_sort(arr)
print("Sorted list:", arr)

SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================


Enter number of elements: 6
Enter element: 8
Enter element: 54
Enter element: 0
Enter element: -5
Enter element: 2
Enter element: 98
Original list: [8, 54, 0, -5, 2, 98]
Sorted list: [-5, 0, 2, 8, 54, 98]

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

9)Write a python program using function to sort the elements of a list using selection sort
method

def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]

def input_list():
arr = []
n = int(input("Enter number of elements: "))
for _ in range(n):
[Link](int(input("Enter element: ")))
return arr

arr = input_list()
print("Original list:", arr)
selection_sort(arr)
print("Sorted list:", arr)

SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] =================


Enter number of elements: 6
Enter element: 8
Enter element: 54
Enter element: 0
Enter element: -5
Enter element: 2
Enter element: 98
Original list: [8, 54, 0, -5, 2, 98]
Sorted list: [-5, 0, 2, 8, 54, 98]

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

10) Write a python program using function to sort the elements of a list using insertion
sort method
# python program using function to sort the elements of list using insertion sort method

def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

def input_list():
arr = []
n = int(input("Enter number of elements: "))
for _ in range(n):
[Link](int(input("Enter element: ")))
return arr

arr = input_list()
print("Original list:", arr)
insertion_sort(arr)
print("Sorted list:", arr)
SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] ================


Enter number of elements: 6
Enter element: 5
Enter element: 4
Enter element: 99
Enter element: 85
Enter element: 0
Enter element: 2
Original list: [5, 4, 99, 85, 0, 2]
Sorted list: [0, 2, 4, 5, 85, 99]

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

11)Write a python program using function to search an element in a list using linear
search method
# Search function with parameter list name
# and the value to be searched - Linear Search

def linear_search(arr, target):


for index, element in enumerate(arr):
if element == target:
return index
# FIX: Move this OUTSIDE the for loop
# Only return -1 after checking the ENTIRE list
return -1

def input_list():
arr = []
n = int(input("Enter number of elements: "))
for _ in range(n):
[Link](int(input("Enter element: ")))
# FIX: Move this OUTSIDE the for loop
return arr

# Execution logic
arr = input_list()
target = int(input("Enter the element to search: "))

result = linear_search(arr, target)

if result != -1:
print(target, "found at index", result)
else:
print(target, "not found in the list.")

SAMPLE-OUTPUT
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit
(AMD64)] on win32
Enter "help" below or click "Help" above for more information.

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] ================


Enter number of elements: 5
Enter element: 5
Enter number of elements: 5
Enter element: 2 Enter element: 22
Enter element: 9 Enter element: 33
Enter element: 8 Enter element: 66
Enter element: 45 Enter element: 55
Enter the element to search: 9 Enter element: 897
9 found at index 2 Enter the element to search: 11111
11111 not found in the list.

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

12)Write a python program using function to search an element in a list using binary
search method.
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
# FIX: Move return -1 outside the while loop
return -1

def input_list():
arr = []
n = int(input("Enter number of elements (in ascending order): "))
for _ in range(n):
[Link](int(input("Enter element: ")))
return arr

# FIX: Put these on separate lines


arr = input_list()
target = int(input("Enter the element to search: "))

result = binary_search(arr, target)


if result != -1:
print(target, "found at index", result)
else:
print(target, "not found in the list.")
SAMPLE-OUTPUT
================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] ================
Enter number of elements (in ascending order): 5
Enter element: 89
Enter element: 99
Enter element: 108
Enter element: 208
Enter element: 9999
Enter the element to search: 108
108 found at index 2

Enter number of elements (in ascending order): 5


Enter element: 56
Enter element: 59
Enter element: 58
Enter element: 89
Enter element: 99
Enter the element to search: 100
100 not found in the list.

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

13)Write a python program to add and display elements from a stack using list
# initial empty stack.

stack = []

print("Initially, stack is empty:", stack)

# Push elements
[Link]('x')
[Link]('y')
[Link]('z')

print("After PUSHING stack is:")


print(stack)

# Pop elements
print("After POP from stack:")
print([Link]())
print([Link]())
print([Link]())

print("Stack after popping all elements:")


print(stack)

SAMPLE-OUTPUT

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] ================


Initially, stack is empty: []
After PUSHING stack is:
['x', 'y', 'z']
After POP from stack:
z
y
x
Stack after popping all elements:
[]

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

14)Write a python program to add and display elements from a queue using list
# python program to add and display elements from a queue using list
# Create Queue and perform insert and delete

import queue

def display_queue(q):
print("Queue elements are:", end=" ")
while not [Link]():
print([Link](), end=" ")
print("\nQueue size after REMOVE is", [Link]())

# Create queue and add elements


q = [Link]()
[Link](10)
[Link](20)
[Link](30)
print("Queue size after INSERT is", [Link]())
display_queue(q)

SAMPLE-OUTPUT

================= RESTART: C:\Users\ACER\Desktop\MDRPUCK\[Link] ================


Queue size after INSERT is 3
Queue elements are: 10
Queue size after REMOVE is 2
20
Queue size after REMOVE is 1
30
Queue size after REMOVE is 0

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-


PUC 2ND YEAR PYTHON

MORARJI DESAI RESIDENTIAL PU COLLEGE,KAMTHANA PAGE NO:-

You might also like