All Blogs

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 »

CRUD Operations with Django

CRUD Operations with Django In Django, CRUD (Create, Read, Update, Delete) operations are used to interact with the database and perform various data manipulation tasks. Django’s built-in ORM (Object-Relational Mapping) provides a convenient way to handle these operations. Here’s a step-by-step guide on how to use CRUD operations with Django: Assuming you have already set …

CRUD Operations with Django 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 »

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 »

Tech Amplifier Final Logo