Coding Questions on Functions in Python
Python functions make programs easier to organize by allowing a specific task to be placed inside a reusable block of code. The following coding exercises cover basic functions, arguments, return values, lambda expressions, and functional tools such as map(), filter(), and reduce().
Table of Contents

YT:- DecodeIT
1. Basic Function Practice
a. Create an is_even() Function
Write a function that determines whether the supplied number is even. The remainder operator can be used to check whether the number is completely divisible by 2.
def is_even(number):
return number % 2 == 0
# Example usage:
print(is_even(4)) # Output: True
print(is_even(7)) # Output: False
b. Create a factorial() Function
Develop a function that calculates the factorial of a number using a for loop. Start with a result of 1 and multiply it by each number in the required range.
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
# Example usage:
print(factorial(5)) # Output: 120
print(factorial(0)) # Output: 1
2. Working with Function Arguments
a. Use Keyword Arguments with greet_user()
Create a function that accepts a first name and last name and displays a greeting. When calling the function, pass the values using their parameter names.
def greet_user(first_name, last_name):
print(f"Hello, {first_name} {last_name}!")
# Example usage:
greet_user(first_name="John", last_name="Doe")
# Output:
# Hello, John Doe!
b. Use a Default Argument with calculate_area()
Create a function that calculates the area of a circle. The pi parameter has a default value, but another value can be supplied when required.
def calculate_area(radius, pi=3.14159):
return pi * radius * radius
# Example usage:
print(calculate_area(5))
# Output: 78.53975
print(calculate_area(5, 3.14))
# Output: 78.5
3. Functions That Return Values
a. Find the Largest Value with find_max()
Write a function that searches through a list and returns its largest number. If the list contains no elements, the function returns None.
def find_max(numbers):
if not numbers:
return None
max_value = numbers[0]
for number in numbers:
if number > max_value:
max_value = number
return max_value
# Example usage:
print(find_max([1, 2, 3, 4, 5]))
# Output: 5
print(find_max([-10, -20, -30]))
# Output: -10
b. Reverse Text with reverse_string()
Create a function that receives a string and returns the characters in reverse order. Python slicing provides a short way to accomplish this task.
def reverse_string(s):
return s[::-1]
# Example usage:
print(reverse_string("hello"))
# Output: olleh
print(reverse_string("world"))
# Output: dlrow
4. Lambda Function Exercises
a. Generate Squares Using map()
A lambda expression can be combined with map() to apply the same calculation to every value in a sequence. In this example, each number from 1 through 10 is squared.
squares = list(
map(lambda x: x ** 2, range(1, 11))
)
print(squares)
# Output:
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b. Select Words Using filter()
The filter() function can be used with a lambda expression to keep only the words that satisfy a condition. Here, words containing at least five characters are selected.
words = ["apple", "banana", "pear", "kiwi", "grape"]
long_words = list(
filter(lambda word: len(word) >= 5, words)
)
print(long_words)
# Output:
# ['apple', 'banana', 'grape']
5. Advanced Lambda-Based Operations
a. Calculate a Product Using reduce()
The reduce() function can repeatedly apply a calculation to the elements of a sequence. In this example, every number in the list is multiplied together.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(
lambda x, y: x * y,
numbers
)
print(product)
# Output:
# 120
b. Combine filter() and map()
You can combine functional tools to perform multiple operations in sequence. The following example first selects even numbers from 1 to 20 and then calculates the square of each selected value.
squares_of_even_numbers = list(
map(
lambda x: x ** 2,
filter(lambda x: x % 2 == 0, range(1, 21))
)
)
print(squares_of_even_numbers)
# Output:
# [4, 16, 36, 64, 100, 144, 196, 256, 324, 400]
Final Thoughts
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
These exercises provide practical practice with some of the most useful concepts related to Python functions. Starting with simple functions such as is_even() and factorial() helps build the foundation before moving toward arguments, return values, lambda expressions, and functional programming tools.
Practicing these examples by changing the input values and conditions can help you understand how Python functions behave in real programs.
Frequently Asked Questions
1. What is a function in Python?
A function is a reusable block of Python code designed to perform a particular task.
2. What is a lambda function?
A lambda function is a small anonymous function that can be written as a single expression and is often useful with tools such as map() and filter().
3. What does map() do in Python?
map() applies a specified function to each item in an iterable and produces the resulting values.
4. What is the purpose of filter()?
filter() selects elements from an iterable according to a condition defined by a function.
5. Where is reduce() available?
reduce() is provided by Python’s functools module and can be used to combine sequence elements into a single result.
Keywords: Python function coding questions, Python functions examples, coding questions in Python, Python function practice, Python lambda function questions, map filter reduce Python, Python coding exercises, Python function arguments, Python return value examples