Python Lab Programs New
Python Lab Programs New
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: " ) )
#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")
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()