Python

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 »

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