Class 12 Practical Programs 1-14
Class 12 Practical Programs 1-14
Aim:
To write a Python program to print Fibonacci Series for given n number of
terms using function
Program Coding:
def Fibonacci(n):
t1 = -1
t2 = 1
for i in range(n):
t3 = t1+ t2
print(t3,end = " ")
t1 = t2
t2 = t3
print("Fibonacci Series")
n = int(input("Enter the number of terms you want to
print: "))
Fibonacci(n)
Result: Thus the program to print the Fibonacci series has been
executed successfully and the output is verified.
Ex.no: 02 Palindrome
Problem:
Write a Python program to check whether the given string
is a palindrome or not.
Aim:
To write a Python program to check whether the given string is a
palindrome or not.
Program Coding:
def Palindrome(L):
for i in (0,L//2):
if S[i] != S[L-i-1]:
print("The given string",S," is not a Palindrome")
break
else:
print("The given string",S," is a Palindrome")
Result: Thus the program to print the largest and the smallest c o u
nt r y name using function has been executed successfully and the
output is verified.
Aim:
To write a Python program to develop a menu driven program to
calculate the area of different shapes using functions.
Program Coding:
def Area(Choice):
if Choice == 1:
side = int(input('Enter your side value: '))
area_sq = side * side
print('Area of a square = ',area_sq)
elif Choice == 2:
length = int(input('Enter the Length value: '))
breadth = int(input('Enter the Breadth value: '))
area_rec = length * breadth
print('Area of a rectangle = ',area_rec)
elif Choice == 3:
base = int(input('Enter the base value: '))
height = int(input('Enter the height value: '))
area_tri = 0.5 * base * height
print('Area of a triangle = ',area_tri)
elif Choice == 4:
exit
ch='y'
while ch=='y' or ch=='Y':
Output:
Menu Driven Program to Compute the Area of different
shapes
1. Area of a square
2. Area of a rectangle
3. Area of a triangle
4. Exit
Enter your choice between
1 to 4: 1
Enter your side value: 10
Area of a square = 100
Do you want to continue (y/n) : y
Menu Driven Program to Compute the Area of different shapes
1. Area of a square
2. Area of a rectangle
3. Area of a triangle
4. Exit
Enter your choice between 1 to 4: 2
Enter the Length value: 20
Enter the
Breadth
value: 30
Area of a
rectangle =
600
Do you want to continue (y/n) : y
1. Area of a square
2. Area of a rectangle
3. Area of a triangle
4. Exit
Enter your choice
between 1 to 4: 56
Wrong Choice
Do you want to continue (y/n) : n
Program Coding:
myDict = {}
def CreateDict():
for i in range(5):
rno = int(input('Enter the rollno: '))
name = input('Enter the name: ')
myDict[rno] = name
print(myDict)
def Search(n):
found = 0
for i in myDict:
if i == n:
found = 1
print("The element is found in the dictionary")
CreateDict()
n = int(input('Enter the key to be searched: '))
Search(n)
Output :
import random
Guess = True
while Guess:
n = random.randint(1,6)
userinput = int(input("Enter a number between 1 to 6: "))
if userinput == n:
print("Congratulations!!!,You won the lottery")
else:
print("Sorry, Try again, The lucky number was: ",n)
val = input("Do you want to continue y/n: ")
if val in ['y','Y']:
Guess = True
else:
Guess = False
Output:
Problem:
Write a program to read a text file line by line and
d i s p l a y ’ # ’ a f t er each word.
Aim:
To w r i t e a p r o g r a m t o read a text file, line by line and display each
word separated by ‘#’.
Program Coding :
def addHash():
with open('Mystory.txt') as F:
Lines = F.readlines()
myList = []
for i in Lines:
myList = i.split()
for j in myList:
print(j,"#",end= " ")
addHash()
The content of “Mystory.txt” file:
Python is a Programming language. It is easy to learn.
Output:
Python # is # a # Programming # language. # It # is # easy # to #
learn. #
Result:
The above program has been executed successfully and the output is
verified.
Output :
This is the first line of my file . I want to
this line starts with 't' not 'T' but it is changed into
Uppercase.
Result: The above program has been executed successfully and the
output is verified.
Ex.No.9 Counting the number of occurrences of ‘is’
and ‘ and‘ in a text file
Problem :
Write a program to read a text file and count the number of
occurrences of ‘is’ and ‘and’ in that file.
Aim:
To write a program to read a text file and count the number of
occurrences of ‘is’ and ‘and’ in that file.
Program Coding:
def Counting():
with open('Mystory.txt','r') as rfile:
R = rfile.readlines()
print(R)
iscount=andcount = 0
for line in R:
words = line.split()
for i in words:
if i == 'is':
iscount+=1
elif i=='and' :
andcount+=1
print('The total no. of words with is = ',iscount )
print('The total no. of words with and = ', andcount)
Counting()
Program Coding:
import string
def TextFileCounting():
ucase = 0
lcase = 0
vowels = 0
consonants = 0
digits = 0
special = 0
cspace=0
with open('Mytextfile.txt') as rtextfile:
Read = rtextfile.read()
for ch in Read:
if ch.isalpha():
if ch in ['a','e','i','o','u','A','E','I','O','U']:
vowels+=1
else:
consonants+=1
if ch.isupper():
ucase+=1
elif ch.islower():
lcase+=1
elif ch.isdigit():
digits+=1
elif ch==" ":
cspace=cspace+1
else:
special+=1
print('No of uppercase alphabets = ',ucase)
print('No of lowercase alphabets = ',lcase)
print('No of digits = ',digits)
Output:
No of uppercase alphabets = 5
No of lowercase alphabets = 116
No of digits = 4
No of vowels = 46
No of consonants = 75
No of spaces = 20
No of special characters = 6
Total no. of characters in the file = 151
Result:
The above program has been executed successfully and the output is
verified.
Problem:
Write a program to create a binary file to store member no and
member name. Search the given member no. and display its
associated name else display appropriate error message.
def Search_info():
with open('member.dat','rb') as rbfile:
mno = int(input('Enter the member no. to be
searched:'))
try:
while True:
Member = pickle.load(rbfile)
if Member['MemberNo'] == mno:
print (Member)
break
except EOFError:
print('Member No not found')
Store_binary_info()
Search_info()
Output :
Enter the Member no: 11
Enter the name: Venkat
Do you want to continue y/n :y
Enter the Member no: 12
Enter the name: Mala
Do you want to continue y/n :y
Enter the Member no: 13
Enter the name: Vijay
Do you want to continue y/n :n
Binary File writing is over
Enter the member no. to be searched:13
{'MemberNo': 13, 'Name': 'Vijay'}
Problem:
Write a program to create a menu driven program to write, read,
update and append data on to a binary file.
import pickle
Member = {}
def Writing_Binary():
with open('member.dat','wb') as wbfile:
while True:
Mno = int(input('Enter the member no: '))
def Read_Binary():
with open('member.dat','rb') as rbfile:
try:
while True:
Member = pickle.load(rbfile)
print(Member)
except EOFError:
print('Finished reading binary file')
def Update_Binary():
found = False
with open('member.dat','rb+') as rbfile:
try:
while True:
rpos = rbfile.tell()
Member = pickle.load(rbfile)
if Member['Name'] == 'John':
Member['Name'] = 'Johnson'
rbfile.seek(rpos)
pickle.dump(Member,rbfile)
print(Member)
found = True
break
except EOFError:
if found == False:
print('Data not updated in the file')
else:
print('Data updated successfully')
def Append_Binary():
with open('member.dat','ab') as wbfile:
while True:
Mno = int(input('Enter the member no: '))
name = input('Enter the name: ')
Member['MemberNo'] = Mno
Member['Name'] = name
pickle.dump(Member,wbfile)
ch = input('Do you want to continue y/n :')
if ch == 'n':
print('Binary File writing is over')
break
while True:
print(' ')
print('Menu Driven Programming')
print(' ')
print('1. Create a binary file')
print('2. Read the data from binary file ')
print('3. Update the binary file')
print('4. Append data to the binary file')
print('5. Exit')
ch = int(input('Enter your choice: '))
if ch == 1:
Writing_Binary()
elif ch == 2:
Read_Binary()
elif ch == 3:
Update_Binary()
elif ch == 4:
Append_Binary()
elif ch == 5:
break
Output:
Menu Driven Programming
def read_data():
with open('InputData.csv','r') as F:
Reader = csv.reader(F)
for Data in Reader:
print(Data[0],int(Data[1]),int(Data[1])/5)
write_data()
read_data()
Output:
Enter the student name: Kavitha
Total marks:78
Do you want to continue y/n: y
Enter the student name: Kavin
Total marks:90
Do you want to continue y/n: y
Enter the student name: John
Total marks:44
Do you want to continue y/n: n
kavitha 78 15.6
Kavin 90 18.0
john 44 8.8
Result: The above program has been executed successfully and the
output is verified.
Ex.No.14 Copying the contents of one csv file to another file
Problem:
Write a program to copy the contents of a csv file into another
csv file using different delimiter.
Aim:
To write a program to copy the contents of a csv file into another csv
file using different delimiter.
Program Coding:
import csv
def copy_data():
with open('InputData.csv') as F1:
with open('Infile.csv','w',newline='') as F2:
Read = csv.reader(F1)
write = csv.writer(F2,delimiter= '#')
for line in Read:
write.writerow(line)
def disp_data():
with open('Infile.csv') as F:
Reader = csv.reader(F)
for Data in Reader:
print(Data)
copy_data()
disp_data()
Output:
['100#Hamam#28']
['101#Cinthol#29']
['102#Dove#45']
Result:
The above program has been executed successfully and the output is
verified.