Python Lab Manual
Python Lab Manual
Display the student details, total marks, and percentages with suitable messages.
# Read student details
name = input("Enter student's name: ")
usn = input("Enter student's USN: ")
# Read marks for three subjects
marks1 = float(input("Enter marks in subject 1: "))
marks2 = float(input("Enter marks in subject 2: "))
marks3 = float(input("Enter marks in subject 3: "))
# Calculate total marks and percentage
total_marks = marks1 + marks2 + marks3
percentage = (total_marks / 300) * 100
# Display student details, total marks, and percentage
print("\n--- Student Details ---")
print("Name:",name)
print("USN:",usn)
print("Marks in Subject 1:",marks1)
print("Marks in Subject 2:",marks2)
print("Marks in Subject 3:",marks1)
print("Total Marks" + str(total_marks) +"out of 300")
print("Percentage:"+ str(percentage)+"%")
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.
# Input: Name and Year of Birth
name = input("Enter your name: ")
year_of_birth = int(input("Enter your year of birth: "))
if __name__ == "__main__":
# Read N from the console
N = int(input("Enter the length of the Fibonacci sequence: "))
if __name__ == "__main__":
# Read N and R from the console
N = int(input("Enter the value of N: "))
R = int(input("Enter the value of R: "))
def main():
# Read N from the console
N = int(input("Enter the number of elements: "))
# Calculate mean
mean = statistics.mean(numbers)
# Calculate variance
variance = statistics.variance(numbers)
return digit_frequency
if __name__ == "__main__":
# Read a multi-digit number as characters from the console
number = input("Enter a multi-digit number: ")
# Use regular expressions to find all words (considering words as sequences of alphanumeric
characters)
words = re.findall(r'\b\w+\b', text.lower())
if __name__ == "__main__":
# Path to the text file
file_path = 'example.txt' # Replace 'example.txt' with the path to your text file
if __name__ == "__main__":
# Specify the input and output file paths
input_file = 'input.txt' # Replace with your input file path
output_file = 'sorted_output.txt' # Replace with your desired output file path
# Sort the contents of the input file and write to the output file
sort_file_contents(input_file, output_file)
def backup_folder_to_zip(folder_name):
# Get the current working directory
current_dir = os.getcwd()
if __name__ == "__main__":
# Folder name to be backed up
folder_name = input("Enter the name of the folder to back up: ")
# Raise an exception if b is 0
if b == 0:
raise ZeroDivisionError("Division by zero is not allowed")
if __name__ == "__main__":
try:
# Read two values from the console
a = float(input("Enter the value of a (must be greater than 0): "))
b = float(input("Enter the value of b: "))
except AssertionError as e:
print(f"Error: {e}")
except ZeroDivisionError as e:
print(f"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.
def main():
# Read the number of complex numbers to be added (N >= 2)
N = int(input("Enter the number of complex numbers (N >= 2): "))
if N < 2:
print("N must be greater than or equal to 2.")
return
if __name__ == "__main__":
main()} + {self.imag}i"
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. [Hint: Use list to store the
marks in three subjects and total marks. Use __init__() method to initialize name, USN and the lists to
store marks and total, Use getMarks() method to read marks into the list, and display() method to
display the score card details.]
class Student:
def __init__(self, name, usn):
self.name = name
self.usn = usn
self.marks = [] # List to store marks in three subjects
self.total_marks = 0
self.percentage = 0.0
def getMarks(self):
print(f"Enter marks for {self.name} (USN: {self.usn}):")
for i in range(3):
mark = float(input(f"Subject {i+1} marks: "))
self.marks.append(mark)
# Calculate percentage
self.percentage = self.total_marks / 3
def display(self):
print("\nScore Card")
print("----------")
print(f"Name: {self.name}")
print(f"USN: {self.usn}")
for i in range(3):
print(f"Subject {i+1} marks: {self.marks[i]}")
print(f"Total Marks: {self.total_marks}")
print(f"Percentage: {self.percentage:.2f}%")
# Main Program
if __name__ == "__main__":
# Read student details
name = input("Enter the student's name: ")
usn = input("Enter the student's USN: ")