Python function is a code block or group of statements that perform a particular task. We use reuse functions whenever required.
This Python functions exercise aims to help Python developers to learn and practice how to create a function, nested functions, and use the function arguments effectively. All questions are tested on Python 3.
This exercise includes the following: –
- It contains 10 questions and solutions provided for each question.
- Coding exercise to solve Python function assignments, programs, questions, and challenges.
This function exercise covers questions on concepts such as function creation, function calls, function arguments, inner functions, built-in functions. Let us know if you have any alternative solutions. It will help other developers.
Exercise 1: Create a function that can accept two arguments name and age and print its value
def demo(name, age): print(name, age) demo("Ben", 25)
Exercise 2: Write a function
func1() such that it can accept a variable length of argument and print all arguments value
func1(20, 40, 60) func1(80, 100)
func1(20, 40, 60) 20 40 60 func1(80, 100) 80 100
def func1(*args): for i in args: print(i) func1(20, 40, 60) func1(80, 100)
To accept Variable Length of Positional Arguments, i.e., To create functions that take n number of Positional arguments we use
*args (prefix a parameter name with an asterisk
Exercise 3: Write a function calculation() such that it can accept two variables and calculate the addition and subtraction of them. And also it must return both addition and subtraction in a single return call
def calculation(a, b): # Your Code res = calculation(40, 10) print(res)
In Python, we can return multiple values from a function. You can do this by separating return values with a comma.
def calculation(a, b): return a+b, a-b res = calculation(40, 10) print(res)
def calculation(a, b): return a+b, a-b add, sub = calculation(40, 10) print(add) print(sub)
Exercise 4: Create a function showEmployee() in such a way that it should accept employee name, and its salary and display both. If the salary is missing in the function call assign default value 9000 to salary
showEmployee("Ben", 9000) showEmployee("Ben")
Employee Ben salary is: 9000
Employee Ben salary is: 9000
In Python, we can specify default values for arguments when defining a function.
def showEmployee(name, salary=9000): print("Employee", name, "salary is:", salary) showEmployee("Ben", 9000) showEmployee("Ben")
Exercise 5: Create an inner function to calculate the addition in the following way
- Create an outer function that will accept two parameters,
- Create an inner function inside an outer function that will calculate the addition of
- At last, an outer function will add 5 into addition and return it
In Python, we can create a nested function inside a function. We can use the nested function to perform complex tasks multiple times within another function or avoid loop and code duplication.
def outerFun(a, b): square = a**2 def innerFun(a,b): return a+b add = innerFun(a, b) return add+5 result = outerFun(5, 10) print(result)
Exercise 6: Write a recursive function to calculate the sum of numbers from 0 to 10
def calculateSum(num): if num: return num + calculateSum(num-1) else: return 0 res = calculateSum(10) print(res)
Exercise 7: Assign a different name to function and call it through the new name
Below is the function
displayStudent(name, age). Assign a new name
showStudent(name, age) to it and call through the new name
def displayStudent(name, age): print(name, age) displayStudent("Emma", 26)
You should be able to call the same function using
def displayStudent(name, age): print(name, age) displayStudent("Emma", 26) showStudent = displayStudent showStudent("Emma", 26)
Exercise 8: Generate a Python list of all the even numbers between 4 to 30
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
print(list( range(4, 30, 2)))
Exercise 9: Return the largest item from the given list
aList = [4, 6, 8, 24, 12, 2]
aList = [4, 6, 8, 24, 12, 2] print(max(aList))