PRACTICAL MODULE
A- Demonstration
A1. a) Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.(Module- 1)
b) Write a function to calculate factorial of a number.
A2. Write a python program to convert temperature to and from Celsius to fahrenheit.(Module-
1)
A3. Write a Python class to reverse a string word by word.(Module- 2)
B- Exercise
B1. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.(Module- 2)
B2. Read N numbers from the console and create a list. Develop a program to print mean,
variance and standard deviation with suitable messages.(Module- 3)
B3. Write a python program to sort dictionary elements based on key(Module- 3)
B4. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(c=a/b). Write suitable assertion for a>0 in function DivExp and raise an exception for
when b=0. Develop a suitable program which reads two values from the console and calls
a function DivExp.(Module- 4)
C- Structured Inquiry
C1. Write a Python program to count frequency of characters in a given file.(Module- 4)
C2. Write a program to read 3 subject marks and display pass or failed using class and
object.(Module- 5)
C3. Write a program to calculate area of a circle using classes and function(Module- 5)
D- Open Ended Experiments
D1. Develop a program to read the student details like Name, USN, and Marks in three
subjects. Display the student details, total marks and percentage with suitable messages.
D2. Develop a program to compute binomial coefficient (Given N and R).
A1. a)
N = int(input("Enter number of terms? "))
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# check if the number of terms is valid
If N <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(N):
print(recur_fibo(i))
b)
def factorial(x):
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# to take input from the user
num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
A2.
# Python Program to convert temperature in celsius to fahrenheit
c = float(input(“Enter temperature in celsius)
# calculate fahrenheit
fahrenheit = (c* 1.8) + 32
print('%f degree Celsius is equal to %f degree Fahrenheit' %(celsius,fahrenheit))
#Python Program to convert temperature in fahrenheit to Celsius
f = float(input(“Enter temperature in Fahrenheit”)
# calculate Celsius
celsius = (f - 32) / 1.8
print('%f degree Celsius is equal to %f degree Fahrenheit' %(celsius,fahrenheit))
A3.
class printRev:
def rev(self,s):
rev=s[: :-1]
return rev
a=input(“Enter the String:”)
print(printRev().rev(a))
B1.
s=input(“Enter the String:”)
d={}
for ch in s:
d[ch]=[Link](ch,0)+1
print(d)
B2.
import numpy as np
lst = []
# number of elements as input
n = int(input("Enter number of elements : "))
print("Enter the elements of the list:")
# iterating till the range
for i in range(0, n):
ele = int(input())
[Link](ele) # adding the element
print(lst)
# Calculating average using average()
print("Mean=", [Link](lst))
# Calculating variance using var()
print("Variance=", [Link](lst))
# Calculating standard deviation
print("Standard Deviation=", [Link](lst))
B3.
data = {'banana': 80,'cherry': 200,'apple': 60,'grapes':120}
# Sorting on the basis of key in alphabetically ascending order
sorted_result = sorted([Link]())
print(sorted_result)
B4.
def DivExp(a,b):
try:
c=a/b
return c
except ZeroDivisionError:
print("Division by Zero!")
n1=float(input(“Enter first number”))
n2=float(input(“Enter second number”))
result=DivExp(n1,n2)
print(result)
C1.
f="D:\\VVCE\\[Link]"
file = open ( f, "r" )
a=[]
b={}
for i in file:
for j in range(0,len(i)):
[Link](i[j])
for i in a:
if i in b:
b[i]+=1
else:
b[i]=1
print(b)
Output:
OR
import collections
import pprint
file_input = "D:\\VVCE\\[Link]"
with open(file_input, 'r') as info:
count = [Link]([Link]())
value = [Link](count)
print(value)
Output:
C2.
class stud:
def display(self):
i=0
print('Enter the Number of subjects:', end=’’)
no_subj=int(input())
while i<no_subj:
subj=input('Enter the subject: ')
marks=int(input('Enter the corresponding marks: '))
if marks<40:
print(“Fail”)
else:
print(“Pass”)
i=i+1
return self
s=stud
[Link]()
C3.
import math
class Area:
def area_of_the_circle (self, Radius):
area = [Link] * Radius**2
return area
R = float (input ("Please enter the radius of the given circle: "))
A= Area()
print (" The area of the given circle is: ", A.area_of_the_circle (R))
D1.
import numpy as np
class students:
count = 0
def __init__(self, name,usn):
[Link] = name
[Link]=usn
[Link] = []
[Link] = [Link] + 1
def enterMarks(self):
for i in range(3):
m = int(input("Enter the marks of %s in %d subject: "%([Link], i+1)))
[Link](m)
def display(self):
print("Name:",[Link])
print("USN:",[Link])
print ([Link], "got ", [Link], " ", "marks")
print("Total marks=",sum([Link]))
print("Percentage=",(sum([Link])/300)*100)
name = input("Enter the name of Student:")
usn = input("Enter the usn of Student:")
s1 = students(name,usn)
[Link]()
[Link]()
D2.
import math
N=int(input(“Enter N”))
R=int(input(“Enter R”))
B= [Link](N,R)
print(“Binomial Coefficient=”,B)
or
from math import factorial as fact
def binomial(a,b):
return fact(a) // fact(b) // fact(a-b)
N=int(input("Enter N"))
R=int(input("Enter R"))
B= binomial(N,R)
print("Binomial Coefficient=",B)