You are on page 1of 8

Python Functions Exercise

Exercise Question 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 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)

Expected Output:

After func1(20, 40, 60):

20

40

60

After func1(80, 100):


80

100

def func1(*args):

for i in args:

print(i)

func1(20, 40, 60)

func1(80, 100)

Explanation:

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

For example:

def calculation(a, b):


# Your Code

res = calculation(40, 10)

print(res)

A res should produce result 50, 30

Solution:

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)

Or

def calculation(a, b):

return a+b, a-b


add, sub = calculation(40, 10)

print(add)

print(sub)

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

Expected Output:

showEmployee("Ben", 9000)

showEmployee("Ben")

Should Produce:

Employee Ben salary is: 9000

Employee Ben salary is: 9000

Solution:

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 Question 5: Create an inner function to calculate the addition in the


following way

Create an outer function that will accept two parameters a and b

Create an inner function inside an outer function that will calculate the addition of
a and b

At last, an outer function will add 5 into addition and return it

Solution:

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 Question 6: Write a recursive function to calculate the sum of numbers


from 0 to 10

Expected Output:

55

def calculateSum(num):

if num:

return num + calculateSum(num-1)

else:

return 0

res = calculateSum(10)

print(res)

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

showStudent(name, age)

Solution:

def displayStudent(name, age):

print(name, age)

displayStudent("Emma", 26)

showStudent = displayStudent

showStudent("Emma", 26)

Exercise Question 8: Generate a Python list of all the even numbers between 4 to
30

Expected Output:
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Solution:

Use built-in function range().

print(list( range(4, 30, 2)))

Exercise Question 9: Return the largest item from the given list

aList = [4, 6, 8, 24, 12, 2]

Expected Output:

24

aList = [4, 6, 8, 24, 12, 2]

print(max(aList))

You might also like