Python Lists Program To Sort N Numbers List

By | March 23, 2020

Topic: Python Lists Program To Sort N Numbers List

# Python program to sort n numbers 
# List and print unsorted and sorted List
# using sort() method
  
def main():
    
    # create empty list so that user will input at runtime
    number_list = [] 
  
    # ask the user to input n / how many numbers? 
    num = int(input("How many numbers you wish to Enter in list:")) 
  
    # Use for loop to enter n numbers in the list 
    for i in range(0, num): 
        item = int(input("Enter Number "+str(i+1)+" in List: ")) 
        number_list.append(item)
    
    
    # print Un Sorted List
    print("Un sorted List is:",number_list)
    # Sort the actual list using sort() method and print Sorted List 
    number_list.sort()
    print("The Sorted List using sort() method is\n", number_list)

main()    

Output

How many numbers you wish to Enter in list:5
Enter Number 1 in List: 2344
Enter Number 2 in List: 675
Enter Number 3 in List: 1212
Enter Number 4 in List: 666
Enter Number 5 in List: 23
Un sorted List is: [2344, 675, 1212, 666, 23]
The Sorted List using sort() method is
 [23, 666, 675, 1212, 2344]

Leave a Reply

Your email address will not be published. Required fields are marked *