0% found this document useful (0 votes)
4 views2 pages

Python Functions Exercise Guide

The document contains a series of exercises focused on creating and using Python functions. Each exercise covers different aspects of functions, including defining functions, using arguments, keyword arguments, default values, return values, recursion, and handling multiple argument types. The exercises provide specific tasks to implement and test various function features in Python.

Uploaded by

charlesrockdecor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Functions Exercise Guide

The document contains a series of exercises focused on creating and using Python functions. Each exercise covers different aspects of functions, including defining functions, using arguments, keyword arguments, default values, return values, recursion, and handling multiple argument types. The exercises provide specific tasks to implement and test various function features in Python.

Uploaded by

charlesrockdecor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Functions - Exercise Sheet

Exercise 1: Simple Function


Write a function greet() that prints "Hello, world!" when called.
1. Define the function greet().
2. Call it after defining it.

Exercise 2: Function with Arguments


Write a function greet_person() that takes one argument name and prints "Hello [name]!" where
[name] is the name passed to the function.
1. Define the function greet_person(name).
2. Call the function with different names (e.g., "Alice", "Bob", "Charlie").

Exercise 3: Variable Number of Arguments


Create a function display_names() that can accept a variable number of names and print each of
them.
1. Use *args to accept an indefinite number of parameters.
2. Call the function with different numbers of names (e.g., display_names("Alice", "Bob")
and display_names("Charlie", "David", "Eve")).

Exercise 4: Keyword Arguments


Modify the display_names() function to accept arguments as keyword arguments and print a
message like "The first name is [name1], the second is [name2], etc."
1. Use **kwargs to accept arguments as a dictionary.
2. Call the function with keyword arguments (e.g., display_names(name1="Alice",
name2="Bob")).

Exercise 5: Default Parameter Value


Create a function say_hello() that takes an argument language with a default value of "French". If
the argument language is not passed, the function should print "Bonjour!", otherwise, it should
print "Hello!" if the language is English.
1. Define the function say_hello(language="French").
2. Call it with different arguments (e.g., say_hello("English") and say_hello()).

Exercise 6: Return Values


Create a function add(a, b) that takes two arguments a and b and returns their sum.
1. Define the function add(a, b).
2. Call the function with two numbers (e.g., add(3, 5) and add(10, 7)).
Exercise 7: Passing a List as an Argument
Create a function display_elements() that takes a list of numbers as an argument and prints each
element of the list.
1. Define the function display_elements(list).
2. Pass a list as an argument (e.g., display_elements([1, 2, 3, 4])).

Exercise 8: Recursive Function


Create a recursive function factorial(n) that calculates the factorial of n (the product of all
integers from 1 to n).
1. Define the function factorial(n).
2. Call the function with different arguments (e.g., factorial(5) and factorial(3)).

Exercise 9: Positional and Keyword Arguments


Create a function calculate_volume() that takes 2 positional arguments length and width, and 1
keyword argument height with a default value of 10.
1. Define the function calculate_volume(length, width, height=10).
2. Call the function with and without the height parameter (e.g., calculate_volume(5, 3) and
calculate_volume(5, 3, height=12)).

Exercise 10: Positional-Only and Keyword-Only Arguments


Create a function display_info() that takes one positional argument name and one keyword-only
argument age.
1. Define the function display_info(name, *, age).
2. Call the function with name as a positional argument and age as a keyword argument
(e.g., display_info("Alice", age=30)).

Exercise 11: Function with Multiple Argument Types


Create a function process_data() that takes:
• one positional argument user_id,
• a variable number of positional arguments *data,
• and keyword arguments **options (e.g., option1="value").
1. Define the function process_data(user_id, *data, **options).
2. Print user_id, then the elements of *data, then the values from **options.
3. Test the function with different arguments:
a. process_data(1, "data1", "data2", option1="A", option2="B")
b. process_data(2, "data3", option1="C").

You might also like