Problems on Function
1. Print a Greeting Message
Problem: Write a function greet()
that prints "Hello, Python!"
.
Explanation: A simple function with no parameters.
Example Output:
Hello, Python!
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)
Output:
12
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)
Output:
15
4. Function to Check Even or Odd
Problem: Write a function is_even(n)
that returns "Even"
if the number is even and "Odd"
if it is odd.
Example Input:
is_even(7)
Output:
Odd
5. Function to Calculate Factorial
Problem: Write a function factorial(n)
to calculate factorial of a number using a loop.
Example Input:
factorial(5)
Output:
120
6. Function to Check Prime Number
Problem: Write a function is_prime(n)
that returns True
if a number is prime, otherwise False
.
Example Input:
is_prime(13)
Output:
True
7. Function to Find Sum of List
Problem: Write a function sum_list(lst)
that returns the sum of all numbers in a list.
Example Input:
sum_list([2, 4, 6])
Output:
12
8. Function to Reverse a String
Problem: Write a function reverse_string(s)
that returns the reversed string.
Example Input:
reverse_string("Python")
Output:
nohtyP
9. Function with Default Argument
Problem: Write a function greet_person(name="Guest")
that prints "Hello, <name>!"
.
Example Input:
greet_person("Alice")
Output:
Hello, Alice!
Example Input (without argument):
greet_person()
Output:
Hello, Guest!
10. Function to Count Vowels in String
Problem: Write a function count_vowels(s)
that returns the number of vowels in a string.
Example Input:
count_vowels("Python is fun")
Output:
4
11. Function to Find Maximum in a List
Problem: Write a function max_in_list(lst)
that returns the largest number in a list.
Example Input:
max_in_list([4,7,2,9,5])
Output:
9
12. Function to Calculate Power
Problem: Write a function power(base, exp)
that returns base
raised to the power of exp
.
Example Input:
power(2, 3)
Output:
8
13. Function to Count Words in a Sentence
Problem: Write a function count_words(sentence)
that returns the number of words in a sentence.
Example Input:
count_words("Python is fun to learn")
Output:
5
14. Function to Check Armstrong Number
Problem: Write a function is_armstrong(n)
that returns True
if a number is an Armstrong number, otherwise False
.
Example Input:
is_armstrong(153)
Output:
True
15. Function to Check Palindrome String
Problem: Write a function is_palindrome(s)
that returns True
if a string is a palindrome, otherwise False
.
Example Input:
is_palindrome("radar")
Output:
True
16. Function to Count Occurrences of Character
Problem: Write a function count_char(s, ch)
that counts the number of times a character occurs in a string.
Example Input:
count_char("banana", "a")
Output:
3
17. Function to Merge Two Lists
Problem: Write a function merge_lists(lst1, lst2)
that returns a combined list.
Example Input:
merge_lists([1,2,3], [4,5])
Output:
[1,2,3,4,5]
18. Function to Remove Duplicates from List
Problem: Write a function remove_duplicates(lst)
that returns a list with duplicates removed.
Example Input:
remove_duplicates([1,2,2,3,3,3])
Output:
[1,2,3]
19. Function to Reverse a List
Problem: Write a function reverse_list(lst)
that returns the reversed list.
Example Input:
reverse_list([1,2,3,4])
Output:
[4,3,2,1]
20. Function to Find Palindrome Numbers in a List
Problem: Write a function palindrome_numbers(lst)
that returns a list of all palindrome numbers from a given list.
Example Input:
palindrome_numbers([121,123,454,567,787])
Output:
[121,454,787]