Ad Code

Write a Python function to find max and min elements using (for/While) from a list and perform Addition

 Write a Python function to find max and min elements using (for/While) from a list and perform Addition


def find_max_min_and_sum(lst):
    if not lst:
        print("List is empty")
        return None

    # Initialize max and min to the first element of the list
    max_element = lst[0]
    min_element = lst[0]
    total_sum = 0

    # Iterate through the list to find max, min, and sum
    for num in lst:
        if num > max_element:
            max_element = num
        if num < min_element:
            min_element = num
        total_sum += num

    return max_element, min_element, total_sum

# Test the function
numbers = [5, 8, 3, 12, 6]
max_num, min_num, total_sum = find_max_min_and_sum(numbers)
print("Maximum element:", max_num)
print("Minimum element:", min_num)
print("Sum of all elements:", total_sum)
    

Reactions

Post a Comment

0 Comments