All Blogs

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 »

M.Sc Python Programming Assignment 17

M.Sc Python Programming Assignments Assignment 17 Question: Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. Code: def factorial(n): if n == 0: return 1 else: return n * factorial(n – 1) number = int(input(“Enter a non-negative integer: “)) result = factorial(number) …

M.Sc Python Programming Assignment 17 Read More »

M.Sc Python Programming Assignment 16

M.Sc Python Programming Assignments Assignment 16 Question: Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters Code: def count_uppercase_lowercase(sentence): uppercase_count = 0 lowercase_count = 0 for char in sentence: if char.isupper(): uppercase_count += 1 elif char.islower(): lowercase_count += 1 return uppercase_count, lowercase_count sentence = input(“Enter …

M.Sc Python Programming Assignment 16 Read More »

M.Sc Python Programming Assignment 15

M.Sc Python Programming Assignments Assignment 15 Question: Write a program that accepts a sentence and calculate the number of letters and digits Code: def count_letters_digits(sentence): letter_count = 0 digit_count = 0 for char in sentence: if char.isalpha(): letter_count += 1 elif char.isdigit(): digit_count += 1 return letter_count, digit_count sentence = input(“Enter a sentence: “) letter_count, …

M.Sc Python Programming Assignment 15 Read More »

M.Sc Python Programming Assignment 14

M.Sc Python Programming Assignments Assignment 14 Question: Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalised. Code: def capitalize_lines(lines): capitalized_lines = [] for line in lines: capitalized_line = line.upper() capitalized_lines.append(capitalized_line) return capitalized_lines # Accept input lines print(“Enter lines (Press Enter to stop):”) …

M.Sc Python Programming Assignment 14 Read More »

Tech Amplifier Final Logo