Functions in Python: 10 problems solved
Functions are an essential part of any programming language, and Python is no exception. They allow you to encapsulate a block of code that can be reused and called multiple times. In this article, we'll explore ten different types of functions in Python, from the basic syntax to more advanced concepts like polymorphism and recursion.
1. Basic Function Syntax
Problem Statement:
Write a function to calculate and return the square of a number.
Solution:
def square_of_num(number):
return number ** 2
result = square_of_num(2)
print(result) # Output: 4
2. Function with Multiple Parameters
Problem Statement:
Create a function that takes two numbers as parameters and returns their sum.
Solution:
def sum_of_num(num1, num2):
return num1 + num2
result = sum_of_num(2, 3)
print(result) # Output: 5
3. Polymorphism in Functions
Problem Statement:
Write a function multiply that multiplies two numbers, but can also accept and multiply strings.
Solution:
def multiply(first, second):
return first * second
print(multiply(8, 5)) # Output: 40
print(multiply("hruthik", 5)) # Output: hruthikhruthikhruthikhruthikhruthik
print(multiply(5, "hruthik")) # Output: hruthikhruthikhruthikhruthikhruthik
4. Function Returning Multiple Values
Problem Statement:
Create a function that returns both the area and circumference of a circle given its radius.
Solution:
import math
def circle_stats(radius):
area = round(math.pi * radius ** 2, 2)
circumference = round(2 * math.pi * radius, 2)
return area, circumference
area, circumference = circle_stats(5)
print("Area: ", area, "circumference: ", circumference)
5. Default Parameter Value
Problem Statement:
Write a function that greets a user. If no name is provided, it should greet with a default name.
Solution:
def greet(name = "alex"):
return "Hello, " + name + "!"
print(greet("Hruthik")) # Output: Hello, Hruthik!
print(greet()) # Output: Hello, alex!
6. Lambda Function
Problem Statement:
Create a lambda function to compute the cube of a number.
Solution:
cube = lambda x: x ** 3
print(cube(2)) # Output: 8
7. Function with *args
Problem Statement:
Write a function that takes variable number of arguments and returns their sum.
Solution:
def sum_all(*args):
return sum(args)
print(sum_all(1, 1)) # Output: 2
print(sum_all(1, 1, 2)) # Output: 4
8. Function with **kwargs
Problem Statement:
Create a function that accepts any number of keyword arguments and prints them in the format key: value.
Solution:
def print_kwargs(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_kwargs(name="Gojo", power="Red") # Output: name: Gojo, power: Red
print_kwargs(name="Gojo", power="Red", enemy="Sukuna") # Output: name: Gojo, power: Red, enemy: Sukuna
print_kwargs(name="Itadori") # Output: name: Itadori
9. Generator Function with yield
Problem Statement:
Write a generator function that yields even numbers up to a specified limit.
Solution:
def generate_even_num(limit):
for i in range(2, limit+1, 2):
yield i
for num in generate_even_num(10):
print(num) # Output: 2, 4, 6, 8, 10
10. Recursive Function
Problem Statement:
Create a recursive function to calculate the factorial of a number.
Solution:
def factorial(number):
if number == 0:
return 1
else:
return number * factorial(number - 1)
print(factorial(5)) # Output: 120