Practical_5: Functions, Modules and Packages#
Exercise 1:#
Write a Python function add_numbers(a, b)
that takes two numbers and returns their sum.
# Exercise 1
Exercise 2:#
Create a function greet(name)
that takes a name and prints “Hello,
# Exercise 2
Exercise 3:#
Write a Python function is_even(n)
that checks whether a number is even.
# Exercise 3
Exercise 4:#
Write a function max_of_three(a, b, c)
that returns the largest of three numbers.
# Exercise 4
Exercise 5:#
Create a Python module math_utils.py
and define a function square(x)
that returns the square of a number.
# Exercise 5
Exercise 6:#
Write a function factorial(n)
that returns the factorial of a number using recursion.
# Exercise 6
Exercise 7:#
Write a function is_prime(n)
that checks if a number is prime.
# Exercise 7
Exercise 8:#
Write a function sum_of_list(lst)
that takes a list of numbers and returns the sum of all the elements.
# Exercise 8
Exercise 9:#
Write a function fibonacci(n)
that returns the n
th Fibonacci number using recursion.
# Exercise 9
Exercise 10:#
Create a function reverse_string(s)
that takes a string and returns the reverse of the string.
# Exercise 10
Exercise 11:#
Write a function count_vowels(s)
that counts the number of vowels in a string.
# Exercise 11
Exercise 12:#
Write a function is_palindrome(s)
that checks whether a string is a palindrome.
# Exercise 12
Exercise 13:#
Write a function fahrenheit_to_celsius(f)
that converts Fahrenheit to Celsius.
# Exercise 13
Exercise 14:#
Write a Python function is_leap_year(year)
that checks if a year is a leap year.
# Exercise 14
Exercise 15:#
Write a Python function find_max_in_list(lst)
that returns the maximum value from a list.
# Exercise 15
Exercise 16:#
Write a Python function remove_duplicates(lst)
that removes duplicates from a list.
# Exercise 16
Exercise 17:#
Write a Python function sum_of_squares(n)
that returns the sum of squares of the first n
natural numbers.
# Exercise 17
Exercise 18:#
Write a Python function check_password_strength(password)
that returns True
if the password meets the length and complexity requirements.
# Exercise 18
Exercise 19:#
Write a function merge_lists(list1, list2)
that merges two lists into one.
# Exercise 19
Exercise 20:#
Write a function find_factors(n)
that returns a list of all factors of a number n
.
# Exercise 20
Exercise 21:#
Write a Python module string_utils.py
that defines a function capitalize_first_letter(s)
which capitalizes the first letter of each word in a string.
# Exercise 21
Exercise 22:#
Write a Python function sum_even_numbers(lst)
that returns the sum of all even numbers in a list.
# Exercise 22
Exercise 23:#
Write a Python function generate_random_number()
that returns a random number between 1 and 100.
# Exercise 23
Exercise 24:#
Write a Python function get_even_numbers(lst)
that returns a list of all even numbers from a given list.
# Exercise 24
Exercise 25:#
Write a Python function count_occurrences(lst, item)
that counts how many times an item appears in a list.
# Exercise 25
Exercise 26:#
Write a Python function gcd(a, b)
that calculates the greatest common divisor of two numbers using recursion.
# Exercise 26
Exercise 27:#
Create a Python module date_utils.py
and define a function days_in_month(month, year)
that returns the number of days in a given month.
# Exercise 27
Exercise 28:#
Write a Python function convert_to_binary(n)
that converts an integer to its binary equivalent.
# Exercise 28
Exercise 29:#
Write a Python function multiply_elements(lst, factor)
that multiplies each element of a list by a given factor.
# Exercise 29
Exercise 30:#
Write a Python function replace_spaces(s)
that replaces spaces in a string with underscores.
# Exercise 30
Exercise 31:#
Write a Python function is_substring(s1, s2)
that checks whether s1
is a substring of s2
.
# Exercise 31
Exercise 32:#
Write a Python function sum_of_digits(n)
that returns the sum of the digits of a given number.
# Exercise 32
Exercise 33:#
Write a Python function caesar_cipher(s, shift)
that implements the Caesar cipher encryption.
# Exercise 33
Exercise 34:#
Write a Python function find_min_in_list(lst)
that returns the minimum value from a list.
# Exercise 34
Exercise 35:#
Write a Python function double_each_element(lst)
that doubles each element in a list.
# Exercise 35
Exercise 36:#
Write a Python function mean(lst)
that returns the average of a list of numbers.
# Exercise 36
Exercise 37:#
Write a Python function remove_vowels(s)
that removes all vowels from a string.
# Exercise 37
Exercise 38:#
Write a Python function is_multiple_of(n, divisor)
that checks whether a number is a multiple of another number.
# Exercise 38
Exercise 39:#
Write a Python function concat_strings(lst)
that concatenates all strings in a list into a single string.
# Exercise 39
Exercise 40:#
Write a Python function get_unique_elements(lst)
that returns a list of unique elements from a given list.
# Exercise 40
Exercise 41:#
Write a Python function read_file(filename)
that reads the contents of a file and prints each line.
# Exercise 41
Exercise 42:#
Write a Python function save_to_file(filename, data)
that writes data to a file.
# Exercise 42
Exercise 43:#
Write a Python function find_longest_word(lst)
that finds the longest word in a list of words.
# Exercise 43
Exercise 44:#
Write a Python function replace_vowels(s)
that replaces all vowels in a string with asterisks (*
).
# Exercise 44
Exercise 45:#
Write a Python function nth_fibonacci(n)
that returns the n
th Fibonacci number without recursion.
# Exercise 45
Exercise 46:#
Write a Python function read_numbers_from_file(filename)
that reads a file containing numbers and returns them as a list.
# Exercise 46
Exercise 47:#
Write a Python function capitalize_first_last(s)
that capitalizes the first and last letter of each word in a string.
# Exercise 47
Exercise 48:#
Write a Python function sum_matrix(matrix)
that returns the sum of all elements in a 2D matrix.
# Exercise 48
Exercise 49:#
Write a Python function has_duplicates(lst)
that returns True
if a list contains duplicate elements, otherwise returns False
.
# Exercise 49
Exercise 50:#
Write a Python function sum_odd_numbers(lst)
that returns the sum of all odd numbers in a list.
# Exercise 50
Exercise 51:#
Write a Python function merge_dictionaries(dict1, dict2)
that merges two dictionaries.
# Exercise 51
Exercise 52:#
Write a Python function find_second_largest(lst)
that returns the second-largest number in a list.
# Exercise 52
Exercise 53:#
Write a Python function read_csv_file(filename)
that reads a CSV file and prints each row.
# Exercise 53
Exercise 54:#
Write a Python function count_consonants(s)
that counts the number of consonants in a string.
# Exercise 54
Exercise 55:#
Write a Python function celsius_to_fahrenheit(c)
that converts Celsius to Fahrenheit.
# Exercise 55
Exercise 56:#
Write a Python function is_perfect_square(n)
that checks if a number is a perfect square.
# Exercise 56
Exercise 57:#
Write a Python function is_anagram(s1, s2)
that checks if two strings are anagrams of each other.
# Exercise 57
Exercise 58:#
Write a Python function sum_of_cubes(n)
that returns the sum of cubes of the first n
natural numbers.
# Exercise 58
Exercise 59:#
Write a Python function remove_punctuation(s)
that removes all punctuation from a string.
# Exercise 59
Exercise 60:#
Write a Python function sum_even_digits(n)
that returns the sum of the even digits of a number.
# Exercise 60