Write a function to return the second largest number from a list of numbers.
Question
Write a function to return the second largest number from a list of numbers.
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here's a function to return the second largest number from a list of numbers:
def second_largest(numbers):
# Sort the list in descending order
sorted_numbers = sorted(numbers, reverse=True)
# Return the second element in the sorted list
return sorted_numbers[1]
# Example usage:
numbers = [10, 20, 4, 45, 99]
result = second_largest(numbers)
print("The second largest number is:", result)
Explanation:
Sorting: The list is sorted in descending order.
Second Element: The second element in this sorted list will be the second largest number.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month