0% found this document useful (0 votes)
12 views

Python Lab Programs New

Uploaded by

Harshitha B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Lab Programs New

Uploaded by

Harshitha B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. a.

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.
#For One Student
print("Student Details")
name=input("Enter the student name:")
usn = input("Enter the student usn:")
print(input("Enter the Student Marks of Three Subject"))
m1 = int(input("Enter marks1:"))
m2 = int(input("Enter marks2:"))
m3 = int(input("Enter marks3:"))

print("Marks1:", m1)
print("Marks2:", m2)
print("Marks3:", m3)
total_marks = m1+m2+m3
print("Total Marks:", total_marks)
per=((m1+m2+m3)/300)*100
print("Percentage:", per)

#For N students
print("Student Details")
n = int(input("Enter the number of students"))
for i in range(1,n+1):
print("Enter Student details of", i)
name=input("Enter the student name:")
usn = input("Enter the student usn:")
print(input("Enter the Student Marks of Three Subject"))
m1 = int(input("Enter Subject 1 marks:"))
m2 = int(input("Enter Subject 2 marks:"))
m3 = int(input("Enter Subject 3 marks:"))
print("Marks1:", m1)
print("Marks2:", m2)
print("Marks3:", m3)
total_marks = m1+m2+m3
print("Total Marks:", total_marks)
per=((m1+m2+m3)/300)*100
print("Percentage:", per)

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("Enter the Person Name:")
age = int ( input ( "Enter the age of the person: " ) )

#Condition to check whether the person is senior citizenship or not:


if age >= 60:
status = " Senior citizenship "
else:
status = " Not a senior citizenship "

print("Name of the person is ", name)


print ( " The person is ", status )

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


the console.
fib1=0
fib2=1
count=0
n = int(input("Enter the number:"))
print(fib1)
print(fib2)
for count in range(3, n+1):
fib3=fib2+fib1
print(fib3)
fib1=fib2
fib2=fib3

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


compute binomial coefficient (Given N and R).
#Factorial Program
fact=1
n = int(input("Enter the number:"))
if n == 0:
fact = 1
for i in range(1, n+1):
fact = fact * i
print("N=%d Factorial=%d" % (n,fact))

#Binomial Coeeficient
print("Enter the Value of n: ", end="")
n = int(input())
print("Enter the Value of r: ", end="")
r = int(input())
fact = i = 1
while i<=n:
fact = i*fact
i += 1

numerator = fact
sub = n-r
fact = i = 1

while i<=sub:
fact = i*fact
i += 1

denominator = fact
fact = i = 1
while i<=r:
fact = i*fact
i += 1

denominator = fact*denominator
comb = numerator/denominator
print("\nCombination (nCr) =", comb)

4. Read a multi-digit number (as chars) from the console. Develop a program to
print the frequency of each digit with suitable message.
number=int(input("Enter any Number"))
print("Digit\tFrequency")
for i in range(0,10):
count=0;
temp=number;
while temp>0:
digit=temp%10
if digit==i:
count=count+1
temp=temp//10;
if count>0:
print(i,"\t",count)

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()].
def sorting(filename):
infile = open(filename)
words = []
for line in infile:
temp = line.split()
for i in temp:
words.append(i)
infile.close()
words.sort()
outfile = open("result.txt", "w")
for i in words:
outfile.writelines(i)
outfile.writelines(" ")
outfile.close()
sorting("sample.txt")

fname = raw_input("Enter file name: ")


fh = open(fname)
lst = list() # list for the desired output
for line in fh: # to read every line of file romeo.txt
word= line.rstrip().split() # to eliminate the unwanted blanks and turn the
line into a list of words
for element in word: # check every element in word
if element in lst: # if element is repeated
continue # do nothing
else : # else if element is not in the list
lst.append(element) # append
lst.sort() # sort the list (de-indent indicates that you
sort when the loop ends)
print lst # print the list

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
import zipfile

zf = zipfile.ZipFile("myzipfile.zip", "w")
for dirname, subdirs, files in os.walk("mydirectory"):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
zf.close()

You might also like