Find Middle Number In Python

The provided Python code defines a function called find_middle_number designed to determine the middle number in a list of integers. The function begins by checking if the input list, referred to as numbers, is empty. If the list is not empty, the function proceeds to sort the numbers in ascending order using the sorted function. Following the sorting step, the code calculates the middle index of the sorted list by utilizing the floor division operator (//). If the length of the list is odd, the function returns the middle number directly. However, if the length is even, the code returns the middle-right number. It’s important to note that the behavior for even-length lists can be adjusted to return the middle-left number if desired. An example usage section demonstrates how to apply the function to a list of numbers and prints the result, showcasing the middle number in the provided list.


Source code

def find_middle_number(numbers):
    """
    Find the middle number in a list of numbers.

    Parameters:
    - numbers (list): A list of numbers.

    Returns:
    - middle_number: The middle number.
    """
    if not numbers:
        return None

    sorted_numbers = sorted(numbers)
    middle_index = len(sorted_numbers) // 2

    # If the length of the list is odd, return the middle number
    # If the length is even, you may choose to return the middle-left or middle-right number.
    return sorted_numbers[middle_index]

# Example usage:
numbers_list = [4, 2, 8, 1, 7, 5]
middle_number = find_middle_number(numbers_list)

print(f"The middle number is: {middle_number}")

Description

  1. Function Definition def find_middle_number(numbers): The code begins by defining a function named find_middle_number that takes a single parameter, numbers, which is expected to be a list of numbers.
  2. Check for Empty List:if not numbers: return None This block of code checks if the input list (numbers) is empty. If it is, the function immediately returns None, as there is no middle number to find in an empty list.
  3. Sort the List:sorted_numbers = sorted(numbers) The function sorts the input list (numbers) in ascending order using the sorted function and assigns the result to the variable sorted_numbers.
  4. Calculate Middle Index:middle_index = len(sorted_numbers) // 2 The code calculates the middle index of the sorted list by performing floor division (//) on the length of the list. This index will be used to extract the middle number.
  5. Return the Middle Number:return sorted_numbers[middle_index] If the length of the sorted list is odd, the function returns the middle number directly. If the length is even, it returns the middle-right number. This behavior can be adjusted based on the desired outcome.
  6. Example Usage:numbers_list = [4, 2, 8, 1, 7, 5] middle_number = find_middle_number(numbers_list) print(f"The middle number is: {middle_number}") This section demonstrates how to use the find_middle_number function with an example list (numbers_list). It calculates the middle number and prints the result.

In summary, this code defines a function that finds the middle number in a list of numbers. It checks for an empty list, sorts the numbers, calculates the middle index, and returns the middle number. The example usage illustrates its application on a specific list.


Screenshot


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *