Solution on Function

October 7, 2025

1. Print a Greeting Message

Problem: Write a function greet() that prints "Hello, Python!".
Solution:

def greet():
    print("Hello, Python!")

greet()


2. Function to Add Two Numbers

Problem: Write a function add(a, b) that returns the sum of two numbers.
Example Input: add(5, 7)12
Solution:

def add(a, b):
    return a + b

print(add(5, 7))


3. Function to Find Maximum of Two Numbers

Problem: Write a function maximum(a, b) that returns the larger number.
Example Input: maximum(10, 15)15
Solution:

def maximum(a, b):
    if a > b:
        return a
    else:
        return b

print(maximum(10, 15))


4. Function to Check Even or Odd

Problem: Write a function is_even(n) that returns "Even" or "Odd".
Example Input: is_even(7)"Odd"
Solution:

def is_even(n):
    if n % 2 == 0:
        return "Even"
    else:
        return "Odd"

print(is_even(7))


5. Function to Calculate Factorial

Problem: Write a function factorial(n) using a loop.
Example Input: factorial(5)120
Solution:

def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print(factorial(5))


6. Function to Check Prime Number

Problem: Write a function is_prime(n) that returns True if prime, else False.
Example Input: is_prime(13)True
Solution:

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

print(is_prime(13))


7. Function to Find Sum of List

Problem: Write a function sum_list(lst) to sum all numbers.
Example Input: [2, 4, 6]12
Solution:

def sum_list(lst):
    total = 0
    for num in lst:
        total += num
    return total

print(sum_list([2, 4, 6]))


8. Function to Reverse a String

Problem: Write a function reverse_string(s) that returns reversed string.
Example Input: "Python""nohtyP"
Solution:

def reverse_string(s):
    return s[::-1]

print(reverse_string("Python"))


9. Function with Default Argument

Problem: Write greet_person(name="Guest") that prints "Hello, <name>!".
Solution:

def greet_person(name="Guest"):
    print(f"Hello, {name}!")

greet_person("Alice")
greet_person()


10. Function to Count Vowels in String

Problem: Write a function count_vowels(s) returning number of vowels.
Example Input: "Python is fun"4
Solution:

def count_vowels(s):
    vowels = "aeiouAEIOU"
    count = 0
    for char in s:
        if char in vowels:
            count += 1
    return count

print(count_vowels("Python is fun"))


11. Function to Find Maximum in a List

Problem: Write max_in_list(lst) returning the largest number.
Example Input: [4,7,2,9,5]9
Solution:

def max_in_list(lst):
    max_num = lst[0]
    for num in lst:
        if num > max_num:
            max_num = num
    return max_num

print(max_in_list([4,7,2,9,5]))


12. Function to Calculate Power

Problem: Write power(base, exp) returning base^exp.
Example Input: power(2,3)8
Solution:

def power(base, exp):
    result = 1
    for _ in range(exp):
        result *= base
    return result

print(power(2,3))


13. Function to Count Words in a Sentence

Problem: Write count_words(sentence) returning number of words.
Example Input: "Python is fun to learn"5
Explanation: Words are separated by spaces.
Solution:

def count_words(sentence):
    words = 0
    for char in sentence:
        if char == " ":
            words += 1
    words += 1
    return words

print(count_words("Python is fun to learn"))


14. Function to Check Armstrong Number

Problem: Write is_armstrong(n) that returns True if a number is Armstrong.

Example Input: 153True
Explanation: Sum of cubes of digits equals number (1³+5³+3³=153).
Solution:

def is_armstrong(n):
    total = 0
    temp = n
    while temp > 0:
        digit = temp % 10
        total += digit ** 3
        temp //= 10
    return total == n

print(is_armstrong(153))


15. Function to Check Palindrome String

Problem: Write is_palindrome(s) to check if string is palindrome.
Example Input: "radar"True
Solution:

def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("radar"))


16. Function to Count Occurrences of Character

Problem: Write count_char(s, ch) counting occurrences of ch in string s.
Example Input: "banana", "a"3
Solution:

def count_char(s, ch):
    count = 0
    for c in s:
        if c == ch:
            count += 1
    return count

print(count_char("banana", "a"))


17. Function to Merge Two Lists

Problem: Write merge_lists(lst1, lst2) returning combined list.
Example Input: [1,2,3],[4,5][1,2,3,4,5]
Solution:

def merge_lists(lst1, lst2):
    return lst1 + lst2

print(merge_lists([1,2,3],[4,5]))


18. Function to Remove Duplicates from List

Problem: Write remove_duplicates(lst) returning list without duplicates.
Example Input: [1,2,2,3,3,3][1,2,3]
Solution:

def remove_duplicates(lst):
    return list(set(lst))

print(remove_duplicates([1,2,2,3,3,3]))


19. Function to Reverse a List

Problem: Write reverse_list(lst) returning reversed list.
Example Input: [1,2,3,4][4,3,2,1]
Solution:

def reverse_list(lst):
    return lst[::-1]

print(reverse_list([1,2,3,4]))


20. Function to Find Palindrome Numbers in a List

Problem: Write palindrome_numbers(lst) returning palindromes from list.
Example Input: [121,123,454,567,787][121,454,787]
Solution:

def palindrome_numbers(lst):
    result = []
    for num in lst:
        if str(num) == str(num)[::-1]:
            result.append(num)
    return result

print(palindrome_numbers([121,123,454,567,787]))


© Copyright 2025 Fessorpro

Terms of Service / Privacy Policy