All Blogs

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 »

Creating first Django Project (Hello World in Django)

How to create “Hello world” program in django. Creating a “Hello, World!” application in Django is a simple process. Django is a Python web framework that allows you to build powerful web applications quickly and efficiently. Here’s a step-by-step guide to creating a basic “Hello, World!” Django application:     1. Install Django: Make sure …

Creating first Django Project (Hello World in Django) 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 »

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 »

M.Sc Python Programming Assignment 20

M.Sc Python Programming Assignments Assignment 20 Question: Write a Python program to get the sum of a non-negative integer. Code: def sum_of_digits(n): if n < 10: return n else: return n % 10 + sum_of_digits(n // 10) number = int(input("Enter a non-negative integer: ")) sum_result = sum_of_digits(number) print("Sum of the digits:", sum_result) Output: Enter a …

M.Sc Python Programming Assignment 20 Read More »

Tech Amplifier Final Logo