Python Array Example - Insert and sort the array items

Published November 16, 2021


In this Python array examples Ask the user to Enter five integeres, then Store them in an array. After that sort the list and display them in reveres order.

 

Let's write the program for the above

from array import *
nums =array('i',[])

for i in range (0,5):
   num = int(raw_input("Enter a Number: "))
   nums.append(num)
nums = sorted(nums)
nums.reverse()
print(nums)

 

  • In the above example we created array as nums
  • Then looping 5 times to ask the user to eneter numbers to array by nums.append(num)
  • Then sort the created array with sorted() method sorted(nums)
  • Then reverse the array with array reverse() method
  • Finally Print the array

 

Output:

Enter a Number: 21
Enter a Number: 13
Enter a Number: 56
Enter a Number: 12
Enter a Number: 23
[56, 23, 21, 13, 12]

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

10 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions