Now that you understand the concepts behind defining and using functions, it's time to put that knowledge into practice. The best way to solidify your understanding is by writing code yourself. These exercises will guide you through creating and calling functions, handling parameters, returning values, and using default arguments. Remember, functions help make your code organized, reusable, and easier to understand.
Goal: Define a function that prints a simple greeting message and then call it.
greet_user
. Inside this function, use the print()
function to display the message "Hello! Welcome to the world of Python functions."greet_user()
to execute the code inside it.# Define the function here
def greet_user():
print("Hello! Welcome to the world of Python functions.")
# Call the function here
greet_user()
Expected Output:
Hello! Welcome to the world of Python functions.
This first exercise demonstrates the basic structure: using def
to define the function and calling it by its name followed by parentheses.
Goal: Create a function that accepts a name as an argument and prints a personalized greeting.
greet_personalized
that accepts one parameter, let's call it name
.[name]
is replaced by the value passed to the function.greet_personalized()
multiple times, passing different names as arguments (e.g., "Alice", "Bob").# Define the function here
def greet_personalized(name):
print(f"Hello, {name}! It's nice to meet you.")
# Call the function with different arguments
greet_personalized("Alice")
greet_personalized("Bob")
greet_personalized("Charlie")
Expected Output:
Hello, Alice! It's nice to meet you.
Hello, Bob! It's nice to meet you.
Hello, Charlie! It's nice to meet you.
This exercise shows how parameters allow functions to operate on different data provided during the call.
Goal: Write a function that calculates the area of a rectangle and returns the result.
calculate_rectangle_area
that takes two parameters: width
and height
.return
statement to send the calculated area back to the part of the program that called the function.calculate_rectangle_area()
with sample dimensions (e.g., width=10, height=5). Store the returned value in a variable named area_result
.area_result
.# Define the function here
def calculate_rectangle_area(width, height):
area = width * height
return area
# Call the function and store the result
rect_width = 10
rect_height = 5
area_result = calculate_rectangle_area(rect_width, rect_height)
# Print the result
print(f"The area of a rectangle with width {rect_width} and height {rect_height} is: {area_result}")
# You can also use the returned value directly
print(f"Another rectangle (8x3) area is: {calculate_rectangle_area(8, 3)}")
Expected Output:
The area of a rectangle with width 10 and height 5 is: 50
Another rectangle (8x3) area is: 24
This demonstrates how return
allows functions to produce results that can be used elsewhere in your code.
Goal: Create a function that calculates the power of a number, with a default exponent of 2 (squaring the number).
power
that takes two parameters: base
and exponent
. Provide a default value of 2
for the exponent
parameter.base
raised to the power of exponent
(you can use the **
operator).power
function providing only the base
argument (e.g., power(5)
). This should use the default exponent.power
function providing both base
and exponent
arguments (e.g., power(3, 3)
).# Define the function with a default argument
def power(base, exponent=2):
result = base ** exponent
return result
# Call the function using the default exponent
number_squared = power(5)
print(f"5 squared (using default exponent) is: {number_squared}")
# Call the function providing both arguments
number_cubed = power(3, 3)
print(f"3 cubed (providing exponent) is: {number_cubed}")
Expected Output:
5 squared (using default exponent) is: 25
3 cubed (providing exponent) is: 27
This exercise illustrates the convenience of default arguments, making functions more flexible.
These exercises cover the fundamental aspects of creating and using functions in Python. Try modifying them or creating your own functions based on these patterns. Experimenting is a great way to learn! As your programs grow, functions will become indispensable tools for managing complexity.
© 2025 ApX Machine Learning