Python function is a code block or group of statements that perform a particular task. We use Python functions for code reuse so we can use it repeatedly in a program. 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.
This exercise will help you to improve your understanding of the Python functions. All questions are tested on Python 3.
What included in this Python functions exercise?
The exercise contains 10 questions and solutions provided for each question. This Python function coding exercise is nothing but Python function assignments to solve, where you can solve and practice different function programs, questions, problems, and challenges.
Each question includes a specific function related topic you need to learn. This function exercise covers questions on topics 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.
Practice Questions using Online Code Editor
Exercise Question 1: Create a function that can accept two arguments name and age and print its value
Exercise Question 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):
Exercise Question 3: Write a function
calculation() such that it can accept two variables and calculate the addition and subtraction of it. 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)
A res should produce result 50, 30
Exercise Question 4: Create a function
showEmployee() in such a way that it should accept employee name, and it’s salary and display both, and if the salary is missing in function call it should show it as 9000
showEmployee("Ben", 9000) showEmployee("Ben")
Employee Ben salary is: 9000
Employee Ben salary is: 9000
Exercise Question 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
Exercise Question 6: Write a recursive function to calculate the sum of numbers from 0 to 10
Exercise Question 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
Exercise Question 8: Generate a Python list of all the even numbers between 4 to 40
Expected Output:[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
Exercise Question 9: Return the largest item from the given list
aList = [4, 6, 8, 24, 12, 2]