Python Lab Programs
Python Lab Programs
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.
percentage = total_marks/300*100
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.
if age>60 :
print('Senior Citizen')
else:
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.
def fibonacci_sequence(length):
sequence = [0, 1] # Initialize the sequence with the first two Fibonacci numbers
sequence.append(next_number)
return sequence
fibonacci = fibonacci_sequence(n)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
return coefficient
coefficient = binomial_coefficient(n, r)
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
def calculate_mean(numbers):
def calculate_standard_deviation(variance):
return math.sqrt(variance)
numbers = []
for i in range(N):
numbers.append(num)
mean = calculate_mean(numbers)
standard_deviation = calculate_standard_deviation(variance)
print("Mean:", mean)
print("Variance:", variance)
def count_digit_frequency(number):
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message
if digit.isdigit():
frequency[int(digit)] += 1
for i in range(10):
count_digit_frequency(number)
def count_word_frequency(filename):
word_frequency = {}
def print_top_10_words(word_frequency):
# Sort the dictionary in reverse order of frequency
sorted_words = sorted(word_frequency.items(), key=lambda x: x[1], reverse=True)
6. Develop a program to sort the contents of a text file and write the sorted contents into a
separate text file
lines = input_file.readlines()
lines.sort()
output_file.write(line + '\n')
input_filename = "D:/Documents/Python/test.txt"
output_filename = "D:/Documents/Python/Output.txt"
# Sort the contents of the input file and write to the output file
sort_file_contents(input_filename, output_filename)
import os
import zipfile
# Get the absolute path of the folder and the zip file
folder_path = os.path.abspath(folder_name)
zip_path = os.path.abspath(zip_name)
if not os.path.exists(folder_path):
return
try:
# Calculate the relative path for the file inside the ZIP
backup_zip.write(file_path, arcname=relative_path)
except Exception as e:
backup_zip_name = "backup.zip" # Change this to the desired name for the backup ZIP file
backup_folder_to_zip(folder_to_backup, backup_zip_name)
8. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(c=a/b). Write suitable assertion for a>0 in function DivExp and raise an exception for when
b=0. Develop a suitable program which reads two values from the console and calls a function
DivExp
if b == 0:
return a / b
try:
result = DivExp(a, b)
print("Result:", result)
print("Error:", e)
9. Define a function which takes TWO objects representing complex numbers and returns new
complex number with a addition of two complex numbers. Define a suitable class ‘Complex’
to represent the complex number. Develop a program to read N (N >=2) complex numbers and
to compute the addition of N complex numbers.
class Complex:
self.real = real
self.imaginary = imaginary
def __str__(self):
# Main program
if n < 2:
else:
total = Complex(0, 0)
for i in range(n):
10. Develop a program that uses class Student which prompts the user to enter marks in three
subjects and calculates total marks, percentage and displays the score card details
class Student:
self.name = name
self.usn = usn
self.marks = [0, 0, 0]
self.total_marks = 0
self.percentage = 0.0
def getMarks(self):
for i in range(3):
self.total_marks += self.marks[i]
def display(self):
print("\nScore Card")
print("Name:", self.name)
print("USN:", self.usn)
# Main program
student.getMarks()
student.display()