python lab programs
python lab programs
Display the student details, total marks and percentage with suitable messages.
percentage = Total_Marks/3
1 b. Develop a program to read the name and year of birth of a person. Display whether the person is
a senior citizen or not.
import datetime
today = datetime.date.today()
year = today.year
age = year-Birth_year
if age>=60:
else:
first_number = 0
second_number = 1
if num<= 0:
elif num == 1:
else:
for i in range(2,num):
next_number = first_number+second_number
first_number = second_number
second_number = next_number
print(next_number,end=' ')
2b. Write a function to calculate factorial of a number. Develop a program to compute binomial
coefficient (Given N and R).
def factorial(num):
result = 1
for i in range(num,0,-1):
else:
result = fact(n)/(fact(r)*fact(n-r))
3. Read N numbers from the console and create a list. Develop a program to print mean, variance
and standard deviation with suitable messages
import math
List_1 = list()
for i in range(int(number)):
sum_series=0
sum_square=0
for x in List_1:
sum_series = sum_series+x
mean = sum_series/number
for x in List_1:
sum_square =sum_square+(x-mean)**2
#print("sum_square=", sum_square)
variance=sum_square/number
std_dev = math.sqrt(variance)
4. Read a multi-digit number (as chars) from the console. Develop a program to print the frequency
of each digit with suitable message.
num=input("input multi-digit number:")
n=len(num)
n0,n1,n2,n3,n4,n5,n6,n7,n8,n9=0,0,0,0,0,0,0,0,0,0
for i in range(n):
if num[i]=='0':
elif num[i]=='1':
n1+=1
elif num[i]=='2':
n2+=1
elif num[i]=='3':
n3+=1
elif num[i]=='4':
n4+=1
elif num[i]=='5':
n5+=1
elif num[i]=='6':
n6+=1
elif num[i]=='7':
n7+=1
elif num[i]=='8':
n8+=1
elif num[i]=='9':
n9+=1
dfreq=[n0,n1,n2,n3,n4,n5,n6,n7,n8,n9]
for i in range(10):
if dfreq[i]==0:
continue
print(i,"digit", dfreq[i],"times")
5. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use dictionary
with distinct words and their frequency of occurrences. Sort the dictionary in the reverse order of
frequency and display dictionary slice of first 10 items]
ifile=open("abc.txt")
dict_words={}
#print("\n",words)
dict_words[word]=dict_words.get(word,0)+1
#print(dict_words)
list_words=[]
#print(key,val)
list_words.append((val,key))
list_words.sort(reverse=True)
print(list_words[0:10])
6. Develop a program to sort the contents of a text file and write the sorted contents into a separate
text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods
open(), readlines(), and write()]
ifile=open("file1.txt")
ofile=open("file2.txt", mode='w')
word_list=[]
words=line.split()
word_list.append(word)
word_list.sort()
print(word_list)
ofile.write(word+" ")
ofile.close()
7. Develop a program to backing Up a given Folder (Folder in a current working directory) into a ZIP
File by using relevant modules and suitable methods.
import os
os.getcwd()
import os
import sys
import pathlib
import zipfile
print(os.getcwd())
if not os.path.isdir(dirName):
sys.exit(0)
curDirectory = pathlib.Path(dirName)
archive.write(file_path,arcname=file_path.relative_to(curDirectory))
if os.path.isfile("myZip_Hithesh.zip"):
else:
def DivExp(a,b):
try:
if b==0:
c=a/b
return c
except ZeroDivisionError as x:
print(x)
except AssertionError as x:
print(x," ",y)
print(DivExp(x,y))
9. Define a function which takes two objects representing complex numbers and returns new
complex number with a addition of two complex numbers. define a suitable class ‘complex’ to
represent the complex number. develop a program to read n (n >=2) complex numbers and to
compute the addition of n complex numbers.
class Complex:
self.real=real
self.img=img
def __str__(self):
for i in range(n):
complex_list.append(Complex(m,n))
sum_series=Complex()
for x in complex_list:
sum_series+=x
Exp.10 Develop a program that uses class student which prompts the user to enter marks in three
subjects and calculates total marks, percentage and displays the score card details using init method.
class Student:
self.name=name
self.usn=usn
self.marks=list()
def getMarks(self):
self.marks += x
def getDetails(self):
def display(self):
total=0
for x in self.marks:
total+=x
x=Student()
x.getDetails()
x.getMarks()
x.display()