The following exercises cover scenarios on functions in Python.
Exercise 1
Define a function named print_message
with zero parameters.
: print('Hello World')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 2
We defined a function print_message
. Make a call to the function.
def print_message(): print('Hello World')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 3
The function add
has two parameters. Return their sum.
def add(a, b): a + b
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
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))
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
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)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.