Write a Python function to return the second smallest number in a list and print it.Example:# input# second_smallest([1, 2, -8, -2, 0])# output# -2


Topic: Write a Python function to return the second smallest number in a list and print it.Example:# input# second_smallest([1, 2, -8, -2, 0])# output# -2

Solution

def second_smallest(numbers):
    a1, a2 = float('inf'), float('inf')
    for x in numbers:
        if x <= a1:
            a1, a2 = x, a1
        elif x < a2:
            a2 = x
    return a2
print(second_smallest([1, 2, -8, -2, 0]))



List all Python Programs