All Blogs

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 »

M.Sc Python Programming Assignment 13

M.Sc Python Programming Assignments Assignment 13 Question: Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25])and makes a new list of only the first and last elements of the given list. Code: def extract_first_and_last(numbers): if len(numbers) >= 2: return [numbers[0], numbers[-1]] else: return numbers # Example …

M.Sc Python Programming Assignment 13 Read More »

M.Sc Python Programming Assignment 10

M.Sc Python Programming Assignments Assignment 10 Question:   Write a program (using functions!) that asks the user for a long string containing multiple  words. Print back to the user the same string, except with the words in backwards order. E.g “ I am Msc student” is :”student Msc am I” Code: def reverse_words(string): words = string.split() …

M.Sc Python Programming Assignment 10 Read More »

M.Sc Python Programming Assignment 9

M.Sc Python Programming Assignments Assignment 9 Question:   Write a program that asks the user how many Fibonnaci numbers to generate and then    generates them. Code: def generate_fibonacci_numbers(count): fibonacci_numbers = [] if count >= 1: fibonacci_numbers.append(0) if count >= 2: fibonacci_numbers.append(1) if count > 2: for i in range(2, count): fibonacci_numbers.append(fibonacci_numbers[i-1] + fibonacci_numbers[i-2]) return …

M.Sc Python Programming Assignment 9 Read More »

Tech Amplifier Final Logo