Python assignments

M.Sc Python Programming Assignment 31

M.Sc Python Programming Assignments Assignment 31 Question: Write a Python class named Circle constructed by a radius and two methods which will  compute the area and the perimeter of a circle Code: import math class Circle: def __init__(self, radius): self.radius = radius def compute_area(self): return math.pi * self.radius ** 2 def compute_perimeter(self): return 2 * …

M.Sc Python Programming Assignment 31 Read More »

M.Sc Python Programming Assignment 30

M.Sc Python Programming Assignments Assignment 30 Question: Write a Python class named Rectangle constructed by a length and width and a method which will compute the area and perimeter of a rectangle. Code: class Rectangle: def __init__(self, length, width): self.length = length self.width = width def compute_area(self): return self.length * self.width def compute_perimeter(self): return 2 …

M.Sc Python Programming Assignment 30 Read More »

M.Sc Python Programming Assignment 29

M.Sc Python Programming Assignments Assignment 29 Question: Write a Python class to reverse a string word by word. Input string : ‘hello.py’ Expected Output : ‘.py hello’ Code: class StringReverser: def reverse_words(self, input_string): words = input_string.split() reversed_string = ‘ ‘.join(reversed(words)) return reversed_string # Example usage input_string = input(“Enter a string: “) reverser = StringReverser() reversed_string …

M.Sc Python Programming Assignment 29 Read More »

M.Sc Python Programming Assignment 28

M.Sc Python Programming Assignments Assignment 28 Question: Write a Python program to read a random line from a file Code: import random def read_random_line(file_name): with open(file_name, ‘r’) as file: lines = file.readlines() random_line = random.choice(lines) return random_line.strip() # Example usage file_name = input(“Enter the file name: “) random_line = read_random_line(file_name) print(“Random line from the file:”) …

M.Sc Python Programming Assignment 28 Read More »

M.Sc Python Programming Assignment 27

M.Sc Python Programming Assignments Assignment 27 Question: Write a Python program to copy the contents of a file to another file Code: def copy_file(source_file, destination_file): with open(source_file, ‘r’) as source: with open(destination_file, ‘w’) as destination: for line in source: destination.write(line) print(“File copied successfully!”) # Example usage source_file = input(“Enter the source file name: “) destination_file …

M.Sc Python Programming Assignment 27 Read More »

M.Sc Python Programming Assignment 26

M.Sc Python Programming Assignments Assignment 26 Question: Write a Python program to count the frequency of words in a file Code: def count_word_frequency(file_name): word_frequency = {} with open(file_name, ‘r’) as file: for line in file: words = line.strip().split() for word in words: if word in word_frequency: word_frequency[word] += 1 else: word_frequency[word] = 1 return word_frequency …

M.Sc Python Programming Assignment 26 Read More »

M.Sc Python Programming Assignment 25

M.Sc Python Programming Assignments Assignment 25 Question: Write a Python program to count the number of lines in a text file Code: def count_file_lines(file_name): line_count = 0 with open(file_name, ‘r’) as file: for line in file: line_count += 1 return line_count # Example usage file_name = input(“Enter the file name: “) line_count = count_file_lines(file_name) print(“Number …

M.Sc Python Programming Assignment 25 Read More »

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 »

Tech Amplifier Final Logo