Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

First - Year - Python - Programs - Jupyter Notebook - Python Lab Program VTU

Uploaded by

vedantaverma08
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

First - Year - Python - Programs - Jupyter Notebook - Python Lab Program VTU

Uploaded by

vedantaverma08
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1(a).

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

2(a). Develop a program to generate Fibonacci sequence of length (N). Read N


from the console.
I n [ 2 3 ] : n = int(input("Enter t h e n t h v a l u e : "))
a=0
b=1
sum = 0
i=0
p r i n t ( " F i b o n a c c i S e r i e s : ", end = " ")
w h i l e ( i < n):
p r i n t ( s u m , end = " ")
a=b
b = sum
sum = a + b
i=i+1

Enter the nth value: 5


Fibonacci Series : 0 1 1 2 3

2(b). Write a function to calculate factorial of a number. Develop a program to


compute binomial coefficient (Given N and R).
I n [2 4 ]:
# This program for first year students
def factorial(z):
if z==1:
return 1
else:
return z* factorial(z-1)

def binomial_coefficient(n,k):
a= (factorial(n)) (factorial(k) factorial(n k))
return a

n=int(input("Enter N=H)) k=int(input("Enter K=")) print( " T h e


binomial coefficient is:H,binomial_coefficient(n,k))

Enter N=9 Enter K=2 The binomial


coefficient is: 36.0

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))

# Stan dard Deviation


sd = math.sqrt(var)
print(HSD =",round(sd,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)

Enter multi-digit number = 656465464654


Frequency of 5 = 3
Frequency of 4 = 4
Frequency of 6 = 5
I n [1 ]: #Method 2
n = input("Enter multi-digit number -
1=[]
for i in n :
l.append(i)
s = s e t ( 1 ) for
i in s :
c=l.count(i)
print("Frequency of",i," ,c)

Enter multi-digit number = 53544254645423436474


Frequency of 5 = 4
Frequency of 7 = 1
Frequency of 2 = 2
Frequency of 4 = 8
Frequency of 6 = 2
Frequency of 3 = 3

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)

Enter the number: 456457568759


The frequency of digit 8 = 1
The frequency of digit 7 = 2
The frequency of digit 5 = 4
The frequency of digit 6 = 2
The frequency of digit 9 = 1
The frequency of digit 4 = 2

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] read the file
In [15]: #Method 1: Sorting dictionary vaLues and fiLtering the frequency of words
fp = open('fruits.txt', 'r+')
word_lst = []
for l i n e in f p . r e a d l i n e s ( ) :
word_lst += line.split()

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

1 The word mango frequency = 4


2 The word guava frequency = 3
3 The word banana frequency = 3
4 The word grapes frequency = 3
5 The word is frequency = 1
6 The word sapota frequency = 1
7 The word and frequency = 1
8 The word pineapple frequency = 1
In [14]: #Method 2: sorted() and Using dictionary getmethod
fp = open('fruits.txt', 'r+')
word_lst = []
'for line in fp.readlines():
word_lst += line.split()

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

1 Frequency of word mango = 4


2 Frequency of word guava = 3
3 Frequency of word banana = 3
4 Frequency of word grapes = 3
5 Frequency of word is = 1
6 Frequency of word sapota = 1
7 Frequency of word and = 1
8 Frequency of word pineapple = 1

In [4]: #Method 3: Lambda function


text = open("fruits.txt", "r")
d = dict()
'for line in text:
line = line.strip()
line = line.lower()
words = line.split( )
for word in words:
if word in d:
d[word] = d[word] + 1
else :
d[word]
da = sorted(d.items(), key x:x[ ], reverse
sorted_dict = dict(da)
i=1
for key in sorted_dict.keys():
if i<=10:
print(key, ":", d[key])
i=i+1

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

1 Fequency of word mango = 4


2 Fequency of word guava = 3
3 Fequency of word banana = 3
4 Fequency of word grapes = 3
5 Fequency of word is = 1
6 Fequency of word sapota = 1
7 Fequency of word and = 1
8 Fequency of word pineapple = 1

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 .

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.
#Method 1 import os i m p o r t zipfile
In
zf = zipfile.ZipFile("myzipfile.zip", "w")
f o r dirname, subdirs, files i n os.walk("../Python_Programs"): zf.write(dirname)
f o r filenamefiles:
zf.write(os.path.join(dirname, filename))
zf.close()

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'

You might also like