Python program for arranging the students according to their marks in descending order

Consider a class of 20 students whose names and marks are given to you. The task is to arrange the students according to their marks in decreasing order. Write a python program to perform the task.

Examples:

Input:
Arun: 78%
Geeta: 86%
Shilpi: 65%

Output: 
Geeta: 86%
Arun: 78%
Shilpi: 65%


Approach: Since problem states that we need to collect student name and their marks first so for this we will take inputs one by one in the list data structure then sort them in reverse order using sorted() built-in function based on the student’s percentage and in the last we will print the value accordingly.

Below is the implementation:



filter_none

edit
close

play_arrow

link
brightness_4
code

print("-----Program for printing student name with marks using list-----")
  
# create an empty dictionary
D = {}
  
n = int(input('How many student record you want to store?? '))
  
# create an empty list
# Add student information to the list
ls = []
  
for i in range(0, n):
    
      # Take combined input name and 
    # percentage and split values 
    # using split function.
    x,y = input("Enter the student name and it's percentage: ").split()
      
    # Add name and marks stored in x, y
    # respectively using tuple to the list
    ls.append((y,x))
      
# sort the elements of list
# based on marks
ls = sorted(ls, reverse = True)
  
print('Sorted list of students according to their marks in descending order')
  
for i in ls:
    
    # print name and marks stored in 
    # second and first position 
    # respectively in list of tuples.
    print(i[1], i[0])
chevron_right

Output:

-----Program for printing student name with marks using list-----
How many student record you want to store?? 3
Enter the student name and percentage: Arun: 78%
Enter the student name and percentage: Geeta: 86%
Enter the student name and percentage: Shilpi: 65%
Sorted list of students according to their marks in descending order
Geeta: 86%
Arun: 78%
Shilpi: 65%


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :