python programs
python programs
python programs
1a.Develop a program to read the student details like USN, name and marks of three subjects .Display the
student details, total marks and percentage with suitable message
Program:
Output:
Enter Usn 4BD22IS005
Enter Name: hafsa
Enter Marks Of 3 Subjects
Enter The Marks :90
[90]
Enter The Marks :60
[90, 60]
Enter The Marks :80
[90, 60, 80]
4BD22IS005 hafsa 230 76.66666666666667
1b.Develop a program to read the name and year of birth of a person .Display whether the person is senior
citizen or not.
Program:
import datetime
Output:
Program:
n=int(input('Enter n'))
if n<0:
print('Enter the positive number')
else:
print('fibonnaci sequence')
for i in range(n):
print(fib(i)) # Function call
Output:
Enter n 5
fibonnaci sequence
0
1
1
2
3
2b.Write a function to calculate a factorial of a number .Develop a program to compute binomial coefficient
(Given N and R)
n=int(input('Enter n'))
r=int(input('Enter r'))
if n<0 or r<0:
print('invalid')
else:
fn=fact(n) #Function call to calculate n!
Print(„Factorial of n is”,fn)
fr=fact(r) #Function call to calculate r!
Print(„Factorial of r is”,fr)
fnr=fact(n-r) #Function call to calculate (n-r)!
Print(„Factorial of n-r is”,fnr)
BC=fn/(fr*fnr) # computing binomial coefficient nCr= n!/r!*(n-r)!
print ('binomial coefficient is',BC)
------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter n 6
Enter r 3
Factorial of n is 720
Factorial of r is 6
Factorial of n-r is 6
Binomial coefficient is 20.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
import math
data=listA
----------------------------------------------------------------------------------------------------------------------------- --------------
Output:
Program:
frequency={}
Print(“Enter the multi digit number”)
Digit=input()
For i in digit:
if i not in frequency :
frequency[i]=1
else:
frequency[i]=frequency[i]+1
print (frequency)
Output:
Program:
--------------------------------------------------------------------------------------------------------------------------------------------
Once upon a time there lived a poor widow and her son Jack. One day, Jack‟s mother told him to sell their only cow.
Jack went to the market and on the way he met a man who wanted to buy his cow
Output:
1st 10 Most Frequent words are :{'a': 3, 'to': 3, 'and': 2, 'Jack': 2, 'the': 2, 'Once': 1, 'upon': 1, 'time': 1, 'there': 1, 'lived'
: 1}
6. Develop a program to sort the contents of a text file and write the sorted contents into a separate text file
words = [ ]
f=open („D://sample.txt‟)
text= f.read()
content=text. split („\n‟)
for line in content:
temp=line. split ()
for w in temp:
words. append (w)
words. sort()
f1=open („D://new.txt‟, „w‟)
f1.write („ ‟.join (words))
f1.close ()
7. Develop a program to backing up a given folder (folder is a current working directory) into a ZIP file by using
relevant modules and suitable methods
----------------------------------------------------------------------------------------------------------------------------- -
#! python3
# backupToZip.py
# Copies an entire folder and its contents into
# a zip file whose filename increments.
import zipfile, os
def backupToZip(folder):
# Backup the entire contents of "folder" into a zip file.
folder = os.path.abspath(folder) # make sure folder is absolute
# Figure out the filename this code should used based on
# what files already exist.
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1