Class 12 Practical Programs 1-10
Class 12 Practical Programs 1-10
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
country name using function has been executed successfully and the
output is verified.
Ex.no: 04 Menu driven program to calculate area of different
shapes
Problem:
Write a Python program to develop a menu driven program
to calculate the area of different shapes using functions.
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
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
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: 3
Enter the base value: 25
Enter the
height
value: 50
Area of a
triangle =
625.0
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
Result: Thus the program to develop a menu driven program to calculate the
area of different shapes using functions has been executed successfully and
the output is verified.
Ex.no: 05 Search and display- using dictionary
Problem:
Write a Python program to create a dictionary to store roll number
and name of 5 students. Display the corresponding name for the
given Roll number, if it is not found then display appropriate error
message.
Aim:
To write a Python program to create a dictionary to store roll
number and name of 5 students, for a given roll number display
the corresponding name else display appropriate error message.
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:
Result:
The program to generate random numbers between 1 to 6
(stimulate a dice) has been executed successfully and the output is
verified.
Ex. No. 7 Separating words by ‘#’ symbol
Problem:
Write a program to read a text file line by line and display ’#’after each
word.
Aim:
To write a program to 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.