This is a Python Program to find the second largest number in a list.
The program takes a list and prints the second largest number in the list.
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the second last element of the list.
5. Exit.
Here is source code of the Python Program to find the second largest number in a list. The program output is also shown below.
a=[] n=int(input("Enter number of elements:")) for i in range(1,n+1): b=int(input("Enter element:")) a.append(b) a.sort() print("Second largest element is:",a[n-2])
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. The list should then be sorted.
4. Then the last element of the list is printed which is also the largest element of the list.
Case 1: Enter number of elements:4 Enter element:23 Enter element:56 Enter element:39 Enter element:11 Second largest element is: 39 Case 2: Enter number of elements:3 Enter element:23 Enter element:4 Enter element:67 Second largest element is: 23
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Information Technology Books
- Apply for Programming Internship
- Apply for Python Internship
- Practice Programming MCQs
- Check Python Books