Exercises on Python Functions

The following exercises cover scenarios on functions in Python.

Exercise 1

Define a function named print_message with zero parameters.

:
    print('Hello World')

Exercise 2

We defined a function print_message. Make a call to the function.

def print_message():
    print('Hello World')

Exercise 3

The function add has two parameters. Return their sum.

def add(a, b):
     a + b

Exercise 4

Define the function add such that it can take any number of arguments and return their sum.

def add(nums):
    sum = 0
    for num in nums:
        sum += num
    return sum

print(add(1, 4, 2, 0, 6))

Exercise 5

Define the function add such that it can take any number of keyword arguments and print them to standard output.

def add(args):
    print(args)

add(a=1, b=2)
Code copied to clipboard successfully 👍