First - Year - Python - Programs - Jupyter Notebook - Python Lab Program VTU
First - Year - Python - Programs - Jupyter Notebook - Python Lab Program VTU
Develop a program to read the student details like Name, USN, and Marks
in three subjects. 9
name = input("Name=")
usn = input("USN=")
mark1 = int(input("Mark1="))
mark2 = int(input("Mark2="))
mark3 = int(input("Mark3="))
total = mark1+mark2+mark3
per = total/3
print("STUDENT DETAILS")
print("Name = ", name)
print("USN = ",usn)
print("Total = ",total)
print("Percentage = ",per)
Name=viji
USN=23
Mark1=90
Mark2=90
Mark3=90
STUDENT DETAILS
Name = viji
USN = 23
Total = 270
Percentage = 90.0
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.
name = input("Name=")
year = int(input("Year="))
age = 2 0 2 3 - year
i f age>=60:
print("Senior citizen")
else:
print("Not Senior Citizer")
Name=Viji
Year=1983 Not
Senior Citizer
def binomial_coefficient(n,k):
a= (factorial(n)) (factorial(k) factorial(n k))
return a
3. Read N numbers from the console and create a list. Develop a program to
print mean, variance and standard deviation with suitable messages
I n [2 5 ]:
import math
list = [ ]
N = int(input(HEnter N =H))
for i in range(N):
item=int(input())
list.append(item)
# Mean
sum=0
f o r i i n list:
sum = sum+i
mean = sum/N
print(" M e a n = H , m e a n )
# Variance
d=0
f o r i i n list:
d = d+(i-mean)**2
v a r= d /N
print(HVariance = H,round(var,2))
Enter N =5
1
2
3
4
5
Mean = 3.0
Variance = 2.0
SD = 1.41
4. Read a multi-digit number (as chars) from the console. Develop a program to
print the frequency of each digit with suitable message.
I n [2 ]: #Method 1
n = list(input(HEnter multi-digit number = H))
u = set(n)
for i in u:
c=n.count(i)
print(HFrequency ofH,i,H ,c)
In [18]: Method 3
num = input("Enter the number: ")
for i in s e t ( n u m ) :
count = 0
for j i n n u m : # I t e r a t e s o v e r a L L d i g i t s i n t h e n u m
if ( i = = j ) : # C h e c k s f o r t h e d i g i t s i m i L a r i t y .
count = count + 1
print("The frequency of digit ", i , " = ", count)
word_dst = {}
for w d in s e t ( w o r d _ l s t ) :
word dst[wd] = word_lst.count(wd)
# Creating dictionary with word and count.
count = 0
for i in s o r t e d ( w o r d _ d s t . v a l u e s ( ) , r e v e r s e . u e ) :
for k e y in w o r d _ d s t :
if(word_dst[key] == 1):
print(count+1, The word", key, "frequency =", i)
del word_dst[key]
count+=1
break
if(count == 10):
break
word_dst = {}
for wd in s et(word_lst):
word_dst[wd] = word_lst.count(wd)
#Creating dictionary with word and count.
#sortedkey = sorted(dst,key=dst.get, reverse=True)
sortedkey = sorted(word_dst,key=word_dst.get, reverse=True)
#sortedkey is a List of keys in decreasing order.
count = 0
for y in sortedkey:
print(count+1," Frequency of word ", y,'=', word_dst[y])
count+=1
if(count == 10):
break
mango : 4
banana : 3
grapes : 3
guava : 3
is : 1
and : 1
sapota : 1
pineapple : 1
In [13]: #Method 4: Lambda function
fp = open('fruits.txt', 'r+')
word_lst = []
f o r line i n fp.readlines():
wor d_ ls t + = l i n e . s p l i t ( )
word_dst =
f o r w d i n set(word_lst):
word_dst[wd] = word_lst.count(wd)
#Creating dictionary with word and count .
#sortedkey = sorted(dst,key=dst .get, reverse=True)
sortedkey = sorted(word_dst.items(),key=lambda x:x[1], reverse=True)
#sortedkey is a List of sorted tupLe with key:vaLue
count = 0
f o r y i n sortedkey:
print(count+1," Fequency of word ", y[1])
count+=1 if(count
== 10):
break
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()].
I n [ 1 2 ] : fp = open("fruits.txt", "r")
word_lst = []
f o r 1 i n fp:
word_lst+=1.split()
#append aLL the words to the List -> Lst
word_lst.sort()
# Sorts aLL the words aLphabeticaL order.
' s t r = " ".join(word_lst)
#Joins aLL words by the deLimited " " = space .
fp = open("textw.txt", "w")
#t e x t w . t x t i s t h e f i L e t o w h i c h t h e s o r t e d c o n t e n t t o b e w r i t t e n .
#O p e n t h e f i l e t o b e w r i t t e n i n w r i t e m o d e .
fp.write(str)
#Write the content.
fp.close()
#C L o s e t h e f i l e p o i n t e r .
In [11]: Method 2
f r o m shutil i m p o r t make_archive
make_archive(
'myzipfile', # Z i p f i l e n a m e .
'zip', # the archive format - or tar, bztar, gztar
root_dir.None, # r o o t f o r a r c h i v e - c u r r e n t w o r k i n g d i r if N o n e
base_dir="../Python_Programs") # s t a r t a r c h i v i n g f r o m h e r e - c w d i f N o n e t o o
#t e s t d i r i s t h e f o l d e r t o b e z i p p e d .
Out[11]: 'myzipfile.zip'