Python Function Exercises with Solution
Python Function Exercises: Python functions are the reusable block of codes. We can define python functions to perform a specific operation on a function call. The use of functions in any program reduces the coding effort and also improves the reusability of code.
In case, while working on large projects, it is a good practice to use the concepts of function.
The main idea behind solving these questions is to make your concept more clear and improve logical thinking of how to approach a problem in a functional way. We are going to cover python functions with some mix of conditional statements i.e if, if-else, for loop(), range(), while loop() etc.
For reference
Function Exercises in Python:
We will solve 15 function exercises in python with a solution & detailed code explanation.
Exercise 1: Define a function that accepts 2 values and return its sum, subtraction and multiplication.
Hint
Input:
Enter value of a = 7
Enter value of b = 5
Expected output
Sum is = 12
Sub is = 2
Multiplication is = 35
Exercise 2: Define a function that accepts roll number and returns whether the student is present or absent.
Hint
Use a list to store sample roll. no.
Get input from a user and check if the number
exist in the list or not, if exists return present
else return absent
Exercise 3: Define a function in python that accepts 3 values and returns the maximum of three numbers.
Hint
Input:
Value of a = 30
Value of b = 22
value of c = 18
Expected output
30 is the maximum among all
Exercise 4: Define a function that accepts a number and returns whether the number is even or odd.
Hint
Input:
Enter a number = 4
Expected output
4 is an even number
Hint
Input:
Enter a number = 5
Expected output
4 is an odd number
Exercise 5: Define a function which counts vowels and consonant in a word.
Hint
Input:
Enter a word = pythonlobby
Expected output
Count of vowel is = 2
Count of consonant is = 9
Exercise 6: Define a function that returns Factorial of a number.
Hint
Input:
Enter a number = 6
Expected output
Factorial is 720
Exercise 7: Define a function that accepts lowercase words and returns uppercase words.
Hint
Input:
Enter a word = pythonlobby
Expected output
Result is = PYTHONLOBBY
Exercise 8: Define a function that accepts radius and returns the area of a circle.
Hint
Input:
Enter radius 4
Expected output
Area of a circle is 50.24
Exercise 9: What is the difference between local and global variable?
Answer: Local variable is defined inside the function whereas global variable is defined or declared outside of the function. A local variable is always created during the start of function execution. And is destroyed automatically when the function terminates.
And global variable is created when execution starts and terminates when the program execution ends. A global variable is common for all program members but the scope of the local variable is limited to its own function only where it is defined.
Exercise 10: What is the scope of a variable (in function)?
Answer: If we are talking about scope, it means we are referring to the visibility of a variable in function. If a variable is defined inside the function then it is called a local variable and its scope is limited to its own function only.
And if the variable is defined outside of the function then it is said to be a global variable that is accessible for every member and its scope is available throughout the program.
Exercise 11: What is the difference between a parameter and an argument?
Answer: Likewise parameter and arguments are similar terms but there is a slight difference between both of them. Parameters are may be a variable or a value passed through the parenthesis of a function.
And the argument is the value to the variable that is sent to the function when it is called.
Exercise 12: Name three iterable object in Python?
Answer: Iterable objects are the objects whose values or elements can be iterable using a loop i.e. for loop or while loop etc. Three types of iterable object in python are list, tuples and dictionaries etc.
Exercise 13: What does function returns by default in Python?
Answer: A function should always return a value. If we do not explicitly return a value or statement after the execution of a function then it returns a default value as None implicitly.