July 31, 2023

M.Sc Python Programming Assignment 24

M.Sc Python Programming Assignments Assignment 24 Question: Write a Python program to read a file line by line store it into an array. Code: def read_file_lines(file_name): lines = [] with open(file_name, ‘r’) as file: for line in file: lines.append(line.strip()) return lines # Example usage file_name = input(“Enter the file name: “) lines = read_file_lines(file_name) print(“File …

M.Sc Python Programming Assignment 24 Read More »

M.Sc Python Programming Assignment 23

M.Sc Python Programming Assignments Assignment 23 Question: Write a Python function to check whether a number is perfect or not Code: def is_perfect_number(number): divisors = [] for i in range(1, number): if number % i == 0: divisors.append(i) if sum(divisors) == number: return True else: return False number = int(input(“Enter a positive integer: “)) if …

M.Sc Python Programming Assignment 23 Read More »

M.Sc Python Programming Assignment 22

M.Sc Python Programming Assignments Assignment 22 Question: Write a Python function that takes a list and returns a new list with unique elements of the first list. Code: def unique_elements(lst): unique_list = list(set(lst)) return unique_list # Example usage my_list = [1, 2, 3, 2, 4, 5, 1, 3, 6] result = unique_elements(my_list) print(“Unique elements:”) print(result) …

M.Sc Python Programming Assignment 22 Read More »

M.Sc Python Programming Assignment 21

M.Sc Python Programming Assignments Assignment 21 Question: Write a Python program to find the greatest common divisor (gcd) of two integers  Code: def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) # Example usage num1 = int(input(“Enter the first integer: “)) num2 = int(input(“Enter the second integer: “)) gcd_result …

M.Sc Python Programming Assignment 21 Read More »

Tech Amplifier Final Logo