Write a function to return the second largest number from a list of numbers.
Function to Find Second Largest Number:
python def secondLargest(list1): # Sort the list in ascending order list1.sort() # Return the second last element return list1[-2]
Explanation: The function secondLargest accepts a list as a parameter. The sort() method arranges all elements in ascending order. After sorting, the largest number is at the last position (index -1), and the second largest is at the second last position (index -2).
Complete Program with Output: python def secondLargest(list1): list1.sort() return list1[-2]
Main program
numbers = [10, 20, 4, 45, 99] print("Second largest number is:", secondLargest(numbers))
Output:
Second largest number is: 45
Note: The sort() method modifies the original list. To preserve the original list, use sorted(list1) which returns a new sorted list.
Explanation
The textbook context shows similar function structures (like linearSearch and increment functions) that accept lists as parameters. Following the hint from Question 5 about using built-in sort functions, this solution uses the sort() method to arrange elements and then accesses the second largest using negative indexing (-2), which is a standard Python technique taught in Class 11 NCERT.
Solution Steps
Step 1: Define function with list parameter
Step 2: Sort the list using sort() method
Step 3: Return element at index -2 (second last position)
Step 4: Call function and display result