Function to Find Array's Min and Max Values - Python Programming Exercise

In this exercise, you will develop a Python program that defines a function to find the smallest and largest numbers in a list. The function should accept the list as an input and use reference parameters to return the results. This exercise is perfect for practicing function definition, list manipulation, and comparison operations in Python. By implementing this function, you will gain hands-on experience in handling function definitions, list manipulation, and comparison operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Find Array's Min And Max Values

 Objective

Develop a Python program that defines a function to find the smallest and largest numbers in a list. The function should accept the list as an input and use reference parameters to return the results. For example, if you pass a list like data = [1.5, 0.7, 8.0], after the function call, the minimum variable will hold the value 0.7 and the maximum will hold the value 8.0.

 Example Python Exercise

 Copy Python Code
# Function to find the smallest and largest numbers in the list
def find_min_max(data, min_value, max_value):
    # Initialize min_value and max_value to the first element in the list
    min_value[0] = data[0]  # Set the first element as the initial min_value
    max_value[0] = data[0]  # Set the first element as the initial max_value

    # Iterate through the list to find the smallest and largest numbers
    for num in data:
        if num < min_value[0]:
            min_value[0] = num  # Update min_value if current number is smaller
        if num > max_value[0]:
            max_value[0] = num  # Update max_value if current number is larger

# Example list of numbers
data = [1.5, 0.7, 8.0]

# Initialize the reference variables for min and max values
min_value = [float('inf')]  # Start with a very large number as initial min_value
max_value = [float('-inf')]  # Start with a very small number as initial max_value

# Call the function with the list and reference variables
find_min_max(data, min_value, max_value)

# Print the results
print(f"The smallest number is: {min_value[0]}")  # Min value after function call
print(f"The largest number is: {max_value[0]}")  # Max value after function call

 Output

python min_max.py
The smallest number is: 0.7
The largest number is: 8.0

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Recursive Function to Invert a Sequence

    In this exercise, you will develop a Python program that utilizes recursion to reverse a sequence of characters. This exercise is perfect for practicing functi...

  •  Function to Display a Rectangle

    In this exercise, you will develop a Python program that defines a function to print a filled rectangle on the screen, where the dimensions (width and height) are pro...

  •  Iterative Palindrome Function

    In this exercise, you will develop a Python program with an iterative function to check if a string is a palindrome (symmetric). This exercise is perfect for p...

  •  Recursive Palindrome Function

    In this exercise, you will develop a Python program with a recursive function to determine if a string is symmetric (a palindrome). This exercise is perfect fo...

  •  Function FindMinMax

    In this exercise, you will develop a Python program with a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum v...

  •  Function Multiply & MultiplyRecursive

    In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The f...