## 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 ifif-elsefor 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

```def result(a, b):
sum = a+b
sub = a-b
mul = a*b
print(f"Sum is {sum}, Sub is {sub}, & Multiply is {mul}")

a = int(input("Enter value of a: "))  # 7
b = int(input("Enter value of b: "))  # 5
result(a,b)```

### 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

```def detail(roll):
x = [23,43,22,56]
if roll in x:
print(f"Roll number {roll} is present")
else:
print(f"Roll number {roll} is absent")
roll = int(input("Enter roll no. ")) # 24
detail(roll)

# output
# Roll number 24 is 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

```def max(a, b, c):
if a > b and a > c:
print(f"{a} is maximum among all")
elif b > a and b > c:
print(f"{b} is maximum among all")
else:
print(f"{c} is maximum among all")

max(30,22,18)```

### 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

```def func(x):
if x % 2 == 0:
print(f"{x} is Even number")
else:
print(f"{x} is Odd number")
x = int(input("Enter a number "))
func(4)```

### 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

```def count(val):
vov = 0
con = 0
for i in range(len(val)):
if val[i] in ['a','e','i','o','u']:
vov = vov+1
else:
con = con + 1

print("Count of vowels is ",vov)
print("Count of consonant is ",con)

x = input("Enter Value: ") # pythonlobby
count(x)```

### Exercise 6: Define a function that returns Factorial of a number.

Hint
Input:
Enter a number = 6

Expected output
Factorial is 720

```def factorial(num):
fact = 1
while(num!=0):
fact *= num
num = num - 1
print("Factorial is",fact)

num = int(input("Enter number "))
factorial(num)```

### Exercise 7: Define a function that accepts lowercase words and returns uppercase words.

Hint
Input:
Enter a word = pythonlobby

Expected output
Result is = PYTHONLOBBY

```def response(text):
z = text.upper()
print(z)

text = input("Enter String: ")  # pythonlobby
response(text)```

### Exercise 8: Define a function that accepts radius and returns the area of a circle.

Hint
Input:

Expected output
Area of a circle is  50.24

```def area(radius):
return area

### Exercise 9: What is the difference between local and global variable?

AnswerLocal 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.